HttpSessionState.jvm.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 object _app;
  54. private bool _readFirstTime = true;
  55. private bool _needSessionPersistence = false;
  56. internal HttpSessionState (string id,
  57. SessionDictionary dict,
  58. HttpStaticObjectsCollection staticObjects,
  59. int timeout,
  60. bool newSession,
  61. bool isCookieless,
  62. SessionStateMode mode,
  63. bool isReadonly)
  64. {
  65. _id = id;
  66. _dict = dict;
  67. _staticObjects = staticObjects.Clone ();
  68. _timeout = timeout;
  69. _newSession = newSession;
  70. _isCookieless = isCookieless;
  71. _mode = mode;
  72. _isReadonly = isReadonly;
  73. _app = HttpContext.Current.ApplicationInstance;
  74. _needSessionPersistence = false;
  75. javax.servlet.ServletConfig config = (javax.servlet.ServletConfig)AppDomain.CurrentDomain.GetData(J2EEConsts.SERVLET_CONFIG);
  76. string sessionPersistance = config.getInitParameter(J2EEConsts.Enable_Session_Persistency);
  77. if (sessionPersistance!= null)
  78. {
  79. try
  80. {
  81. _needSessionPersistence = Boolean.Parse(sessionPersistance);
  82. }
  83. catch (Exception)
  84. {
  85. Console.WriteLine("EnableSessionPersistency init param's value is invalid. the value is " + sessionPersistance);
  86. }
  87. }
  88. }
  89. public HttpSessionState ()
  90. {
  91. _id = null;
  92. _dict = new SessionDictionary();
  93. _staticObjects = new HttpStaticObjectsCollection();
  94. _timeout = 0;
  95. _newSession = false;
  96. _isCookieless = false;
  97. _mode = SessionStateMode.Off;
  98. _isReadonly = false;
  99. }
  100. public void writeExternal(java.io.ObjectOutput output)
  101. {
  102. lock (this)
  103. {
  104. output.writeBoolean(_needSessionPersistence);
  105. if (!_needSessionPersistence)
  106. {
  107. return;
  108. }
  109. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  110. System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
  111. bw.Write(_id);
  112. _dict.Serialize(bw);
  113. _staticObjects.Serialize(bw);
  114. bw.Write(_timeout);
  115. bw.Write(_newSession);
  116. bw.Write(_isCookieless);
  117. if (_mode == SessionStateMode.Off)
  118. bw.Write(0);
  119. else if (_mode == SessionStateMode.InProc)
  120. bw.Write(1);
  121. else if (_mode == SessionStateMode.StateServer)
  122. bw.Write(2);
  123. else
  124. bw.Write(3);
  125. bw.Write(_isReadonly);
  126. output.writeObject(vmw.common.TypeUtils.ToSByteArray(ms.GetBuffer()));
  127. }
  128. }
  129. public void readExternal(java.io.ObjectInput input)
  130. {
  131. lock(this)
  132. {
  133. _needSessionPersistence = input.readBoolean();
  134. if (!_needSessionPersistence)
  135. {
  136. return;
  137. }
  138. sbyte[] array = (sbyte[])input.readObject();
  139. if (!_readFirstTime || array == null || array.Length == 0)
  140. {
  141. return;
  142. }
  143. _readFirstTime = false;
  144. System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])vmw.common.TypeUtils.ToByteArray(array));
  145. System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
  146. _id = br.ReadString();
  147. _dict = SessionDictionary.Deserialize(br);
  148. _staticObjects = HttpStaticObjectsCollection.Deserialize(br);
  149. _timeout = br.ReadInt32();
  150. _newSession = br.ReadBoolean();
  151. _isCookieless = br.ReadBoolean();
  152. int mode = br.ReadInt32();
  153. if (mode == 0)
  154. _mode = SessionStateMode.Off;
  155. else if (mode == 1)
  156. _mode = SessionStateMode.InProc;
  157. else if (mode == 2)
  158. _mode = SessionStateMode.StateServer;
  159. else
  160. _mode = SessionStateMode.SQLServer;
  161. _isReadonly = br.ReadBoolean();
  162. // _app = HttpContext.Current.ApplicationInstance;
  163. }
  164. }
  165. internal object App
  166. {
  167. get
  168. {
  169. return _app;
  170. }
  171. }
  172. internal HttpSessionState Clone ()
  173. {
  174. return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
  175. _isCookieless, _mode, _isReadonly);
  176. }
  177. public int CodePage {
  178. get {
  179. HttpContext current = HttpContext.Current;
  180. if (current == null)
  181. return Encoding.Default.CodePage;
  182. return current.Response.ContentEncoding.CodePage;
  183. }
  184. set {
  185. HttpContext current = HttpContext.Current;
  186. if (current != null)
  187. current.Response.ContentEncoding = Encoding.GetEncoding (value);
  188. }
  189. }
  190. public HttpSessionState Contents {
  191. get { return this; }
  192. }
  193. public int Count {
  194. get { return _dict.Count; }
  195. }
  196. internal bool IsAbandoned {
  197. get { return _abandoned; }
  198. }
  199. public bool IsCookieless {
  200. get {
  201. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  202. return worker.ServletRequest.isRequestedSessionIdFromURL();
  203. }
  204. }
  205. public bool IsNewSession {
  206. get { return _newSession; }
  207. }
  208. public bool IsReadOnly {
  209. get { return _isReadonly; }
  210. }
  211. public bool IsSynchronized {
  212. get { return false; }
  213. }
  214. public object this [string key] {
  215. get { return _dict [key]; }
  216. set {
  217. _dict [key] = value;
  218. _newSession = false;
  219. SetJavaSessionAttribute();
  220. }
  221. }
  222. public object this [int index] {
  223. get { return _dict [index]; }
  224. set {
  225. _dict [index] = value;
  226. _newSession = false;
  227. SetJavaSessionAttribute();
  228. }
  229. }
  230. public NameObjectCollectionBase.KeysCollection Keys {
  231. get { return _dict.Keys; }
  232. }
  233. public int LCID {
  234. get { return Thread.CurrentThread.CurrentCulture.LCID; }
  235. set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
  236. }
  237. public SessionStateMode Mode {
  238. get { return _mode; }
  239. }
  240. public string SessionID {
  241. get { return _id; }
  242. }
  243. public HttpStaticObjectsCollection StaticObjects {
  244. get { return _staticObjects; }
  245. }
  246. public object SyncRoot {
  247. get { return this; }
  248. }
  249. public int Timeout {
  250. get {
  251. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  252. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  253. if (javaSession != null)
  254. return javaSession.getMaxInactiveInterval()/60;
  255. else
  256. throw new NotSupportedException();
  257. }
  258. set {
  259. if (value < 1)
  260. throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
  261. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  262. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  263. if (javaSession != null)
  264. javaSession.setMaxInactiveInterval(value*60);
  265. else
  266. throw new NotSupportedException();
  267. }
  268. }
  269. internal SessionDictionary SessionDictionary {
  270. get { return _dict; }
  271. }
  272. internal void SetNewSession (bool value)
  273. {
  274. _newSession = value;
  275. }
  276. public void Abandon ()
  277. {
  278. _abandoned = true;
  279. SessionDictionary.Clear();
  280. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  281. // worker.Servlet.getServletContext().removeAttribute("GH_SESSION_STATE");
  282. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  283. if (_app == null)
  284. _app = HttpContext.Current.ApplicationInstance;
  285. if (javaSession != null)
  286. {
  287. javaSession.setAttribute("GH_SESSION_STATE",this);
  288. javaSession.invalidate();
  289. }
  290. }
  291. public void Add (string name, object value)
  292. {
  293. _dict [name] = value;
  294. _newSession = false;
  295. SetJavaSessionAttribute();
  296. }
  297. public void Clear ()
  298. {
  299. if (_dict != null)
  300. _dict.Clear ();
  301. SetJavaSessionAttribute();
  302. }
  303. public void CopyTo (Array array, int index)
  304. {
  305. NameObjectCollectionBase.KeysCollection all = Keys;
  306. for (int i = 0; i < all.Count; i++)
  307. array.SetValue (all.Get(i), i + index);
  308. }
  309. public IEnumerator GetEnumerator ()
  310. {
  311. return _dict.GetEnumerator ();
  312. }
  313. public void Remove (string name)
  314. {
  315. _dict.Remove (name);
  316. SetJavaSessionAttribute();
  317. }
  318. public void RemoveAll ()
  319. {
  320. _dict.Clear ();
  321. SetJavaSessionAttribute();
  322. }
  323. public void RemoveAt (int index)
  324. {
  325. _dict.RemoveAt (index);
  326. SetJavaSessionAttribute();
  327. }
  328. public void SetJavaSessionAttribute ()
  329. {
  330. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  331. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  332. if (javaSession != null)
  333. javaSession.setAttribute("GH_SESSION_STATE",this);
  334. }
  335. }
  336. }