svn path=/branches/mono-1-1-9/mono/; revision=51217
[mono.git] / mcs / class / System.Web / System.Web / HttpApplicationState.cs
1 // 
2 // System.Web.HttpApplicationState
3 //
4 // Author:
5 //   Patrik Torstensson
6 //
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Threading;
30 using System.Web;
31 using System.Collections.Specialized;
32
33 namespace System.Web \r
34 {
35
36         public sealed class HttpApplicationState : NameObjectCollectionBase \r
37         {
38                 private HttpStaticObjectsCollection _AppObjects;
39                 private HttpStaticObjectsCollection _SessionObjects;
40
41                 private ReaderWriterLock _Lock; 
42
43                 internal HttpApplicationState ()
44                 {
45                         _AppObjects = new HttpStaticObjectsCollection ();
46                         _SessionObjects = new HttpStaticObjectsCollection ();
47                         _Lock = new ReaderWriterLock ();
48                 }
49
50                 internal HttpApplicationState (HttpStaticObjectsCollection AppObj,
51                         HttpStaticObjectsCollection SessionObj)
52                 {
53                         if (null != AppObj) \r
54                         {
55                                 _AppObjects = AppObj;
56                         } \r
57                         else \r
58                         {
59                                 _AppObjects = new HttpStaticObjectsCollection ();
60                         }
61
62                         if (null != SessionObj) \r
63                         {
64                                 _SessionObjects = SessionObj;
65                         } \r
66                         else \r
67                         {
68                                 _SessionObjects = new HttpStaticObjectsCollection ();
69                         }
70                         _Lock = new ReaderWriterLock ();
71                 }
72
73                 public void Add (string name, object value)
74                 {
75                         _Lock.AcquireWriterLock (-1); 
76                         try \r
77                         {
78                                 BaseAdd (name, value);
79                         } \r
80                         finally \r
81                         {
82                                 _Lock.ReleaseWriterLock ();
83                         }
84                 }
85
86                 public void Clear ()
87                 {
88                         _Lock.AcquireWriterLock (-1); 
89                         try \r
90                         {
91                                 BaseClear ();
92                         } \r
93                         finally \r
94                         {
95                                 _Lock.ReleaseWriterLock ();
96                         }
97                 } 
98
99                 public object Get (string name)
100                 {
101                         object ret = null;
102
103                         _Lock.AcquireReaderLock (-1); 
104                         try \r
105                         {
106                                 ret = BaseGet (name);
107                         } \r
108                         finally \r
109                         {
110                                 _Lock.ReleaseReaderLock ();
111                         }
112
113                         return ret;
114                 }
115
116                 public object Get (int index)
117                 {
118                         object ret = null;
119
120                         _Lock.AcquireReaderLock (-1); 
121                         try \r
122                         {
123                                 ret = BaseGet (index);
124                         } \r
125                         finally \r
126                         {
127                                 _Lock.ReleaseReaderLock ();
128                         }
129
130                         return ret;
131                 }   
132
133                 public string GetKey (int index)
134                 {
135                         string ret = null;
136
137                         _Lock.AcquireReaderLock (-1); 
138                         try \r
139                         {
140                                 ret = BaseGetKey (index);
141                         } \r
142                         finally \r
143                         {
144                                 _Lock.ReleaseReaderLock ();
145                         }
146
147                         return ret;
148                 }      
149
150                 public void Lock ()
151                 {
152                         _Lock.AcquireWriterLock (-1);
153                 }
154
155                 public void Remove (string name)
156                 {
157                         _Lock.AcquireWriterLock (-1); 
158                         try \r
159                         {
160                                 BaseRemove (name);
161                         } \r
162                         finally \r
163                         {
164                                 _Lock.ReleaseWriterLock ();
165                         }      
166                 }
167
168                 public void RemoveAll ()
169                 {
170                         Clear ();
171                 }
172
173                 public void RemoveAt (int index)
174                 {
175                         _Lock.AcquireWriterLock (-1); 
176                         try \r
177                         {
178                                 BaseRemoveAt (index);
179                         } \r
180                         finally \r
181                         {
182                                 _Lock.ReleaseWriterLock ();
183                         }      
184                 }
185
186                 public void Set (string name, object value)
187                 {
188                         _Lock.AcquireWriterLock (-1); 
189                         try \r
190                         {
191                                 BaseSet (name, value);
192                         } \r
193                         finally \r
194                         {
195                                 _Lock.ReleaseWriterLock ();
196                         }      
197                 }   
198
199                 public void UnLock ()
200                 {
201                         _Lock.ReleaseWriterLock ();
202                 }
203
204                 public string [] AllKeys \r
205                 {
206                         get \r
207                         {
208                                 string [] ret = null;
209
210                                 _Lock.AcquireReaderLock (-1); 
211                                 try \r
212                                 {
213                                         ret = BaseGetAllKeys ();
214                                 } \r
215                                 finally \r
216                                 {
217                                         _Lock.ReleaseReaderLock ();
218                                 }     
219
220                                 return ret;
221                         }
222                 }
223
224                 public HttpApplicationState Contents \r
225                 {
226                         get { return this; }
227                 }
228
229                 public override int Count \r
230                 {
231                         get \r
232                         {
233                                 int ret = 0;
234
235                                 _Lock.AcquireReaderLock (-1); 
236                                 try \r
237                                 {
238                                         ret = base.Count;
239                                 } \r
240                                 finally \r
241                                 {
242                                         _Lock.ReleaseReaderLock ();
243                                 }     
244
245                                 return ret;
246                         }
247                 }   
248
249                 public object this [string name] \r
250                 {
251                         get { return Get (name); }
252                         set { Set (name, value); }
253                 }
254
255                 public object this [int index] \r
256                 {
257                         get { return Get (index); }
258                 }
259
260                 //  ASP Session based objects
261                 internal HttpStaticObjectsCollection SessionObjects \r
262                 {
263                         get { return _SessionObjects; }
264                 }
265
266                 //  ASP App based objects
267                 public HttpStaticObjectsCollection StaticObjects \r
268                 {
269                         get { return _AppObjects; }
270                 }
271         }
272 }
273