2 // System.Net.Configuration.ConnectionManagementHandler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.com)
10 using System.Collections;
11 using System.Configuration;
14 namespace System.Net.Configuration
16 class ConnectionManagementData
18 Hashtable data; // key -> address, value -> maxconnections
20 public ConnectionManagementData (object parent)
22 data = new Hashtable ();
23 if (parent != null && parent is ConnectionManagementData) {
24 ConnectionManagementData p = (ConnectionManagementData) parent;
25 foreach (string k in p.data.Keys)
26 data [k] = p.data [k];
30 public void Add (string address, string nconns)
32 if (nconns == null || nconns == "")
34 // Adding duplicates works fine under MS, so...
35 data [address] = UInt32.Parse (nconns);
38 public void Remove (string address)
40 // Removing non-existent address is fine.
41 data.Remove (address);
49 public Hashtable Data {
54 class ConnectionManagementHandler : IConfigurationSectionHandler
56 public virtual object Create (object parent, object configContext, XmlNode section)
58 ConnectionManagementData cmd = new ConnectionManagementData (parent);
60 if (section.Attributes != null && section.Attributes.Count != 0)
61 HandlersUtil.ThrowException ("Unrecognized attribute", section);
63 XmlNodeList httpHandlers = section.ChildNodes;
64 foreach (XmlNode child in httpHandlers) {
65 XmlNodeType ntype = child.NodeType;
66 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
69 if (ntype != XmlNodeType.Element)
70 HandlersUtil.ThrowException ("Only elements allowed", child);
72 string name = child.Name;
73 if (name == "clear") {
74 if (child.Attributes != null && child.Attributes.Count != 0)
75 HandlersUtil.ThrowException ("Unrecognized attribute", child);
81 //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
82 // if you use that. "address" is correct.
84 string address = HandlersUtil.ExtractAttributeValue ("address", child);
86 string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
87 if (child.Attributes != null && child.Attributes.Count != 0)
88 HandlersUtil.ThrowException ("Unrecognized attribute", child);
90 cmd.Add (address, maxcnc);
94 if (name == "remove") {
95 if (child.Attributes != null && child.Attributes.Count != 0)
96 HandlersUtil.ThrowException ("Unrecognized attribute", child);
102 HandlersUtil.ThrowException ("Unexpected element", child);
109 internal class HandlersUtil
111 private HandlersUtil ()
115 static internal string ExtractAttributeValue (string attKey, XmlNode node)
117 return ExtractAttributeValue (attKey, node, false);
120 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
122 if (node.Attributes == null) {
126 ThrowException ("Required attribute not found: " + attKey, node);
129 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
133 ThrowException ("Required attribute not found: " + attKey, node);
136 string value = att.Value;
137 if (value == String.Empty) {
138 string opt = optional ? "Optional" : "Required";
139 ThrowException (opt + " attribute is empty: " + attKey, node);
145 static internal void ThrowException (string msg, XmlNode node)
147 if (node != null && node.Name != String.Empty)
148 msg = msg + " (node name: " + node.Name + ") ";
149 throw new ConfigurationException (msg, node);