HttpSessionState.jvm.cs 9.4 KB

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