2009-02-18 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Channels / MessageProperties.cs
1 //
2 // MessageProperties.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.ServiceModel.Channels;
33 using System.ServiceModel.Security;
34
35 using Pair = System.Collections.Generic.KeyValuePair<string, object>;
36
37 namespace System.ServiceModel.Channels
38 {
39         [MonoTODO ("it's untested")]
40         public sealed class MessageProperties : IDictionary<string, object>, 
41                 ICollection<Pair>, IEnumerable<Pair>, IEnumerable, IDisposable
42         {
43                 bool allow_output_batch;
44                 MessageEncoder encoder;
45                 Uri via;
46                 List<Pair> list;
47
48                 public MessageProperties ()
49                 {
50                         list = new List<Pair> ();
51                 }
52
53                 public MessageProperties (MessageProperties properties)
54                 {
55                         properties.CopyProperties (this);
56                 }
57
58                 [MonoTODO ("This should actually be internal of a property.")]
59                 public bool AllowOutputBatching {
60                         get { return allow_output_batch; }
61                         set { allow_output_batch = value; }
62                 }
63
64                 public int Count {
65                         get { return list.Count; }
66                 }
67
68                 [MonoTODO ("This should actually be internal of a property.")]
69                 public MessageEncoder Encoder {
70                         get { return encoder; }
71                         set { encoder = value; }
72                 }
73
74                 public bool IsFixedSize {
75                         get { return false; }
76                 }
77
78                 public bool IsReadOnly {
79                         get { return false; }
80                 }
81
82                 public ICollection<string> Keys {
83                         get { return new ParameterKeyCollection (list); }
84                 }
85
86                 public object this [string name] {
87                         get {
88                                 for (int i = 0; i < list.Count; i++)
89                                         if (list [i].Key == name)
90                                                 return list [i].Value;
91                                 return null;
92                         }
93                         set {
94                                 for (int i = 0; i < list.Count; i++)
95                                         if (list [i].Key == name) {
96                                                 list [i] = new Pair (name, value);
97                                                 return;
98                                         }
99                                 list.Add (new Pair (name, value));
100                         }
101                 }
102
103 #if !NET_2_1
104                 public SecurityMessageProperty Security {
105                         get { return (SecurityMessageProperty) this ["Security"]; }
106                         set { this ["Security"] = value; }
107                 }
108 #endif
109
110                 public ICollection<object> Values {
111                         get { return new ParameterValueCollection (list); }
112                 }
113
114                 [MonoTODO ("This should actually be internal of a property.")]
115                 public Uri Via {
116                         get { return via; }
117                         set { via = value; }
118                 }
119
120                 public void Add (string name, object property)
121                 {
122                         list.Add (new Pair (name, property));
123                 }
124
125                 public void Clear ()
126                 {
127                         list.Clear ();
128                 }
129
130                 public bool ContainsKey (string name)
131                 {
132                         for (int i = 0; i < list.Count; i++)
133                                 if (list [i].Key == name)
134                                         return true;
135                         return false;
136                 }
137
138                 public void CopyProperties (MessageProperties properties)
139                 {
140                         list = new List<Pair> (properties.list);
141                         allow_output_batch = properties.allow_output_batch;
142                         encoder = properties.encoder;
143                         via = properties.via;
144                 }
145
146                 public void Dispose ()
147                 {
148                 }
149
150                 public bool Remove (string name)
151                 {
152                         for (int i = 0; i < list.Count; i++)
153                                 if (list [i].Key == name) {
154                                         list.RemoveAt (i);
155                                         return true;
156                                 }
157                         return false;
158                 }
159
160                 public bool TryGetValue (string name, out object value)
161                 {
162                         for (int i = 0; i < list.Count; i++)
163                                 if (list [i].Key == name) {
164                                         value = list [i].Value;
165                                         return true;
166                                 }
167                         value = null;
168                         return false;
169                 }
170
171                 void ICollection<Pair>.Add (Pair pair)
172                 {
173                         list.Add (pair);
174                 }
175
176                 bool ICollection<Pair>.Contains (Pair pair)
177                 {
178                         return list.Contains (pair);
179                 }
180
181                 void ICollection<Pair>.CopyTo (Pair [] array, int index)
182                 {
183                         list.CopyTo (array, index);
184                 }
185
186                 bool ICollection<Pair>.Remove (Pair pair)
187                 {
188                         return list.Remove (pair);
189                 }
190
191                 IEnumerator<Pair> IEnumerable<Pair>.GetEnumerator ()
192                 {
193                         return list.GetEnumerator ();
194                 }
195
196                 IEnumerator IEnumerable.GetEnumerator ()
197                 {
198                         return (IEnumerator) ((IEnumerable<Pair>) this).GetEnumerator ();
199                 }
200
201                 class ParameterKeyCollection : ICollection<string>
202                 {
203                         List<Pair> source;
204
205                         public ParameterKeyCollection (List<Pair> source)
206                         {
207                                 this.source = source;
208                         }
209
210                         public int Count {
211                                 get { return source.Count; }
212                         }
213
214                         public bool IsReadOnly {
215                                 get { return true; }
216                         }
217
218                         public void Add (string item)
219                         {
220                                 throw new InvalidOperationException ();
221                         }
222
223                         public void Clear ()
224                         {
225                                 throw new InvalidOperationException ();
226                         }
227
228                         public bool Contains (string item)
229                         {
230                                 for (int i = 0; i < source.Count; i++)
231                                         if (source [i].Key == item)
232                                                 return true;
233                                 return false;
234                         }
235
236                         public void CopyTo (string [] array, int index)
237                         {
238                                 for (int i = 0; i < source.Count; i++)
239                                         array [index + i] = source [i].Key;
240                         }
241
242                         public IEnumerator<string> GetEnumerator ()
243                         {
244                                 foreach (Pair p in source)
245                                         yield return p.Key;
246                         }
247
248                         IEnumerator IEnumerable.GetEnumerator ()
249                         {
250                                 foreach (Pair p in source)
251                                         yield return p.Key;
252                         }
253
254                         public bool Remove (string item)
255                         {
256                                 throw new InvalidOperationException ();
257                         }
258                 }
259
260                 class ParameterValueCollection : ICollection<object>
261                 {
262                         List<Pair> source;
263
264                         public ParameterValueCollection (List<Pair> source)
265                         {
266                                 this.source = source;
267                         }
268
269                         public int Count {
270                                 get { return source.Count; }
271                         }
272
273                         public bool IsReadOnly {
274                                 get { return true; }
275                         }
276
277                         public void Add (object item)
278                         {
279                                 throw new InvalidOperationException ();
280                         }
281
282                         public void Clear ()
283                         {
284                                 throw new InvalidOperationException ();
285                         }
286
287                         public bool Contains (object item)
288                         {
289                                 for (int i = 0; i < source.Count; i++)
290                                         if (source [i].Value == item)
291                                                 return true;
292                                 return false;
293                         }
294
295                         public void CopyTo (object [] array, int index)
296                         {
297                                 for (int i = 0; i < source.Count; i++)
298                                         array [index + i] = source [i].Value;
299                         }
300
301                         public IEnumerator<object> GetEnumerator ()
302                         {
303                                 foreach (Pair p in source)
304                                         yield return p.Value;
305                         }
306
307                         IEnumerator IEnumerable.GetEnumerator ()
308                         {
309                                 foreach (Pair p in source)
310                                         yield return p.Key;
311                         }
312
313                         public bool Remove (object item)
314                         {
315                                 throw new InvalidOperationException ();
316                         }
317                 }
318         }
319 }