Fix typo in MergeCollections (); refactoring
[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                         writer.Write (_Objects.Count);
158                         foreach (string key in _Objects.Keys) {
159                                 writer.Write (key);
160                                 System.Web.Util.AltSerialization.Serialize (writer, _Objects [key]);
161                         }
162                 }
163
164 #if NET_2_0
165                 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
166 #else
167                 internal static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
168 #endif
169                 {
170                         HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
171                         for (int i = reader.ReadInt32 (); i > 0; i--)
172                                 result.Set (reader.ReadString (),
173                                         System.Web.Util.AltSerialization.Deserialize (reader));
174
175                         return result;
176                 }
177
178                 internal byte [] ToByteArray ()
179                 {
180                         MemoryStream stream = null;
181                         try {
182                                 stream = new MemoryStream ();
183                                 Serialize (new BinaryWriter (stream));
184                                 return stream.GetBuffer ();
185                         } catch {
186                                 throw;
187                         } finally {
188                                 if (stream != null)
189                                         stream.Close ();
190                         }
191                 }
192
193                 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
194                 {
195                         HttpStaticObjectsCollection objs = null;
196                         MemoryStream stream = null;
197                         try {
198                                 stream = new MemoryStream (data);
199                                 objs = Deserialize (new BinaryReader (stream));
200                         } catch {
201                                 throw;
202                         } finally {
203                                 if (stream != null)
204                                         stream.Close ();
205                         }
206                         return objs;
207                 }
208         }
209 }
210