Merge pull request #3622 from rolfbjarne/remove-stray-file
[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 
38         {
39                 HttpStaticObjectsCollection _AppObjects;
40                 HttpStaticObjectsCollection _SessionObjects;
41
42                 ReaderWriterLockSlim _Lock; 
43
44                 internal HttpApplicationState ()
45                 {
46                         _Lock = new ReaderWriterLockSlim ();
47                 }
48
49                 internal HttpApplicationState (HttpStaticObjectsCollection AppObj, HttpStaticObjectsCollection SessionObj)
50                 {
51                         _AppObjects = AppObj;
52                         _SessionObjects = SessionObj;
53                         _Lock = new ReaderWriterLockSlim ();
54                 }
55
56                 bool IsLockHeld {
57                         get { return _Lock.IsReadLockHeld || _Lock.IsWriteLockHeld; }
58                 }
59                 
60                 public void Add (string name, object value)
61                 {
62                         bool acquired = false;
63                         try {
64                                 if (!IsLockHeld) {
65                                         _Lock.EnterWriteLock ();
66                                         acquired = true;
67                                 }
68                                 BaseAdd (name, value);
69                         } finally {
70                                 if (acquired && IsLockHeld)
71                                         _Lock.ExitWriteLock ();
72                         }
73                 }
74
75                 public void Clear ()
76                 {
77                         bool acquired = false;
78                         try {
79                                 if (!IsLockHeld) {
80                                         _Lock.EnterWriteLock ();
81                                         acquired = true;
82                                 }
83                                 BaseClear ();
84                         } finally {
85                                 if (acquired && IsLockHeld)
86                                         _Lock.ExitWriteLock ();
87                         }
88                 } 
89
90                 public object Get (string name)
91                 {
92                         object ret = null;
93                         bool acquired = false;
94                         try {
95                                 if (!IsLockHeld) {
96                                         _Lock.EnterReadLock ();
97                                         acquired = true;
98                                 }
99                                 ret = BaseGet (name);
100                         }  finally {
101                                 if (acquired && IsLockHeld)
102                                         _Lock.ExitReadLock ();
103                         }
104
105                         return ret;
106                 }
107
108                 public object Get (int index)
109                 {
110                         bool acquired = false;
111                         try {
112                                 if (!IsLockHeld) {
113                                         _Lock.EnterReadLock ();
114                                         acquired = true;
115                                 }
116                                 return BaseGet (index);
117                         } finally {
118                                 if (acquired && IsLockHeld)
119                                         _Lock.ExitReadLock ();
120                         }
121                 }   
122
123                 public string GetKey (int index)
124                 {
125                         bool acquired = false;
126                         try {
127                                 if (!IsLockHeld) {
128                                         _Lock.EnterReadLock ();
129                                         acquired = true;
130                                 }
131                                 return BaseGetKey (index);
132                         } finally {
133                                 if (acquired && IsLockHeld)
134                                         _Lock.ExitReadLock ();
135                         }
136                 }      
137
138                 public void Lock ()
139                 {
140                         if (!_Lock.IsWriteLockHeld)
141                                 _Lock.EnterWriteLock ();
142                 }
143
144                 public void Remove (string name)
145                 {
146                         bool acquired = false;
147                         try {
148                                 if (!IsLockHeld) {
149                                         _Lock.EnterWriteLock ();
150                                         acquired = true;
151                                 }
152                                 BaseRemove (name);
153                         } finally  {
154                                 if (acquired && IsLockHeld)
155                                         _Lock.ExitWriteLock ();
156                         }      
157                 }
158
159                 public void RemoveAll ()
160                 {
161                         Clear ();
162                 }
163
164                 public void RemoveAt (int index)
165                 {
166                         bool acquired = false;
167                         try {
168                                 if (!IsLockHeld) {
169                                         _Lock.EnterWriteLock ();
170                                         acquired = true;
171                                 }
172                                 BaseRemoveAt (index);
173                         } finally  {
174                                 if (acquired && IsLockHeld)
175                                         _Lock.ExitWriteLock ();
176                         }      
177                 }
178
179                 public void Set (string name, object value)
180                 {
181                         bool acquired = false;
182                         try {
183                                 if (!IsLockHeld) {
184                                         _Lock.EnterWriteLock ();
185                                         acquired = true;
186                                 }
187                                 BaseSet (name, value);
188                         } finally  {
189                                 if (acquired && IsLockHeld)
190                                         _Lock.ExitWriteLock ();
191                         }      
192                 }   
193
194                 public void UnLock ()
195                 {
196                         if (_Lock.IsWriteLockHeld)
197                                 _Lock.ExitWriteLock ();
198                 }
199
200                 public string [] AllKeys {
201                         get {
202                                 bool acquired = false;
203                                 try {
204                                         if (!IsLockHeld) {
205                                                 _Lock.EnterReadLock ();
206                                                 acquired = true;
207                                         }
208                                         return BaseGetAllKeys ();
209                                 } finally  {
210                                         if (acquired && IsLockHeld)
211                                                 _Lock.ExitReadLock ();
212                                 }
213                         }
214                 }
215
216                 public HttpApplicationState Contents {
217                         get { return this; }
218                 }
219
220                 public override int Count {
221                         get {
222                                 bool acquired = false;
223                                 try {
224                                         if (!IsLockHeld) {
225                                                 _Lock.EnterReadLock ();
226                                                 acquired = true;
227                                         }
228                                         return base.Count;
229                                 } finally  {
230                                         if (acquired && IsLockHeld)
231                                                 _Lock.ExitReadLock ();
232                                 }     
233                         }
234                 }   
235
236                 public object this [string name] {
237                         get { return Get (name); }
238                         set { Set (name, value); }
239                 }
240
241                 public object this [int index] {
242                         get { return Get (index); }
243                 }
244
245                 //  ASP Session based objects
246                 internal HttpStaticObjectsCollection SessionObjects {
247                         get {
248                                 if (_SessionObjects == null)
249                                         _SessionObjects = new HttpStaticObjectsCollection ();
250                                 
251                                 return _SessionObjects;
252                         }
253                 }
254
255                 //  ASP App based objects
256                 public HttpStaticObjectsCollection StaticObjects {
257                         get {
258                                 if (_AppObjects == null)
259                                         _AppObjects = new HttpStaticObjectsCollection ();
260                                 
261                                 return _AppObjects;
262                         }
263                 }
264         }
265 }
266