New test.
[mono.git] / mcs / class / System.Web / System.Web / HttpStaticObjectsCollection.cs
1
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 using System.Collections;
24 using System.IO;
25 using System.Security.Permissions;
26 using System.Web.UI;
27
28 namespace System.Web {
29
30         // CAS - no InheritanceDemand here as the class is sealed
31         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
32         public sealed class HttpStaticObjectsCollection : ICollection, IEnumerable {
33                 private Hashtable _Objects;
34
35                 class StaticItem {
36                         Type type;
37                         object instance;
38
39                         public StaticItem (Type type)
40                         {
41                                 this.type = type;
42                         }
43
44                         public StaticItem (StaticItem item)
45                         {
46                                 this.type = item.type;
47                         }
48                         
49                         public object Instance {
50                                 get {
51                                         lock (this) {
52                                                 if (instance == null)
53                                                         instance = Activator.CreateInstance (type);
54                                         }
55
56                                         return instance;
57                                 }
58                         }
59                 }
60
61                 // Needs to hold object items that can be latebound and can be serialized
62 #if ONLY_1_1
63                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
64 #endif
65                 public HttpStaticObjectsCollection ()
66                 {
67                         _Objects = new Hashtable ();
68                 }
69
70                 // this ctor has no security requirements and is used when creating HttpApplicationState
71                 internal HttpStaticObjectsCollection (HttpApplicationState appstate)
72                 {
73                         _Objects = new Hashtable ();
74                 }
75
76                 public object GetObject (string name)
77                 {
78                         return this [name];
79                 }
80
81                 public IEnumerator GetEnumerator ()
82                 {
83                         return _Objects.GetEnumerator ();
84                 }
85
86                 public void CopyTo (Array array, int index)
87                 {
88                         _Objects.CopyTo (array, index);
89                 }   
90
91                 internal IDictionary GetObjects ()
92                 {
93                         return _Objects;
94                 }
95
96                 public object this [string name] {
97                         get {
98                                 StaticItem item = _Objects [name] as StaticItem;
99                                 if (item == null)
100                                         return null;
101                                 
102                                 return item.Instance;
103                         }
104                 }
105
106                 public int Count {
107                         get { return _Objects.Count; }
108                 }
109
110                 public bool IsReadOnly {
111                         get { return true; }
112                 }
113
114                 public bool IsSynchronized {
115                         get { return false; }
116                 }
117
118 #if NET_2_0
119                 [MonoTODO ("Not implemented")]
120                 public bool NeverAccessed {
121                         get { throw new NotImplementedException (); }
122                 }
123 #endif
124
125                 public object SyncRoot {
126                         get { return this; }
127                 }
128
129                 internal HttpStaticObjectsCollection Clone ()
130                 {
131                         HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
132                         coll._Objects = new Hashtable ();
133                         foreach (string key in _Objects.Keys) {
134                                 StaticItem item = new StaticItem ((StaticItem) _Objects [key]);
135                                 coll._Objects [key] = item;
136                         }
137                         
138                         return coll;
139                 }
140
141                 internal void Add (ObjectTagBuilder tag)
142                 {
143                         _Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
144                 }
145                 
146                 private void Set (string name, object obj)
147                 {
148                         _Objects [name] = obj;
149                 }
150
151 #if NET_2_0
152                 public void Serialize (BinaryWriter writer)
153 #else
154                 internal void Serialize (BinaryWriter writer)
155 #endif
156                 {
157                         lock (_Objects) {
158                                 writer.Write (Count);
159                                 foreach (string key in _Objects.Keys) {
160                                         writer.Write (key);
161                                         object value = _Objects [key];
162                                         if (value == null) {
163                                                 writer.Write (System.Web.Util.AltSerialization.NullIndex);
164                                                 continue;
165                                         }
166
167                                         System.Web.Util.AltSerialization.SerializeByType (writer, value);
168                                 }
169                         }
170                 }
171
172 #if NET_2_0
173                 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
174 #else
175                 internal static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
176 #endif
177                 {
178                         HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
179                         for (int i = reader.ReadInt32 (); i > 0; i--) {
180                                 string key = reader.ReadString ();
181                                 int index = reader.ReadInt32 ();
182                                 if (index == System.Web.Util.AltSerialization.NullIndex)
183                                         result.Set (key, null);
184                                 else
185                                         result.Set (key, System.Web.Util.AltSerialization.DeserializeFromIndex (index, reader));
186                         }
187
188                         return result;
189                 }
190
191                 internal byte [] ToByteArray ()
192                 {
193                         MemoryStream stream = null;
194                         try {
195                                 stream = new MemoryStream ();
196                                 Serialize (new BinaryWriter (stream));
197                                 return stream.GetBuffer ();
198                         } catch {
199                                 throw;
200                         } finally {
201                                 if (stream != null)
202                                         stream.Close ();
203                         }
204                 }
205
206                 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
207                 {
208                         HttpStaticObjectsCollection objs = null;
209                         MemoryStream stream = null;
210                         try {
211                                 stream = new MemoryStream (data);
212                                 objs = Deserialize (new BinaryReader (stream));
213                         } catch {
214                                 throw;
215                         } finally {
216                                 if (stream != null)
217                                         stream.Close ();
218                         }
219                         return objs;
220                 }
221         }
222 }
223