Facilitate the merge
[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         {
34                 Hashtable _Objects;
35
36                 sealed class StaticItem {
37                         object this_lock = new object();
38                         
39                         Type type;
40                         object instance;
41                         
42                         public StaticItem (Type type)
43                         {
44                                 this.type = type;
45                         }
46
47                         public StaticItem (StaticItem item)
48                         {
49                                 this.type = item.type;
50                         }
51                         
52                         public object Instance {
53                                 get {
54                                         lock (this_lock) {
55                                                 if (instance == null)
56                                                         instance = Activator.CreateInstance (type);
57                                         }
58
59                                         return instance;
60                                 }
61                         }
62                 }
63
64                 // Needs to hold object items that can be latebound and can be serialized
65 #if ONLY_1_1
66                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
67 #endif
68                 public HttpStaticObjectsCollection ()
69                 {
70                         _Objects = new Hashtable ();
71                 }
72
73                 // this ctor has no security requirements and is used when creating HttpApplicationState
74                 internal HttpStaticObjectsCollection (HttpApplicationState appstate)
75                 {
76                         _Objects = new Hashtable ();
77                 }
78
79                 public object GetObject (string name)
80                 {
81                         return this [name];
82                 }
83
84                 public IEnumerator GetEnumerator ()
85                 {
86                         return _Objects.GetEnumerator ();
87                 }
88
89                 public void CopyTo (Array array, int index)
90                 {
91                         _Objects.CopyTo (array, index);
92                 }   
93
94                 internal IDictionary GetObjects ()
95                 {
96                         return _Objects;
97                 }
98
99                 public object this [string name] {
100                         get {
101                                 StaticItem item = _Objects [name] as StaticItem;
102                                 if (item == null)
103                                         return null;
104                                 
105                                 return item.Instance;
106                         }
107                 }
108
109                 public int Count {
110                         get { return _Objects.Count; }
111                 }
112
113                 public bool IsReadOnly {
114                         get { return true; }
115                 }
116
117                 public bool IsSynchronized {
118                         get { return false; }
119                 }
120
121                 [MonoTODO ("Not implemented")]
122                 public bool NeverAccessed {
123                         get { throw new NotImplementedException (); }
124                 }
125
126                 public object SyncRoot {
127                         get { return this; }
128                 }
129
130                 internal HttpStaticObjectsCollection Clone ()
131                 {
132                         HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
133                         coll._Objects = new Hashtable ();
134                         foreach (string key in _Objects.Keys) {
135                                 StaticItem item = new StaticItem ((StaticItem) _Objects [key]);
136                                 coll._Objects [key] = item;
137                         }
138                         
139                         return coll;
140                 }
141
142                 internal void Add (ObjectTagBuilder tag)
143                 {
144                         _Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
145                 }
146                 
147                 void Set (string name, object obj)
148                 {
149                         _Objects [name] = obj;
150                 }
151
152                 public void Serialize (BinaryWriter writer)
153                 {
154                         writer.Write (_Objects.Count);
155                         foreach (string key in _Objects.Keys) {
156                                 writer.Write (key);
157                                 System.Web.Util.AltSerialization.Serialize (writer, _Objects [key]);
158                         }
159                 }
160
161                 public static HttpStaticObjectsCollection Deserialize (BinaryReader reader)
162                 {
163                         HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
164                         for (int i = reader.ReadInt32 (); i > 0; i--)
165                                 result.Set (reader.ReadString (),
166                                         System.Web.Util.AltSerialization.Deserialize (reader));
167
168                         return result;
169                 }
170
171                 internal byte [] ToByteArray ()
172                 {
173                         MemoryStream stream = null;
174                         try {
175                                 stream = new MemoryStream ();
176                                 Serialize (new BinaryWriter (stream));
177                                 return stream.GetBuffer ();
178                         } catch {
179                                 throw;
180                         } finally {
181                                 if (stream != null)
182                                         stream.Close ();
183                         }
184                 }
185
186                 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
187                 {
188                         HttpStaticObjectsCollection objs = null;
189                         MemoryStream stream = null;
190                         try {
191                                 stream = new MemoryStream (data);
192                                 objs = Deserialize (new BinaryReader (stream));
193                         } catch {
194                                 throw;
195                         } finally {
196                                 if (stream != null)
197                                         stream.Close ();
198                         }
199                         return objs;
200                 }
201         }
202 }
203