2004-09-11 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System / System.Net.Configuration / ConnectionManagementHandler.cs
1 //
2 // System.Net.Configuration.ConnectionManagementHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Collections;
32 using System.Configuration;
33 #if (XML_DEP)
34 using System.Xml;
35 #endif
36
37 namespace System.Net.Configuration
38 {
39         class ConnectionManagementData
40         {
41                 Hashtable data; // key -> address, value -> maxconnections
42                 const int defaultMaxConnections = 2;
43                 
44                 public ConnectionManagementData (object parent)
45                 {
46                         data = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
47                                               CaseInsensitiveComparer.Default);
48                         if (parent != null && parent is ConnectionManagementData) {
49                                 ConnectionManagementData p = (ConnectionManagementData) parent;
50                                 foreach (string k in p.data.Keys)
51                                         data [k] = p.data [k];  
52                         }
53                 }
54
55                 public void Add (string address, string nconns)
56                 {
57                         if (nconns == null || nconns == "")
58                                 nconns = "2";
59                         // Adding duplicates works fine under MS, so...
60                         data [address] = UInt32.Parse (nconns);
61                 }
62
63                 public void Remove (string address)
64                 {
65                         // Removing non-existent address is fine.
66                         data.Remove (address);
67                 }
68
69                 public void Clear ()
70                 {
71                         data.Clear ();
72                 }
73
74                 public uint GetMaxConnections (string hostOrIP)
75                 {
76                         object o = data [hostOrIP];
77                         if (o == null)
78                                 o = data ["*"];
79
80                         if (o == null)
81                                 return defaultMaxConnections;
82
83                         return (uint) o;
84                 }
85
86                 public Hashtable Data {
87                         get { return data; }
88                 }
89         }
90
91         class ConnectionManagementHandler : IConfigurationSectionHandler
92         {
93 #if (XML_DEP)
94                 public virtual object Create (object parent, object configContext, XmlNode section)
95                 {
96                         ConnectionManagementData cmd = new ConnectionManagementData (parent);
97                         
98                         if (section.Attributes != null && section.Attributes.Count != 0)
99                                 HandlersUtil.ThrowException ("Unrecognized attribute", section);
100
101                         XmlNodeList httpHandlers = section.ChildNodes;
102                         foreach (XmlNode child in httpHandlers) {
103                                 XmlNodeType ntype = child.NodeType;
104                                 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
105                                         continue;
106
107                                 if (ntype != XmlNodeType.Element)
108                                         HandlersUtil.ThrowException ("Only elements allowed", child);
109                                 
110                                 string name = child.Name;
111                                 if (name == "clear") {
112                                         if (child.Attributes != null && child.Attributes.Count != 0)
113                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
114
115                                         cmd.Clear ();
116                                         continue;
117                                 }
118
119                                 //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
120                                 // if you use that. "address" is correct.
121
122                                 string address = HandlersUtil.ExtractAttributeValue ("address", child);
123                                 if (name == "add") {
124                                         string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
125                                         if (child.Attributes != null && child.Attributes.Count != 0)
126                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
127
128                                         cmd.Add (address, maxcnc);
129                                         continue;
130                                 }
131
132                                 if (name == "remove") {
133                                         if (child.Attributes != null && child.Attributes.Count != 0)
134                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
135
136                                         cmd.Remove (address);
137                                         continue;
138                                 }
139
140                                 HandlersUtil.ThrowException ("Unexpected element", child);
141                         }
142
143                         return cmd;
144                 }
145 #endif
146         }
147
148         internal class HandlersUtil
149         {
150                 private HandlersUtil ()
151                 {
152                 }
153 #if (XML_DEP)
154                 static internal string ExtractAttributeValue (string attKey, XmlNode node)
155                 {
156                         return ExtractAttributeValue (attKey, node, false);
157                 }
158                         
159                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
160                 {
161                         if (node.Attributes == null) {
162                                 if (optional)
163                                         return null;
164
165                                 ThrowException ("Required attribute not found: " + attKey, node);
166                         }
167
168                         XmlNode att = node.Attributes.RemoveNamedItem (attKey);
169                         if (att == null) {
170                                 if (optional)
171                                         return null;
172                                 ThrowException ("Required attribute not found: " + attKey, node);
173                         }
174
175                         string value = att.Value;
176                         if (value == String.Empty) {
177                                 string opt = optional ? "Optional" : "Required";
178                                 ThrowException (opt + " attribute is empty: " + attKey, node);
179                         }
180
181                         return value;
182                 }
183
184                 static internal void ThrowException (string msg, XmlNode node)
185                 {
186                         if (node != null && node.Name != String.Empty)
187                                 msg = msg + " (node name: " + node.Name + ") ";
188                         throw new ConfigurationException (msg, node);
189                 }
190 #endif
191         }
192 }
193