HttpSessionState.jvm.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //
  2. // System.Web.SessionState.HttpSessionState.jvm.cs
  3. //
  4. // Authors:
  5. // Ilya Kharmatsky ([email protected])
  6. // Alon Gazit
  7. // Pavel Sandler
  8. //
  9. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Globalization;
  35. using System.Text;
  36. using System.Threading;
  37. using System.Web;
  38. using System.Web.J2EE;
  39. using System.Web.Hosting;
  40. namespace System.Web.SessionState
  41. {
  42. public sealed class HttpSessionState : ICollection, IEnumerable, java.io.Externalizable
  43. {
  44. private string _id;
  45. private SessionDictionary _dict;
  46. private HttpStaticObjectsCollection _staticObjects;
  47. private int _timeout;
  48. private bool _newSession;
  49. private bool _isCookieless;
  50. private SessionStateMode _mode;
  51. private bool _isReadonly;
  52. internal bool _abandoned;
  53. private bool _needSessionPersistence = false;
  54. internal HttpSessionState (string id,
  55. SessionDictionary dict,
  56. HttpStaticObjectsCollection staticObjects,
  57. int timeout,
  58. bool newSession,
  59. bool isCookieless,
  60. SessionStateMode mode,
  61. bool isReadonly)
  62. {
  63. _id = id;
  64. _dict = dict;
  65. _staticObjects = staticObjects.Clone ();
  66. _timeout = timeout;
  67. _newSession = newSession;
  68. _isCookieless = isCookieless;
  69. _mode = mode;
  70. _isReadonly = isReadonly;
  71. _needSessionPersistence = false;
  72. javax.servlet.ServletConfig config = (javax.servlet.ServletConfig)AppDomain.CurrentDomain.GetData(J2EEConsts.SERVLET_CONFIG);
  73. string sessionPersistance = config.getInitParameter(J2EEConsts.Enable_Session_Persistency);
  74. if (sessionPersistance!= null)
  75. {
  76. try
  77. {
  78. _needSessionPersistence = Boolean.Parse(sessionPersistance);
  79. }
  80. catch (Exception)
  81. {
  82. Console.WriteLine("EnableSessionPersistency init param's value is invalid. the value is " + sessionPersistance);
  83. }
  84. }
  85. }
  86. public HttpSessionState ()
  87. {
  88. _id = null;
  89. _dict = new SessionDictionary();
  90. _staticObjects = new HttpStaticObjectsCollection();
  91. _timeout = 0;
  92. _newSession = false;
  93. _isCookieless = false;
  94. _mode = SessionStateMode.Off;
  95. _isReadonly = false;
  96. }
  97. public void writeExternal(java.io.ObjectOutput output)
  98. {
  99. lock (this)
  100. {
  101. output.writeBoolean(_needSessionPersistence);
  102. if (!_needSessionPersistence)
  103. //indicates that there is nothing to serialize for this object
  104. return;
  105. System.Web.J2EE.ObjectOutputStream ms = new System.Web.J2EE.ObjectOutputStream(output);
  106. System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
  107. bw.Write(_id);
  108. _dict.Serialize(bw);
  109. _staticObjects.Serialize(bw);
  110. bw.Write(_timeout);
  111. bw.Write(_newSession);
  112. bw.Write(_isCookieless);
  113. if (_mode == SessionStateMode.Off)
  114. bw.Write(0);
  115. else if (_mode == SessionStateMode.InProc)
  116. bw.Write(1);
  117. else if (_mode == SessionStateMode.StateServer)
  118. bw.Write(2);
  119. else
  120. bw.Write(3);
  121. bw.Write(_isReadonly);
  122. }
  123. }
  124. public void readExternal(java.io.ObjectInput input)
  125. {
  126. lock(this)
  127. {
  128. _needSessionPersistence = input.readBoolean();
  129. if(!_needSessionPersistence) //noting has been written
  130. return;
  131. System.Web.J2EE.ObjectInputStream ms = new System.Web.J2EE.ObjectInputStream( input );
  132. System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
  133. _id = br.ReadString();
  134. _dict = SessionDictionary.Deserialize(br);
  135. _staticObjects = HttpStaticObjectsCollection.Deserialize(br);
  136. _timeout = br.ReadInt32();
  137. _newSession = br.ReadBoolean();
  138. _isCookieless = br.ReadBoolean();
  139. int mode = br.ReadInt32();
  140. if (mode == 0)
  141. _mode = SessionStateMode.Off;
  142. else if (mode == 1)
  143. _mode = SessionStateMode.InProc;
  144. else if (mode == 2)
  145. _mode = SessionStateMode.StateServer;
  146. else
  147. _mode = SessionStateMode.SQLServer;
  148. _isReadonly = br.ReadBoolean();
  149. }
  150. }
  151. internal HttpSessionState Clone ()
  152. {
  153. return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
  154. _isCookieless, _mode, _isReadonly);
  155. }
  156. public int CodePage {
  157. get {
  158. HttpContext current = HttpContext.Current;
  159. if (current == null)
  160. return Encoding.Default.CodePage;
  161. return current.Response.ContentEncoding.CodePage;
  162. }
  163. set {
  164. HttpContext current = HttpContext.Current;
  165. if (current != null)
  166. current.Response.ContentEncoding = Encoding.GetEncoding (value);
  167. }
  168. }
  169. public HttpSessionState Contents {
  170. get { return this; }
  171. }
  172. public int Count {
  173. get { return _dict.Count; }
  174. }
  175. internal bool IsAbandoned {
  176. get { return _abandoned; }
  177. }
  178. public bool IsCookieless {
  179. get {
  180. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  181. return worker.ServletRequest.isRequestedSessionIdFromURL();
  182. }
  183. }
  184. public bool IsNewSession {
  185. get { return _newSession; }
  186. }
  187. public bool IsReadOnly {
  188. get { return _isReadonly; }
  189. }
  190. public bool IsSynchronized {
  191. get { return false; }
  192. }
  193. public object this [string key] {
  194. get { return _dict [key]; }
  195. set {
  196. _dict [key] = value;
  197. _newSession = false;
  198. SetJavaSessionAttribute();
  199. }
  200. }
  201. public object this [int index] {
  202. get { return _dict [index]; }
  203. set {
  204. _dict [index] = value;
  205. _newSession = false;
  206. SetJavaSessionAttribute();
  207. }
  208. }
  209. public NameObjectCollectionBase.KeysCollection Keys {
  210. get { return _dict.Keys; }
  211. }
  212. public int LCID {
  213. get { return Thread.CurrentThread.CurrentCulture.LCID; }
  214. set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
  215. }
  216. public SessionStateMode Mode {
  217. get { return _mode; }
  218. }
  219. public string SessionID {
  220. get { return _id; }
  221. }
  222. public HttpStaticObjectsCollection StaticObjects {
  223. get { return _staticObjects; }
  224. }
  225. public object SyncRoot {
  226. get { return this; }
  227. }
  228. public int Timeout {
  229. get {
  230. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  231. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  232. if (javaSession != null)
  233. return javaSession.getMaxInactiveInterval()/60;
  234. else
  235. throw new NotSupportedException();
  236. }
  237. set {
  238. if (value < 1)
  239. throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
  240. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  241. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  242. if (javaSession != null)
  243. javaSession.setMaxInactiveInterval(value*60);
  244. else
  245. throw new NotSupportedException();
  246. }
  247. }
  248. internal SessionDictionary SessionDictionary {
  249. get { return _dict; }
  250. }
  251. internal void SetNewSession (bool value)
  252. {
  253. _newSession = value;
  254. }
  255. public void Abandon ()
  256. {
  257. _abandoned = true;
  258. SessionDictionary.Clear();
  259. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  260. // worker.Servlet.getServletContext().removeAttribute("GH_SESSION_STATE");
  261. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  262. if (javaSession != null)
  263. {
  264. javaSession.setAttribute("GH_SESSION_STATE",this);
  265. javaSession.invalidate();
  266. }
  267. }
  268. public void Add (string name, object value)
  269. {
  270. _dict [name] = value;
  271. _newSession = false;
  272. SetJavaSessionAttribute();
  273. }
  274. public void Clear ()
  275. {
  276. if (_dict != null)
  277. _dict.Clear ();
  278. SetJavaSessionAttribute();
  279. }
  280. public void CopyTo (Array array, int index)
  281. {
  282. NameObjectCollectionBase.KeysCollection all = Keys;
  283. for (int i = 0; i < all.Count; i++)
  284. array.SetValue (all.Get(i), i + index);
  285. }
  286. public IEnumerator GetEnumerator ()
  287. {
  288. return _dict.GetEnumerator ();
  289. }
  290. public void Remove (string name)
  291. {
  292. _dict.Remove (name);
  293. SetJavaSessionAttribute();
  294. }
  295. public void RemoveAll ()
  296. {
  297. _dict.Clear ();
  298. SetJavaSessionAttribute();
  299. }
  300. public void RemoveAt (int index)
  301. {
  302. _dict.RemoveAt (index);
  303. SetJavaSessionAttribute();
  304. }
  305. public void SetJavaSessionAttribute ()
  306. {
  307. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  308. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  309. if (javaSession != null)
  310. javaSession.setAttribute("GH_SESSION_STATE",this);
  311. }
  312. }
  313. }