2004-09-09 Gonzalo Paniagua Javier <gonzalo@ximian.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 using System;
23 using System.Collections;
24 using System.IO;
25 using System.Web.UI;
26
27 namespace System.Web {
28         public sealed class HttpStaticObjectsCollection : ICollection, IEnumerable {
29                 private Hashtable _Objects;
30
31                 class StaticItem {
32                         Type type;
33                         object instance;
34
35                         public StaticItem (Type type)
36                         {
37                                 this.type = type;
38                         }
39
40                         public StaticItem (StaticItem item)
41                         {
42                                 this.type = item.type;
43                         }
44                         
45                         public object Instance {
46                                 get {
47                                         lock (this) {
48                                                 if (instance == null)
49                                                         instance = Activator.CreateInstance (type);
50                                         }
51
52                                         return instance;
53                                 }
54                         }
55                 }
56
57                 // Needs to hold object items that can be latebound and can be serialized
58                 public HttpStaticObjectsCollection ()
59                 {
60                         _Objects = new Hashtable();
61                 }
62
63                 public object GetObject (string name)
64                 {
65                         return this [name];
66                 }
67
68                 public IEnumerator GetEnumerator ()
69                 {
70                         return _Objects.GetEnumerator ();
71                 }
72
73                 public void CopyTo (Array array, int index)
74                 {
75                         _Objects.CopyTo (array, index);
76                 }   
77
78                 internal IDictionary GetObjects ()
79                 {
80                         return _Objects;
81                 }
82
83                 public object this [string name] {
84                         get {
85                                 StaticItem item = _Objects [name] as StaticItem;
86                                 if (item == null)
87                                         return null;
88                                 
89                                 return item.Instance;
90                         }
91                 }
92
93                 public int Count {
94                         get { return _Objects.Count; }
95                 }
96
97                 public bool IsReadOnly {
98                         get { return true; }
99                 }
100
101                 public bool IsSynchronized {
102                         get { return false; }
103                 }
104
105                 public object SyncRoot {
106                         get { return this; }
107                 }
108
109                 internal HttpStaticObjectsCollection Clone ()
110                 {
111                         HttpStaticObjectsCollection coll = new HttpStaticObjectsCollection ();
112                         coll._Objects = new Hashtable ();
113                         foreach (string key in _Objects.Keys) {
114                                 StaticItem item = new StaticItem ((StaticItem) _Objects [key]);
115                                 coll._Objects [key] = item;
116                         }
117                         
118                         return coll;
119                 }
120
121                 internal void Add (ObjectTagBuilder tag)
122                 {
123                         _Objects.Add (tag.ObjectID, new StaticItem (tag.Type));
124                 }
125                 
126                 private void Set (string name, object obj)
127                 {
128                         _Objects [name] = obj;
129                 }
130
131                 internal void Serialize (BinaryWriter w)
132                 {
133                         lock (_Objects) {
134                                 w.Write (Count);
135                                 foreach (string key in _Objects.Keys) {
136                                         w.Write (key);
137                                         object value = _Objects [key];
138                                         if (value == null) {
139                                                 w.Write (System.Web.Util.AltSerialization.NullIndex);
140                                                 continue;
141                                         }
142
143                                         System.Web.Util.AltSerialization.SerializeByType (w, value);
144                                 }
145                         }
146                 }
147
148                 internal static HttpStaticObjectsCollection Deserialize (BinaryReader r)
149                 {
150                         HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
151                         for (int i = r.ReadInt32 (); i > 0; i--) {
152                                 string key = r.ReadString ();
153                                 int index = r.ReadInt32 ();
154                                 if (index == System.Web.Util.AltSerialization.NullIndex)
155                                         result.Set (key, null);
156                                 else
157                                         result.Set (key, System.Web.Util.AltSerialization.DeserializeFromIndex (index, r));
158                         }
159
160                         return result;
161                 }
162
163                 internal byte [] ToByteArray ()
164                 {
165                         MemoryStream stream = null;
166                         try {
167                                 stream = new MemoryStream ();
168                                 Serialize (new BinaryWriter (stream));
169                                 return stream.GetBuffer ();
170                         } catch {
171                                 throw;
172                         } finally {
173                                 if (stream != null)
174                                         stream.Close ();
175                         }
176                 }
177
178                 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
179                 {
180                         HttpStaticObjectsCollection objs = null;
181                         MemoryStream stream = null;
182                         try {
183                                 stream = new MemoryStream (data);
184                                 objs = Deserialize (new BinaryReader (stream));
185                         } catch {
186                                 throw;
187                         } finally {
188                                 if (stream != null)
189                                         stream.Close ();
190                         }
191                         return objs;
192                 }
193         }
194 }
195