HttpSessionStateContainer.cs 5.8 KB

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