2 // System.Net.Configuration.ConnectionManagementHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
32 using System.Configuration;
37 namespace System.Net.Configuration
39 class ConnectionManagementData
41 Hashtable data; // key -> address, value -> maxconnections
42 const int defaultMaxConnections = 2;
44 public ConnectionManagementData (object parent)
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];
55 public void Add (string address, string nconns)
57 if (nconns == null || nconns == "")
59 // Adding duplicates works fine under MS, so...
60 data [address] = UInt32.Parse (nconns);
63 public void Remove (string address)
65 // Removing non-existent address is fine.
66 data.Remove (address);
74 public uint GetMaxConnections (string hostOrIP)
76 object o = data [hostOrIP];
81 return defaultMaxConnections;
86 public Hashtable Data {
91 class ConnectionManagementHandler : IConfigurationSectionHandler
94 public virtual object Create (object parent, object configContext, XmlNode section)
96 ConnectionManagementData cmd = new ConnectionManagementData (parent);
98 if (section.Attributes != null && section.Attributes.Count != 0)
99 HandlersUtil.ThrowException ("Unrecognized attribute", section);
101 XmlNodeList httpHandlers = section.ChildNodes;
102 foreach (XmlNode child in httpHandlers) {
103 XmlNodeType ntype = child.NodeType;
104 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
107 if (ntype != XmlNodeType.Element)
108 HandlersUtil.ThrowException ("Only elements allowed", child);
110 string name = child.Name;
111 if (name == "clear") {
112 if (child.Attributes != null && child.Attributes.Count != 0)
113 HandlersUtil.ThrowException ("Unrecognized attribute", child);
119 //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
120 // if you use that. "address" is correct.
122 string address = HandlersUtil.ExtractAttributeValue ("address", child);
124 string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
125 if (child.Attributes != null && child.Attributes.Count != 0)
126 HandlersUtil.ThrowException ("Unrecognized attribute", child);
128 cmd.Add (address, maxcnc);
132 if (name == "remove") {
133 if (child.Attributes != null && child.Attributes.Count != 0)
134 HandlersUtil.ThrowException ("Unrecognized attribute", child);
136 cmd.Remove (address);
140 HandlersUtil.ThrowException ("Unexpected element", child);
148 internal class HandlersUtil
150 private HandlersUtil ()
154 static internal string ExtractAttributeValue (string attKey, XmlNode node)
156 return ExtractAttributeValue (attKey, node, false);
159 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
161 if (node.Attributes == null) {
165 ThrowException ("Required attribute not found: " + attKey, node);
168 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
172 ThrowException ("Required attribute not found: " + attKey, node);
175 string value = att.Value;
176 if (value == String.Empty) {
177 string opt = optional ? "Optional" : "Required";
178 ThrowException (opt + " attribute is empty: " + attKey, node);
184 static internal void ThrowException (string msg, XmlNode node)
186 if (node != null && node.Name != String.Empty)
187 msg = msg + " (node name: " + node.Name + ") ";
188 throw new ConfigurationException (msg, node);