Implemented PipeSecurity. GetAccessControl, SetAccessControl, and ACL-containing...
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Hosting / AbstractAttributeMap.cs
1 //\r
2 // Mainsoft.Web.Hosting.AbstractAttributeMap.cs\r
3 //\r
4 // Authors:\r
5 //      Konstantin Triger <kostat@mainsoft.com>\r
6 //\r
7 // (C) 2008 Mainsoft Co. (http://www.mainsoft.com)\r
8 //\r
9 \r
10 //\r
11 // Permission is hereby granted, free of charge, to any person obtaining\r
12 // a copy of this software and associated documentation files (the\r
13 // "Software"), to deal in the Software without restriction, including\r
14 // without limitation the rights to use, copy, modify, merge, publish,\r
15 // distribute, sublicense, and/or sell copies of the Software, and to\r
16 // permit persons to whom the Software is furnished to do so, subject to\r
17 // the following conditions:\r
18 // \r
19 // The above copyright notice and this permission notice shall be\r
20 // included in all copies or substantial portions of the Software.\r
21 // \r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
29 //\r
30 \r
31 using java.util;\r
32 using System;\r
33 \r
34 namespace Mainsoft.Web.Hosting\r
35 {\r
36         partial class BaseExternalContext\r
37         {\r
38                 public abstract class AbstractAttributeMap : AbstractMap\r
39                 {\r
40                         private Set _keySet;\r
41                         private Collection _values;\r
42                         private Set _entrySet;\r
43 \r
44                         public override void clear () {\r
45                                 List names = new ArrayList ();\r
46                                 for (Enumeration e = getAttributeNames (); e.hasMoreElements (); ) {\r
47                                         names.add (e.nextElement ());\r
48                                 }\r
49 \r
50                                 for (Iterator it = names.iterator (); it.hasNext (); ) {\r
51                                         removeAttribute ((String) it.next ());\r
52                                 }\r
53                         }\r
54 \r
55                         public override bool containsKey (Object key) {\r
56                                 return getAttribute (key.ToString ()) != null;\r
57                         }\r
58 \r
59                         public override bool containsValue (Object findValue) {\r
60                                 if (findValue == null) {\r
61                                         return false;\r
62                                 }\r
63 \r
64                                 for (Enumeration e = getAttributeNames (); e.hasMoreElements (); ) {\r
65                                         Object value = getAttribute ((String) e.nextElement ());\r
66                                         if (findValue.Equals (value)) {\r
67                                                 return true;\r
68                                         }\r
69                                 }\r
70 \r
71                                 return false;\r
72                         }\r
73 \r
74                         public override Set entrySet () {\r
75                                 return (_entrySet != null) ? _entrySet : (_entrySet = new EntrySet (this));\r
76                         }\r
77 \r
78                         public override Object get (Object key) {\r
79                                 return getAttribute (key.ToString ());\r
80                         }\r
81 \r
82                         public override bool isEmpty () {\r
83                                 return !getAttributeNames ().hasMoreElements ();\r
84                         }\r
85 \r
86                         public override Set keySet () {\r
87                                 return (_keySet != null) ? _keySet : (_keySet = new KeySet (this));\r
88                         }\r
89 \r
90                         public override Object put (Object key, Object value) {\r
91                                 String key_ = key.ToString ();\r
92                                 Object retval = getAttribute (key_);\r
93                                 setAttribute (key_, value);\r
94                                 return retval;\r
95                         }\r
96 \r
97                         public override void putAll (Map t) {\r
98                                 for (Iterator it = t.entrySet ().iterator (); it.hasNext (); ) {\r
99                                         Map.Entry entry = (Map.Entry) it.next ();\r
100                                         setAttribute (entry.getKey ().ToString (), entry.getValue ());\r
101                                 }\r
102                         }\r
103 \r
104                         public override Object remove (Object key) {\r
105                                 String key_ = key.ToString ();\r
106                                 Object retval = getAttribute (key_);\r
107                                 removeAttribute (key_);\r
108                                 return retval;\r
109                         }\r
110 \r
111                         public override int size () {\r
112                                 int size = 0;\r
113                                 for (Enumeration e = getAttributeNames (); e.hasMoreElements (); ) {\r
114                                         size++;\r
115                                         e.nextElement ();\r
116                                 }\r
117                                 return size;\r
118                         }\r
119 \r
120                         public override Collection values () {\r
121                                 return (_values != null) ? _values : (_values = new Values (this));\r
122                         }\r
123 \r
124 \r
125                         abstract protected Object getAttribute (string key);\r
126 \r
127                         abstract protected void setAttribute (string key, Object value);\r
128 \r
129                         abstract protected void removeAttribute (string key);\r
130 \r
131                         abstract protected Enumeration getAttributeNames ();\r
132 \r
133 \r
134                         private class KeySet : AbstractSet\r
135                         {\r
136                                 protected readonly AbstractAttributeMap _owner;\r
137                                 public KeySet (AbstractAttributeMap owner) {\r
138                                         _owner = owner;\r
139                                 }\r
140 \r
141                                 public override Iterator iterator () {\r
142                                         return new KeyIterator (_owner);\r
143                                 }\r
144 \r
145                                 public override bool isEmpty () {\r
146                                         return _owner.isEmpty ();\r
147                                 }\r
148 \r
149                                 public override int size () {\r
150                                         return _owner.size ();\r
151                                 }\r
152 \r
153                                 public override bool contains (Object o) {\r
154                                         return _owner.containsKey (o);\r
155                                 }\r
156 \r
157                                 public override bool remove (Object o) {\r
158                                         return _owner.remove (o) != null;\r
159                                 }\r
160 \r
161                                 public override void clear () {\r
162                                         _owner.clear ();\r
163                                 }\r
164                         }\r
165 \r
166                         private class KeyIterator : Iterator\r
167                         {\r
168                                 protected readonly AbstractAttributeMap _owner;\r
169                                 protected readonly Enumeration _e;\r
170 \r
171                                 public KeyIterator (AbstractAttributeMap owner) {\r
172                                         _owner = owner;\r
173                                         _e = _owner.getAttributeNames ();\r
174                                 }\r
175 \r
176                                 protected Object _currentKey;\r
177 \r
178                                 public virtual void remove () {\r
179                                         // remove() may cause ConcurrentModificationException.\r
180                                         // We could throw an exception here, but not throwing an exception\r
181                                         //   allows one call to remove() to succeed\r
182                                         if (_currentKey == null) {\r
183                                                 throw new NoSuchElementException (\r
184                                                         "You must call next() at least once");\r
185                                         }\r
186                                         _owner.remove (_currentKey);\r
187                                 }\r
188 \r
189                                 public bool hasNext () {\r
190                                         return _e.hasMoreElements ();\r
191                                 }\r
192 \r
193                                 public virtual Object next () {\r
194                                         return _currentKey = _e.nextElement ();\r
195                                 }\r
196                         }\r
197 \r
198                         private class Values : KeySet\r
199                         {\r
200 \r
201                                 public Values (AbstractAttributeMap owner)\r
202                                         : base (owner) {\r
203                                 }\r
204 \r
205                                 public override Iterator iterator () {\r
206                                         return new ValuesIterator (_owner);\r
207                                 }\r
208 \r
209                                 public override bool contains (Object o) {\r
210                                         return _owner.containsValue (o);\r
211                                 }\r
212 \r
213                                 public override bool remove (Object o) {\r
214                                         if (o == null) {\r
215                                                 return false;\r
216                                         }\r
217 \r
218                                         for (Iterator it = iterator (); it.hasNext (); ) {\r
219                                                 if (o.Equals (it.next ())) {\r
220                                                         it.remove ();\r
221                                                         return true;\r
222                                                 }\r
223                                         }\r
224 \r
225                                         return false;\r
226                                 }\r
227                         }\r
228 \r
229                         private class ValuesIterator : KeyIterator\r
230                         {\r
231                                 public ValuesIterator (AbstractAttributeMap owner)\r
232                                         : base (owner) { }\r
233                                 public override Object next () {\r
234                                         base.next ();\r
235                                         return _owner.get (_currentKey);\r
236                                 }\r
237                         }\r
238 \r
239                         private class EntrySet : KeySet\r
240                         {\r
241                                 public EntrySet (AbstractAttributeMap owner) : base (owner) { }\r
242 \r
243                                 public override Iterator iterator () {\r
244                                         return new EntryIterator (_owner);\r
245                                 }\r
246 \r
247                                 public override bool contains (Object o) {\r
248                                         if (!(o is Map.Entry)) {\r
249                                                 return false;\r
250                                         }\r
251 \r
252                                         Map.Entry entry = (Map.Entry) o;\r
253                                         Object key = entry.getKey ();\r
254                                         Object value = entry.getValue ();\r
255                                         if (key == null || value == null) {\r
256                                                 return false;\r
257                                         }\r
258 \r
259                                         return value.Equals (_owner.get (key));\r
260                                 }\r
261 \r
262                                 public override bool remove (Object o) {\r
263                                         if (!(o is Map.Entry)) {\r
264                                                 return false;\r
265                                         }\r
266 \r
267                                         Map.Entry entry = (Map.Entry) o;\r
268                                         Object key = entry.getKey ();\r
269                                         Object value = entry.getValue ();\r
270                                         if (key == null || value == null\r
271                                                 || !value.Equals (_owner.get (key))) {\r
272                                                 return false;\r
273                                         }\r
274 \r
275                                         return _owner.remove (((Map.Entry) o).getKey ()) != null;\r
276                                 }\r
277                         }\r
278 \r
279                         /**\r
280                          * Not very efficient since it generates a new instance of <code>Entry</code>\r
281                          * for each element and still internaly uses the <code>KeyIterator</code>.\r
282                          * It is more efficient to use the <code>KeyIterator</code> directly.\r
283                          */\r
284                         private class EntryIterator : KeyIterator\r
285                         {\r
286                                 public EntryIterator (AbstractAttributeMap owner) : base (owner) { }\r
287                                 public override Object next () {\r
288                                         base.next ();\r
289                                         // Must create new Entry every time--value of the entry must stay\r
290                                         // linked to the same attribute name\r
291                                         return new EntrySetEntry (_currentKey);\r
292                                 }\r
293                         }\r
294 \r
295                         private class EntrySetEntry : Map.Entry\r
296                         {\r
297                                 readonly AbstractAttributeMap _owner;\r
298 \r
299                                 public EntrySetEntry (AbstractAttributeMap owner) {\r
300                                         _owner = owner;\r
301                                 }\r
302 \r
303                                 private readonly Object _currentKey;\r
304 \r
305                                 public EntrySetEntry (Object currentKey) {\r
306                                         _currentKey = currentKey;\r
307                                 }\r
308 \r
309                                 public Object getKey () {\r
310                                         return _currentKey;\r
311                                 }\r
312 \r
313                                 public Object getValue () {\r
314                                         return _owner.get (_currentKey);\r
315                                 }\r
316 \r
317                                 public Object setValue (Object value) {\r
318                                         return _owner.put (_currentKey, value);\r
319                                 }\r
320 \r
321                                 public int hashCode () {\r
322                                         return _currentKey == null ? 0 : _currentKey.GetHashCode ();\r
323                                 }\r
324 \r
325                                 public bool equals (Object obj) {\r
326                                         if (!(obj is EntrySetEntry))\r
327                                                 return false;\r
328                                         return _currentKey != null && _currentKey.Equals (obj);\r
329                                 }\r
330                         }\r
331                 }\r
332         }\r
333 }