2004-02-17 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpApplicationState.cs
1 // 
2 // System.Web.HttpApplicationState
3 //
4 // Author:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //
7 using System;
8 using System.Threading;
9 using System.Web;
10 using System.Collections.Specialized;
11
12 namespace System.Web {
13
14         [MonoTODO("Performance - Use SWMR lock here")]
15         public sealed class HttpApplicationState : NameObjectCollectionBase {
16                 private HttpStaticObjectsCollection _AppObjects;
17                 private HttpStaticObjectsCollection _SessionObjects;
18
19                 // TODO : Change to ReadWriteLock when ready
20                 private Mutex _Lock; 
21
22                 private void LockRead ()
23                 {
24                         Monitor.Enter (this);
25                 }
26
27                 private void LockWrite ()
28                 {
29                         Monitor.Enter (this);
30                 }
31
32                 private void UnlockRead ()
33                 {
34                         Monitor.Exit (this);
35                 }
36
37                 private void UnlockWrite ()
38                 {
39                         Monitor.Exit (this);
40                 }
41
42                 internal HttpApplicationState ()
43                 {
44                         _AppObjects = new HttpStaticObjectsCollection ();
45                         _SessionObjects = new HttpStaticObjectsCollection ();
46                         _Lock = new Mutex ();
47                 }
48
49                 internal HttpApplicationState (HttpStaticObjectsCollection AppObj,
50                                                 HttpStaticObjectsCollection SessionObj)
51                 {
52                         if (null != AppObj) {
53                                 _AppObjects = AppObj;
54                         } else {
55                                 _AppObjects = new HttpStaticObjectsCollection ();
56                         }
57
58                         if (null != SessionObj) {
59                                 _SessionObjects = SessionObj;
60                         } else {
61                                 _SessionObjects = new HttpStaticObjectsCollection ();
62                         }
63                         _Lock = new Mutex ();
64                 }
65
66                 public void Add (string name, object value)
67                 {
68                         LockWrite (); 
69                         try {
70                                 BaseAdd (name, value);
71                         } finally {
72                                 UnlockWrite ();
73                         }
74                 }
75
76                 public void Clear ()
77                 {
78                         LockWrite (); 
79                         try {
80                                 BaseClear ();
81                         } finally {
82                                 UnlockWrite ();
83                         }
84                 } 
85
86                 public object Get (string name)
87                 {
88                         object ret = null;
89
90                         LockRead (); 
91                         try {
92                                 ret = BaseGet (name);
93                         } finally {
94                                 UnlockRead ();
95                         }
96
97                         return ret;
98                 }
99
100                 public object Get (int index)
101                 {
102                         object ret = null;
103
104                         LockRead (); 
105                         try {
106                                 ret = BaseGet (index);
107                         } finally {
108                                 UnlockRead ();
109                         }
110
111                         return ret;
112                 }   
113
114                 public string GetKey (int index)
115                 {
116                         string ret = null;
117
118                         LockRead (); 
119                         try {
120                                 ret = BaseGetKey (index);
121                         } finally {
122                                 UnlockRead ();
123                         }
124
125                         return ret;
126                 }      
127
128                 public void Lock ()
129                 {
130                         LockWrite ();
131                 }
132
133                 public void Remove (string name)
134                 {
135                         LockWrite (); 
136                         try {
137                                 BaseRemove (name);
138                         } finally {
139                                 UnlockWrite ();
140                         }      
141                 }
142
143                 public void RemoveAll ()
144                 {
145                         Clear ();
146                 }
147
148                 public void RemoveAt (int index)
149                 {
150                         LockWrite (); 
151                         try {
152                                 BaseRemoveAt (index);
153                         } finally {
154                                 UnlockWrite ();
155                         }      
156                 }
157
158                 public void Set (string name, object value)
159                 {
160                         LockWrite (); 
161                         try {
162                                 BaseSet (name, value);
163                         } finally {
164                                 UnlockWrite ();
165                         }      
166                 }   
167
168                 public void UnLock ()
169                 {
170                         UnlockWrite ();
171                 }
172
173                 public string [] AllKeys {
174                         get {
175                                 string [] ret = null;
176
177                                 LockRead (); 
178                                 try {
179                                         ret = BaseGetAllKeys ();
180                                 } finally {
181                                         UnlockRead ();
182                                 }     
183
184                                 return ret;
185                         }
186                 }
187
188                 public HttpApplicationState Contents {
189                         get { return this; }
190                 }
191
192                 public override int Count {
193                         get {
194                                 int ret = 0;
195
196                                 LockRead(); 
197                                 try {
198                                         ret = base.Count;
199                                 } finally {
200                                         UnlockRead ();
201                                 }     
202
203                                 return ret;
204                         }
205                 }   
206
207                 public object this [string name] {
208                         get { return Get (name); }
209                         set { Set (name, value); }
210                 }
211
212                 public object this [int index] {
213                         get { return Get (index); }
214                 }
215
216                 //  ASP Session based objects
217                 internal HttpStaticObjectsCollection SessionObjects {
218                         get { return _SessionObjects; }
219                 }
220
221                 //  ASP App based objects
222                 public HttpStaticObjectsCollection StaticObjects {
223                         get { return _AppObjects; }
224                 }
225         }
226 }
227