New test.
[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
29 using System.Threading;
30 using System.Collections.Specialized;
31 using System.Security.Permissions;
32
33 namespace System.Web {
34
35         // CAS - no InheritanceDemand here as the class is sealed
36         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         public sealed class HttpApplicationState : NameObjectCollectionBase \r
38         {
39                 private HttpStaticObjectsCollection _AppObjects;
40                 private HttpStaticObjectsCollection _SessionObjects;
41
42                 private ReaderWriterLock _Lock; 
43
44                 internal HttpApplicationState ()
45                 {
46                         // do not use the public (empty) ctor as it required UnmanagedCode permission
47                         _AppObjects = new HttpStaticObjectsCollection (this);
48                         _SessionObjects = new HttpStaticObjectsCollection (this);
49                         _Lock = new ReaderWriterLock ();
50                 }
51
52                 internal HttpApplicationState (HttpStaticObjectsCollection AppObj,
53                         HttpStaticObjectsCollection SessionObj)
54                 {
55                         if (null != AppObj) \r
56                         {
57                                 _AppObjects = AppObj;
58                         } \r
59                         else \r
60                         {
61                                 // do not use the public (empty) ctor as it required UnmanagedCode permission
62                                 _AppObjects = new HttpStaticObjectsCollection (this);
63                         }
64
65                         if (null != SessionObj) \r
66                         {
67                                 _SessionObjects = SessionObj;
68                         } \r
69                         else \r
70                         {
71                                 // do not use the public (empty) ctor as it required UnmanagedCode permission
72                                 _SessionObjects = new HttpStaticObjectsCollection (this);
73                         }
74                         _Lock = new ReaderWriterLock ();
75                 }
76
77                 public void Add (string name, object value)
78                 {
79                         _Lock.AcquireWriterLock (-1); 
80                         try \r
81                         {
82                                 BaseAdd (name, value);
83                         } \r
84                         finally \r
85                         {
86                                 _Lock.ReleaseWriterLock ();
87                         }
88                 }
89
90                 public void Clear ()
91                 {
92                         _Lock.AcquireWriterLock (-1); 
93                         try \r
94                         {
95                                 BaseClear ();
96                         } \r
97                         finally \r
98                         {
99                                 _Lock.ReleaseWriterLock ();
100                         }
101                 } 
102
103                 public object Get (string name)
104                 {
105                         object ret = null;
106
107                         _Lock.AcquireReaderLock (-1); 
108                         try \r
109                         {
110                                 ret = BaseGet (name);
111                         } \r
112                         finally \r
113                         {
114                                 _Lock.ReleaseReaderLock ();
115                         }
116
117                         return ret;
118                 }
119
120                 public object Get (int index)
121                 {
122                         object ret = null;
123
124                         _Lock.AcquireReaderLock (-1); 
125                         try \r
126                         {
127                                 ret = BaseGet (index);
128                         } \r
129                         finally \r
130                         {
131                                 _Lock.ReleaseReaderLock ();
132                         }
133
134                         return ret;
135                 }   
136
137                 public string GetKey (int index)
138                 {
139                         string ret = null;
140
141                         _Lock.AcquireReaderLock (-1); 
142                         try \r
143                         {
144                                 ret = BaseGetKey (index);
145                         } \r
146                         finally \r
147                         {
148                                 _Lock.ReleaseReaderLock ();
149                         }
150
151                         return ret;
152                 }      
153
154                 public void Lock ()
155                 {
156                         _Lock.AcquireWriterLock (-1);
157                 }
158
159                 public void Remove (string name)
160                 {
161                         _Lock.AcquireWriterLock (-1); 
162                         try \r
163                         {
164                                 BaseRemove (name);
165                         } \r
166                         finally \r
167                         {
168                                 _Lock.ReleaseWriterLock ();
169                         }      
170                 }
171
172                 public void RemoveAll ()
173                 {
174                         Clear ();
175                 }
176
177                 public void RemoveAt (int index)
178                 {
179                         _Lock.AcquireWriterLock (-1); 
180                         try \r
181                         {
182                                 BaseRemoveAt (index);
183                         } \r
184                         finally \r
185                         {
186                                 _Lock.ReleaseWriterLock ();
187                         }      
188                 }
189
190                 public void Set (string name, object value)
191                 {
192                         _Lock.AcquireWriterLock (-1); 
193                         try \r
194                         {
195                                 BaseSet (name, value);
196                         } \r
197                         finally \r
198                         {
199                                 _Lock.ReleaseWriterLock ();
200                         }      
201                 }   
202
203                 public void UnLock ()
204                 {
205                         _Lock.ReleaseWriterLock ();
206                 }
207
208                 public string [] AllKeys \r
209                 {
210                         get \r
211                         {
212                                 string [] ret = null;
213
214                                 _Lock.AcquireReaderLock (-1); 
215                                 try \r
216                                 {
217                                         ret = BaseGetAllKeys ();
218                                 } \r
219                                 finally \r
220                                 {
221                                         _Lock.ReleaseReaderLock ();
222                                 }     
223
224                                 return ret;
225                         }
226                 }
227
228                 public HttpApplicationState Contents \r
229                 {
230                         get { return this; }
231                 }
232
233                 public override int Count \r
234                 {
235                         get \r
236                         {
237                                 int ret = 0;
238
239                                 _Lock.AcquireReaderLock (-1); 
240                                 try \r
241                                 {
242                                         ret = base.Count;
243                                 } \r
244                                 finally \r
245                                 {
246                                         _Lock.ReleaseReaderLock ();
247                                 }     
248
249                                 return ret;
250                         }
251                 }   
252
253                 public object this [string name] \r
254                 {
255                         get { return Get (name); }
256                         set { Set (name, value); }
257                 }
258
259                 public object this [int index] \r
260                 {
261                         get { return Get (index); }
262                 }
263
264                 //  ASP Session based objects
265                 internal HttpStaticObjectsCollection SessionObjects \r
266                 {
267                         get { return _SessionObjects; }
268                 }
269
270                 //  ASP App based objects
271                 public HttpStaticObjectsCollection StaticObjects \r
272                 {
273                         get { return _AppObjects; }
274                 }
275         }
276 }
277