Merge pull request #744 from echampet/bug-10001
[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.Collections.Generic;
25 using System.IO;
26 using System.Security.Permissions;
27 using System.Web.UI;
28
29 namespace System.Web
30 {
31         // CAS - no InheritanceDemand here as the class is sealed
32         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
33         public sealed class HttpStaticObjectsCollection : ICollection, IEnumerable
34         {
35                 sealed class StaticItem {
36                         object this_lock = new object();
37                         
38                         Type type;
39                         object instance;
40                         
41                         public StaticItem (Type type)
42                         {
43                                 this.type = type;
44                         }
45
46                         public StaticItem (StaticItem item)
47                         {
48                                 this.type = item.type;
49                         }
50                         
51                         public object Instance {
52                                 get {
53                                         lock (this_lock) {
54                                                 if (instance == null)
55                                                         instance = Activator.CreateInstance (type);
56                                         }
57
58                                         return instance;
59                                 }
60                         }
61                 }
62
63                 Dictionary <string, object> objects;
64
65                 Dictionary <string, object> Objects {
66                         get {
67                                 if (objects == null)
68                                         objects = new Dictionary <string, object> (StringComparer.Ordinal);
69
70                                 return objects;
71                         }
72                 }
73                 
74                 // Needs to hold object items that can be latebound and can be serialized
75                 public HttpStaticObjectsCollection ()
76                 {
77                 }
78
79                 // this ctor has no security requirements and is used when creating HttpApplicationState
80                 internal HttpStaticObjectsCollection (HttpApplicationState appstate)
81                 {
82                 }
83
84                 public object GetObject (string name)
85                 {
86                         return this [name];
87                 }
88
89                 public IEnumerator GetEnumerator ()
90                 {
91                         return Objects.GetEnumerator ();
92                 }
93
94                 public void CopyTo (Array array, int index)
95                 {
96                         if (objects == null)
97                                 return;
98
99                         // Copied from Hashtable.CopyTo for the most part
100                         if (array == null)
101                                 throw new ArgumentNullException ("array");
102
103                         if (index < 0)
104                                 throw new ArgumentOutOfRangeException ("index");
105
106                         if (array.Rank > 1)
107                                 throw new ArgumentException ("array is multidimensional");
108
109                         if ((array.Length > 0) && (index >= array.Length))
110                                 throw new ArgumentException ("index is equal to or greater than array.Length");
111
112                         if (index + objects.Count > array.Length)
113                                 throw new ArgumentException ("Not enough room from index to end of array for this collection");
114
115                         // We need to emulate Hashtable here, which uses DictionaryEntry for its items
116                         foreach (var de in objects)
117                                 array.SetValue (new DictionaryEntry (de.Key, de.Value), index++);
118                 }   
119
120                 internal IDictionary GetObjects ()
121                 {
122                         return Objects;
123                 }
124
125                 public object this [string name] {
126                         get {
127                                 if (objects == null)
128                                         return null;
129                                 
130                                 StaticItem item = null;
131                                 object o;
132                                 if (Objects.TryGetValue (name, out o))
133                                         item = o as StaticItem;
134                                 
135                                 if (item == null)
136                                         return null;
137                                 
138                                 return item.Instance;
139                         }
140                 }
141
142                 public int Count {
143                         get {
144                                 if (objects == null)
145                                         return 0;
146                                 
147                                 return Objects.Count;
148                         }
149                 }
150
151                 public bool IsReadOnly {
152                         get { return true; }
153                 }
154
155                 public bool IsSynchronized {
156                         get { return false; }
157                 }
158
159                 [MonoTODO ("Not implemented")]
160                 public bool NeverAccessed {
161                         get { throw new NotImplementedException (); }
162                 }
163
164                 public object SyncRoot {
165                         get { return this; }
166                 }
167
168                 internal HttpStaticObjectsCollection Clone ()
169                 {
170                         HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
171                         if (objects == null)
172                                 return coll;
173                         
174                         var collObjects = coll.Objects;
175                         foreach (var de in objects) {
176                                 StaticItem item = new StaticItem ((StaticItem) de.Value);
177                                 collObjects [de.Key] = item;
178                         }
179                         
180                         return coll;
181                 }
182
183                 internal void Add (ObjectTagBuilder tag)
184                 {
185                         Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
186                 }
187                 
188                 void Set (string name, object obj)
189                 {
190                         Objects [name] = obj;
191                 }
192
193                 public void Serialize (BinaryWriter writer)
194                 {
195                         if (objects == null) {
196                                 writer.Write (0);
197                                 return;
198                         }
199
200                         writer.Write (objects.Count);
201                         foreach (var de in objects) {
202                                 writer.Write (de.Key);
203                                 System.Web.Util.AltSerialization.Serialize (writer, de.Value);
204                         }
205                 }
206
207                 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
208                 {
209                         HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
210                         for (int i = reader.ReadInt32 (); i > 0; i--)
211                                 result.Set (reader.ReadString (), System.Web.Util.AltSerialization.Deserialize (reader));
212
213                         return result;
214                 }
215
216                 internal byte [] ToByteArray ()
217                 {
218                         MemoryStream stream = null;
219                         try {
220                                 stream = new MemoryStream ();
221                                 Serialize (new BinaryWriter (stream));
222                                 return stream.GetBuffer ();
223                         } catch {
224                                 throw;
225                         } finally {
226                                 if (stream != null)
227                                         stream.Close ();
228                         }
229                 }
230
231                 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
232                 {
233                         HttpStaticObjectsCollection objs = null;
234                         MemoryStream stream = null;
235                         try {
236                                 stream = new MemoryStream (data);
237                                 objs = Deserialize (new BinaryReader (stream));
238                         } catch {
239                                 throw;
240                         } finally {
241                                 if (stream != null)
242                                         stream.Close ();
243                         }
244                         return objs;
245                 }
246         }
247 }
248