HttpSessionState.jvm.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. if (!_needSessionPersistence)
  105. {
  106. return;
  107. }
  108. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  109. System.IO.BinaryWriter bw = new System.IO.BinaryWriter(ms);
  110. bw.Write(_id);
  111. _dict.Serialize(bw);
  112. _staticObjects.Serialize(bw);
  113. bw.Write(_timeout);
  114. bw.Write(_newSession);
  115. bw.Write(_isCookieless);
  116. if (_mode == SessionStateMode.Off)
  117. bw.Write(0);
  118. else if (_mode == SessionStateMode.InProc)
  119. bw.Write(1);
  120. else if (_mode == SessionStateMode.StateServer)
  121. bw.Write(2);
  122. else
  123. bw.Write(3);
  124. bw.Write(_isReadonly);
  125. bw.Write(_needSessionPersistence);
  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. sbyte[] array = (sbyte[])input.readObject();
  134. if (!_readFirstTime || array == null || array.Length == 0)
  135. {
  136. return;
  137. }
  138. _readFirstTime = false;
  139. System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])vmw.common.TypeUtils.ToByteArray(array));
  140. System.IO.BinaryReader br = new System.IO.BinaryReader(ms);
  141. _id = br.ReadString();
  142. _dict = SessionDictionary.Deserialize(br);
  143. _staticObjects = HttpStaticObjectsCollection.Deserialize(br);
  144. _timeout = br.ReadInt32();
  145. _newSession = br.ReadBoolean();
  146. _isCookieless = br.ReadBoolean();
  147. int mode = br.ReadInt32();
  148. if (mode == 0)
  149. _mode = SessionStateMode.Off;
  150. else if (mode == 1)
  151. _mode = SessionStateMode.InProc;
  152. else if (mode == 2)
  153. _mode = SessionStateMode.StateServer;
  154. else
  155. _mode = SessionStateMode.SQLServer;
  156. _isReadonly = br.ReadBoolean();
  157. _needSessionPersistence = br.ReadBoolean();
  158. // _app = HttpContext.Current.ApplicationInstance;
  159. }
  160. }
  161. internal object App
  162. {
  163. get
  164. {
  165. return _app;
  166. }
  167. }
  168. internal HttpSessionState Clone ()
  169. {
  170. return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
  171. _isCookieless, _mode, _isReadonly);
  172. }
  173. public int CodePage {
  174. get {
  175. HttpContext current = HttpContext.Current;
  176. if (current == null)
  177. return Encoding.Default.CodePage;
  178. return current.Response.ContentEncoding.CodePage;
  179. }
  180. set {
  181. HttpContext current = HttpContext.Current;
  182. if (current != null)
  183. current.Response.ContentEncoding = Encoding.GetEncoding (value);
  184. }
  185. }
  186. public HttpSessionState Contents {
  187. get { return this; }
  188. }
  189. public int Count {
  190. get { return _dict.Count; }
  191. }
  192. internal bool IsAbandoned {
  193. get { return _abandoned; }
  194. }
  195. public bool IsCookieless {
  196. get {
  197. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  198. return worker.ServletRequest.isRequestedSessionIdFromURL();
  199. }
  200. }
  201. public bool IsNewSession {
  202. get { return _newSession; }
  203. }
  204. public bool IsReadOnly {
  205. get { return _isReadonly; }
  206. }
  207. public bool IsSynchronized {
  208. get { return false; }
  209. }
  210. public object this [string key] {
  211. get { return _dict [key]; }
  212. set {
  213. _dict [key] = value;
  214. _newSession = false;
  215. SetJavaSessionAttribute();
  216. }
  217. }
  218. public object this [int index] {
  219. get { return _dict [index]; }
  220. set {
  221. _dict [index] = value;
  222. _newSession = false;
  223. SetJavaSessionAttribute();
  224. }
  225. }
  226. public NameObjectCollectionBase.KeysCollection Keys {
  227. get { return _dict.Keys; }
  228. }
  229. public int LCID {
  230. get { return Thread.CurrentThread.CurrentCulture.LCID; }
  231. set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
  232. }
  233. public SessionStateMode Mode {
  234. get { return _mode; }
  235. }
  236. public string SessionID {
  237. get { return _id; }
  238. }
  239. public HttpStaticObjectsCollection StaticObjects {
  240. get { return _staticObjects; }
  241. }
  242. public object SyncRoot {
  243. get { return this; }
  244. }
  245. public int Timeout {
  246. get {
  247. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  248. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  249. if (javaSession != null)
  250. return javaSession.getMaxInactiveInterval()/60;
  251. else
  252. throw new NotSupportedException();
  253. }
  254. set {
  255. if (value < 1)
  256. throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
  257. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  258. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  259. if (javaSession != null)
  260. javaSession.setMaxInactiveInterval(value*60);
  261. else
  262. throw new NotSupportedException();
  263. }
  264. }
  265. internal SessionDictionary SessionDictionary {
  266. get { return _dict; }
  267. }
  268. internal void SetNewSession (bool value)
  269. {
  270. _newSession = value;
  271. }
  272. public void Abandon ()
  273. {
  274. _abandoned = true;
  275. SessionDictionary.Clear();
  276. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  277. // worker.Servlet.getServletContext().removeAttribute("GH_SESSION_STATE");
  278. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  279. if (_app == null)
  280. _app = HttpContext.Current.ApplicationInstance;
  281. if (javaSession != null)
  282. {
  283. javaSession.setAttribute("GH_SESSION_STATE",this);
  284. javaSession.invalidate();
  285. }
  286. }
  287. public void Add (string name, object value)
  288. {
  289. _dict [name] = value;
  290. _newSession = false;
  291. SetJavaSessionAttribute();
  292. }
  293. public void Clear ()
  294. {
  295. if (_dict != null)
  296. _dict.Clear ();
  297. SetJavaSessionAttribute();
  298. }
  299. public void CopyTo (Array array, int index)
  300. {
  301. NameObjectCollectionBase.KeysCollection all = Keys;
  302. for (int i = 0; i < all.Count; i++)
  303. array.SetValue (all.Get(i), i + index);
  304. }
  305. public IEnumerator GetEnumerator ()
  306. {
  307. return _dict.GetEnumerator ();
  308. }
  309. public void Remove (string name)
  310. {
  311. _dict.Remove (name);
  312. SetJavaSessionAttribute();
  313. }
  314. public void RemoveAll ()
  315. {
  316. _dict.Clear ();
  317. SetJavaSessionAttribute();
  318. }
  319. public void RemoveAt (int index)
  320. {
  321. _dict.RemoveAt (index);
  322. SetJavaSessionAttribute();
  323. }
  324. public void SetJavaSessionAttribute ()
  325. {
  326. ServletWorkerRequest worker = (ServletWorkerRequest)HttpContext.Current.Request.WorkerRequest;
  327. javax.servlet.http.HttpSession javaSession = worker.ServletRequest.getSession(false);
  328. if (javaSession != null)
  329. javaSession.setAttribute("GH_SESSION_STATE",this);
  330. }
  331. }
  332. }