2 // System.Web.SessionState.HttpSessionState.jvm.cs
5 // Ilya Kharmatsky (ilyak@mainsoft.com)
9 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Globalization;
38 using System.Threading;
40 using System.Web.J2EE;
41 using System.Web.Hosting;
43 namespace System.Web.SessionState
45 public sealed class HttpSessionState : ICollection, IEnumerable, java.io.Externalizable
48 private SessionDictionary _dict;
49 private HttpStaticObjectsCollection _staticObjects;
51 private bool _newSession;
52 private bool _isCookieless;
53 private SessionStateMode _mode;
54 private bool _isReadonly;
55 internal bool _abandoned;
57 private bool _needSessionPersistence = false;
59 internal HttpSessionState (string id,
60 SessionDictionary dict,
61 HttpStaticObjectsCollection staticObjects,
65 SessionStateMode mode,
70 _staticObjects = staticObjects.Clone ();
72 _newSession = newSession;
73 _isCookieless = isCookieless;
75 _isReadonly = isReadonly;
77 _needSessionPersistence = false;
78 javax.servlet.ServletConfig config = (javax.servlet.ServletConfig)AppDomain.CurrentDomain.GetData(J2EEConsts.SERVLET_CONFIG);
79 string sessionPersistance = config.getInitParameter(J2EEConsts.Enable_Session_Persistency);
80 if (sessionPersistance!= null)
84 _needSessionPersistence = Boolean.Parse(sessionPersistance);
88 Console.WriteLine("EnableSessionPersistency init param's value is invalid. the value is " + sessionPersistance);
93 public HttpSessionState ()
96 _dict = new SessionDictionary();
97 _staticObjects = new HttpStaticObjectsCollection();
100 _isCookieless = false;
101 _mode = SessionStateMode.Off;
105 public void writeExternal(java.io.ObjectOutput output)
109 output.writeBoolean(_needSessionPersistence);
110 if (!_needSessionPersistence)
111 //indicates that there is nothing to serialize for this object
114 System.Web.J2EE.ObjectOutputStream ms = new System.Web.J2EE.ObjectOutputStream(output);
115 System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
118 _staticObjects.Serialize(bw);
120 bw.Write(_newSession);
121 bw.Write(_isCookieless);
122 if (_mode == SessionStateMode.Off)
124 else if (_mode == SessionStateMode.InProc)
126 else if (_mode == SessionStateMode.StateServer)
130 bw.Write(_isReadonly);
134 public void readExternal(java.io.ObjectInput input)
138 _needSessionPersistence = input.readBoolean();
139 if(!_needSessionPersistence) //noting has been written
142 System.Web.J2EE.ObjectInputStream ms = new System.Web.J2EE.ObjectInputStream( input );
143 System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
144 _id = br.ReadString();
145 _dict = SessionDictionary.Deserialize(br);
146 _staticObjects = HttpStaticObjectsCollection.Deserialize(br);
147 _timeout = br.ReadInt32();
148 _newSession = br.ReadBoolean();
149 _isCookieless = br.ReadBoolean();
150 int mode = br.ReadInt32();
152 _mode = SessionStateMode.Off;
154 _mode = SessionStateMode.InProc;
156 _mode = SessionStateMode.StateServer;
158 _mode = SessionStateMode.SQLServer;
159 _isReadonly = br.ReadBoolean();
163 internal HttpSessionState Clone ()
165 return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
166 _isCookieless, _mode, _isReadonly);
170 public int CodePage {
172 HttpContext current = HttpContext.Current;
174 return Encoding.Default.CodePage;
176 return current.Response.ContentEncoding.CodePage;
180 HttpContext current = HttpContext.Current;
182 current.Response.ContentEncoding = Encoding.GetEncoding (value);
186 public HttpSessionState Contents {
191 get { return _dict.Count; }
194 internal bool IsAbandoned {
195 get { return _abandoned; }
198 public bool IsCookieless {
200 ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
201 return worker.ServletRequest.isRequestedSessionIdFromURL();
205 public bool IsNewSession {
206 get { return _newSession; }
209 public bool IsReadOnly {
210 get { return _isReadonly; }
213 public bool IsSynchronized {
214 get { return false; }
217 public object this [string key] {
218 get { return _dict [key]; }
223 SetJavaSessionAttribute();
227 public object this [int index] {
228 get { return _dict [index]; }
230 _dict [index] = value;
233 SetJavaSessionAttribute();
237 public NameObjectCollectionBase.KeysCollection Keys {
238 get { return _dict.Keys; }
242 get { return Thread.CurrentThread.CurrentCulture.LCID; }
243 set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
246 public SessionStateMode Mode {
247 get { return _mode; }
250 public string SessionID {
254 public HttpStaticObjectsCollection StaticObjects {
255 get { return _staticObjects; }
258 public object SyncRoot {
264 ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
265 javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
266 if (javaSession != null)
267 return javaSession.getMaxInactiveInterval()/60;
269 throw new NotSupportedException();
273 throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
274 ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
275 javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
276 if (javaSession != null)
277 javaSession.setMaxInactiveInterval(value*60);
279 throw new NotSupportedException();
283 internal SessionDictionary SessionDictionary {
284 get { return _dict; }
287 internal void SetNewSession (bool value)
292 public void Abandon ()
296 SessionDictionary.Clear();
297 ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
298 // worker.Servlet.getServletContext().removeAttribute("GH_SESSION_STATE");
299 javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
301 if (javaSession != null)
303 javaSession.setAttribute("GH_SESSION_STATE",this);
304 javaSession.invalidate();
308 public void Add (string name, object value)
310 _dict [name] = value;
313 SetJavaSessionAttribute();
321 SetJavaSessionAttribute();
324 public void CopyTo (Array array, int index)
326 NameObjectCollectionBase.KeysCollection all = Keys;
327 for (int i = 0; i < all.Count; i++)
328 array.SetValue (all.Get(i), i + index);
331 public IEnumerator GetEnumerator ()
333 return _dict.GetEnumerator ();
336 public void Remove (string name)
340 SetJavaSessionAttribute();
343 public void RemoveAll ()
347 SetJavaSessionAttribute();
350 public void RemoveAt (int index)
352 _dict.RemoveAt (index);
354 SetJavaSessionAttribute();
357 public void SetJavaSessionAttribute ()
359 ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
360 javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
361 if (javaSession != null)
362 javaSession.setAttribute("GH_SESSION_STATE",this);