HttpSessionStateContainer.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // System.Web.Compilation.SessionStateItemCollection
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Marek Habersack ([email protected])
  7. //
  8. // (C) 2006 Marek Habersack
  9. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  10. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  11. //
  12. //
  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:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  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.
  31. //
  32. #if NET_2_0
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Collections.Specialized;
  36. using System.Globalization;
  37. using System.Text;
  38. using System.Threading;
  39. using System.Web.Util;
  40. namespace System.Web.SessionState
  41. {
  42. public class HttpSessionStateContainer : IHttpSessionState
  43. {
  44. string id;
  45. HttpStaticObjectsCollection staticObjects;
  46. int timeout;
  47. bool newSession;
  48. bool isCookieless;
  49. SessionStateMode mode;
  50. bool isReadOnly;
  51. internal bool abandoned;
  52. ISessionStateItemCollection sessionItems;
  53. HttpCookieMode cookieMode;
  54. public HttpSessionStateContainer (string id,
  55. ISessionStateItemCollection sessionItems,
  56. HttpStaticObjectsCollection staticObjects,
  57. int timeout,
  58. bool newSession,
  59. HttpCookieMode cookieMode,
  60. SessionStateMode mode,
  61. bool isReadonly)
  62. {
  63. if (id == null)
  64. throw new ArgumentNullException ("id");
  65. this.sessionItems = sessionItems;
  66. this.id = id;
  67. this.staticObjects = staticObjects;
  68. this.timeout = timeout;
  69. this.newSession = newSession;
  70. this.cookieMode = cookieMode;
  71. this.mode = mode;
  72. this.isReadOnly = isReadonly;
  73. this.isCookieless = cookieMode == HttpCookieMode.UseUri;
  74. }
  75. public int CodePage {
  76. get {
  77. HttpContext current = HttpContext.Current;
  78. if (current == null)
  79. return Encoding.Default.CodePage;
  80. return current.Response.ContentEncoding.CodePage;
  81. }
  82. set {
  83. HttpContext current = HttpContext.Current;
  84. if (current != null)
  85. current.Response.ContentEncoding = Encoding.GetEncoding (value);
  86. }
  87. }
  88. public HttpCookieMode CookieMode {
  89. get { return cookieMode; }
  90. }
  91. public int Count {
  92. get {
  93. if (sessionItems != null)
  94. return sessionItems.Count;
  95. return 0;
  96. }
  97. }
  98. public bool IsAbandoned {
  99. get { return abandoned; }
  100. }
  101. public bool IsCookieless {
  102. get { return isCookieless; }
  103. }
  104. public bool IsNewSession {
  105. get { return newSession; }
  106. }
  107. public bool IsReadOnly {
  108. get { return isReadOnly; }
  109. }
  110. public bool IsSynchronized {
  111. get { return false; }
  112. }
  113. object IHttpSessionState.this [int index] {
  114. get {
  115. if (sessionItems == null || sessionItems.Count == 0)
  116. return null;
  117. return sessionItems [index];
  118. }
  119. set {
  120. if (sessionItems != null)
  121. sessionItems [index] = value;
  122. }
  123. }
  124. object IHttpSessionState.this [string name] {
  125. get {
  126. if (sessionItems == null || sessionItems.Count == 0)
  127. return null;
  128. return sessionItems [name];
  129. }
  130. set {
  131. if (sessionItems != null)
  132. sessionItems [name] = value;
  133. }
  134. }
  135. NameObjectCollectionBase.KeysCollection IHttpSessionState.Keys {
  136. get {
  137. if (sessionItems != null)
  138. return sessionItems.Keys;
  139. return null;
  140. }
  141. }
  142. public int LCID {
  143. get { return Thread.CurrentThread.CurrentCulture.LCID; }
  144. set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
  145. }
  146. public SessionStateMode Mode {
  147. get { return mode; }
  148. }
  149. public string SessionID {
  150. get { return id; }
  151. }
  152. public HttpStaticObjectsCollection StaticObjects {
  153. get { return staticObjects; }
  154. }
  155. public Object SyncRoot {
  156. get { return this; }
  157. }
  158. public int Timeout {
  159. get { return timeout; }
  160. set {
  161. if (value < 1)
  162. throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
  163. timeout = value;
  164. }
  165. }
  166. internal void SetNewSession (bool value)
  167. {
  168. newSession = value;
  169. }
  170. public void Abandon ()
  171. {
  172. abandoned = true;
  173. }
  174. public void Add (string name, Object value)
  175. {
  176. if (sessionItems == null)
  177. return;
  178. sessionItems [name] = value;
  179. }
  180. public void Clear ()
  181. {
  182. if (sessionItems == null)
  183. return;
  184. sessionItems.Clear ();
  185. }
  186. public void CopyTo (Array array, int index)
  187. {
  188. if (sessionItems == null)
  189. return;
  190. NameObjectCollectionBase.KeysCollection all = sessionItems.Keys;
  191. for (int i = 0; i < all.Count; i++)
  192. array.SetValue (all.Get(i), i + index);
  193. }
  194. public IEnumerator GetEnumerator ()
  195. {
  196. if (sessionItems == null)
  197. return null;
  198. return sessionItems.GetEnumerator ();
  199. }
  200. public void Remove (string name)
  201. {
  202. if (sessionItems == null)
  203. return;
  204. sessionItems.Remove (name);
  205. }
  206. public void RemoveAll ()
  207. {
  208. if (sessionItems == null)
  209. return;
  210. sessionItems.Clear ();
  211. }
  212. public void RemoveAt (int index)
  213. {
  214. if (sessionItems == null)
  215. return;
  216. sessionItems.RemoveAt (index);
  217. }
  218. }
  219. }
  220. #endif