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;
36 using XmlNode = System.Object;
39 #pragma warning disable 618
41 namespace System.Net.Configuration
43 class ConnectionManagementData
45 Hashtable data; // key -> address, value -> maxconnections
46 const int defaultMaxConnections = 2;
48 public ConnectionManagementData (object parent)
50 data = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
51 CaseInsensitiveComparer.DefaultInvariant);
52 if (parent != null && parent is ConnectionManagementData) {
53 ConnectionManagementData p = (ConnectionManagementData) parent;
54 foreach (string k in p.data.Keys)
55 data [k] = p.data [k];
59 public void Add (string address, string nconns)
61 if (nconns == null || nconns == "")
63 // Adding duplicates works fine under MS, so...
64 data [address] = UInt32.Parse (nconns);
67 public void Add (string address, int nconns)
69 data [address] = (uint) nconns;
72 public void Remove (string address)
74 // Removing non-existent address is fine.
75 data.Remove (address);
83 public uint GetMaxConnections (string hostOrIP)
85 object o = data [hostOrIP];
90 return defaultMaxConnections;
95 public Hashtable Data {
100 class ConnectionManagementHandler : IConfigurationSectionHandler
102 public virtual object Create (object parent, object configContext, XmlNode section)
104 ConnectionManagementData cmd = new ConnectionManagementData (parent);
106 if (section.Attributes != null && section.Attributes.Count != 0)
107 HandlersUtil.ThrowException ("Unrecognized attribute", section);
109 XmlNodeList httpHandlers = section.ChildNodes;
110 foreach (XmlNode child in httpHandlers) {
111 XmlNodeType ntype = child.NodeType;
112 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
115 if (ntype != XmlNodeType.Element)
116 HandlersUtil.ThrowException ("Only elements allowed", child);
118 string name = child.Name;
119 if (name == "clear") {
120 if (child.Attributes != null && child.Attributes.Count != 0)
121 HandlersUtil.ThrowException ("Unrecognized attribute", child);
127 //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
128 // if you use that. "address" is correct.
130 string address = HandlersUtil.ExtractAttributeValue ("address", child);
132 string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
133 if (child.Attributes != null && child.Attributes.Count != 0)
134 HandlersUtil.ThrowException ("Unrecognized attribute", child);
136 cmd.Add (address, maxcnc);
140 if (name == "remove") {
141 if (child.Attributes != null && child.Attributes.Count != 0)
142 HandlersUtil.ThrowException ("Unrecognized attribute", child);
144 cmd.Remove (address);
148 HandlersUtil.ThrowException ("Unexpected element", child);
156 internal class HandlersUtil
158 private HandlersUtil ()
162 static internal string ExtractAttributeValue (string attKey, XmlNode node)
164 return ExtractAttributeValue (attKey, node, false);
167 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
169 if (node.Attributes == null) {
173 ThrowException ("Required attribute not found: " + attKey, node);
176 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
180 ThrowException ("Required attribute not found: " + attKey, node);
183 string value = att.Value;
184 if (value == String.Empty) {
185 string opt = optional ? "Optional" : "Required";
186 ThrowException (opt + " attribute is empty: " + attKey, node);
192 static internal void ThrowException (string msg, XmlNode node)
194 if (node != null && node.Name != String.Empty)
195 msg = msg + " (node name: " + node.Name + ") ";
196 throw new ConfigurationException (msg, node);