Implementation of the 2.0 session state model
[mono.git] / mcs / class / System.Web / System.Web.SessionState_2.0 / HttpSessionStateContainer.cs
1 //
2 // System.Web.Compilation.SessionStateItemCollection
3 //
4 // Authors:
5 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //   Marek Habersack (grendello@gmail.com)
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 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 #if NET_2_0
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.Collections.Specialized;
37 using System.Globalization;
38 using System.Text;
39 using System.Threading;
40 using System.Web.Util;
41
42 namespace System.Web.SessionState 
43 {
44         public class HttpSessionStateContainer : IHttpSessionState
45         {
46                 string id;
47                 HttpStaticObjectsCollection staticObjects;
48                 int timeout;
49                 bool newSession;
50                 bool isCookieless;
51                 SessionStateMode mode;
52                 bool isReadOnly;
53                 internal bool abandoned;
54                 ISessionStateItemCollection sessionItems;
55                 HttpCookieMode cookieMode;
56                 
57                 public HttpSessionStateContainer (string id,
58                                                   ISessionStateItemCollection sessionItems,
59                                                   HttpStaticObjectsCollection staticObjects,
60                                                   int timeout,
61                                                   bool newSession,
62                                                   HttpCookieMode cookieMode,
63                                                   SessionStateMode mode,
64                                                   bool isReadonly)
65                 {
66                         if (id == null)
67                                 throw new ArgumentNullException ("id");
68                         
69                         this.sessionItems = sessionItems;
70                         this.id = id;
71                         this.staticObjects = staticObjects;
72                         this.timeout = timeout;
73                         this.newSession = newSession;
74                         this.cookieMode = cookieMode;
75                         this.mode = mode;
76                         this.isReadOnly = isReadonly;
77                 }
78                 
79                 public int CodePage {
80                         get {
81                                 HttpContext current = HttpContext.Current;
82                                 if (current == null)
83                                         return Encoding.Default.CodePage;
84
85                                 return current.Response.ContentEncoding.CodePage;
86                         }
87                         
88                         set {
89                                 HttpContext current = HttpContext.Current;
90                                 if (current != null)
91                                         current.Response.ContentEncoding = Encoding.GetEncoding (value);
92                         }
93                 }
94                 
95                 public HttpCookieMode CookieMode {
96                         get { return cookieMode; }
97                 }
98                 
99                 public int Count {
100                         get {
101                                 if (sessionItems != null)
102                                         return sessionItems.Count;
103                                 return 0;
104                         }
105                 }
106                 
107                 public bool IsAbandoned {
108                         get { return abandoned; }
109                 }
110                 
111                 public bool IsCookieless {
112                         get { return isCookieless; }
113                 }
114                 
115                 public bool IsNewSession {
116                         get { return newSession; }
117                 }
118                 
119                 public bool IsReadOnly {
120                         get { return isReadOnly; }
121                 }
122                 
123                 public bool IsSynchronized {
124                         get { return false; }
125                 }
126                 
127                 object IHttpSessionState.this [int index] {
128                         get {
129                                 if (sessionItems == null || sessionItems.Count == 0)
130                                         return null;
131                                 return sessionItems [index];
132                         }
133                         
134                         set {
135                                 if (sessionItems != null)
136                                         sessionItems [index] = value;
137                         }
138                 }
139                 
140                 object IHttpSessionState.this [string name] {
141                         get {
142                                 if (sessionItems == null || sessionItems.Count == 0)
143                                         return null;
144                                 return sessionItems [name];
145                         }
146                         
147                         set {
148                                 if (sessionItems != null)
149                                         sessionItems [name] = value;
150                         }
151                 }
152                 
153                 NameObjectCollectionBase.KeysCollection IHttpSessionState.Keys {
154                         get {
155                                 if (sessionItems != null)
156                                         return sessionItems.Keys;
157                                 return null;
158                         }
159                 }
160                 
161                 public int LCID {
162                         get { return Thread.CurrentThread.CurrentCulture.LCID; }
163                         set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
164                 }
165                 
166                 public SessionStateMode Mode {
167                         get { return mode; }
168                 }
169                 
170                 public string SessionID {
171                         get { return id; }
172                 }
173                 
174                 public HttpStaticObjectsCollection StaticObjects {
175                         get { return staticObjects; }
176                 }
177                 
178                 public Object SyncRoot {
179                         get { return this; }
180                 }
181                 
182                 public int Timeout {
183                         get { return timeout; }
184                         set {
185                                 if (value < 1)
186                                         throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
187                                 timeout = value;
188                         }
189                 }
190
191                 internal void SetNewSession (bool value)
192                 {
193                         newSession = value;
194                 }
195                 
196                 public void Abandon ()
197                 {
198                         abandoned = true;
199                 }
200
201                 public void Add (string name, Object value)
202                 {
203                         if (sessionItems == null)
204                                 return;
205                         sessionItems [name] = value;
206                 }
207
208                 public void Clear ()
209                 {
210                         if (sessionItems == null)
211                                 return;
212                         sessionItems.Clear ();
213                 }
214
215                 public void CopyTo (Array array, int index)
216                 {
217                         if (sessionItems == null)
218                                 return;
219                         NameObjectCollectionBase.KeysCollection all = sessionItems.Keys;
220                         for (int i = 0; i < all.Count; i++)
221                                 array.SetValue (all.Get(i), i + index);
222                 }
223
224                 public IEnumerator GetEnumerator ()
225                 {
226                         if (sessionItems == null)
227                                 return null;
228                         return sessionItems.GetEnumerator ();
229                 }
230                 
231                 public void Remove (string name)
232                 {
233                         if (sessionItems == null)
234                                 return;
235                         sessionItems.Remove (name);
236                 }
237
238                 public void RemoveAll ()
239                 {
240                         if (sessionItems == null)
241                                 return;
242                         sessionItems.Clear ();
243                 }
244
245                 public void RemoveAt (int index)
246                 {
247                         if (sessionItems == null)
248                                 return;
249                         sessionItems.RemoveAt (index);
250                 }
251         }
252 }
253 #endif