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