move FrameworkName from corlib to System
[mcs.git] / class / System / System.Net.Configuration / ConnectionManagementHandler.cs
blob42a0d5afb7583832643a0cadd4e791a2d6f0c458
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 //
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.
31 using System.Collections;
32 using System.Configuration;
33 #if (XML_DEP)
34 using System.Xml;
35 #else
36 using XmlNode = System.Object;
37 #endif
39 namespace System.Net.Configuration
41 class ConnectionManagementData
43 Hashtable data; // key -> address, value -> maxconnections
44 const int defaultMaxConnections = 2;
46 public ConnectionManagementData (object parent)
48 data = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
49 CaseInsensitiveComparer.DefaultInvariant);
50 if (parent != null && parent is ConnectionManagementData) {
51 ConnectionManagementData p = (ConnectionManagementData) parent;
52 foreach (string k in p.data.Keys)
53 data [k] = p.data [k];
57 public void Add (string address, string nconns)
59 if (nconns == null || nconns == "")
60 nconns = "2";
61 // Adding duplicates works fine under MS, so...
62 data [address] = UInt32.Parse (nconns);
65 public void Add (string address, int nconns)
67 data [address] = (uint) nconns;
70 public void Remove (string address)
72 // Removing non-existent address is fine.
73 data.Remove (address);
76 public void Clear ()
78 data.Clear ();
81 public uint GetMaxConnections (string hostOrIP)
83 object o = data [hostOrIP];
84 if (o == null)
85 o = data ["*"];
87 if (o == null)
88 return defaultMaxConnections;
90 return (uint) o;
93 public Hashtable Data {
94 get { return data; }
98 class ConnectionManagementHandler : IConfigurationSectionHandler
100 public virtual object Create (object parent, object configContext, XmlNode section)
102 ConnectionManagementData cmd = new ConnectionManagementData (parent);
103 #if (XML_DEP)
104 if (section.Attributes != null && section.Attributes.Count != 0)
105 HandlersUtil.ThrowException ("Unrecognized attribute", section);
107 XmlNodeList httpHandlers = section.ChildNodes;
108 foreach (XmlNode child in httpHandlers) {
109 XmlNodeType ntype = child.NodeType;
110 if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
111 continue;
113 if (ntype != XmlNodeType.Element)
114 HandlersUtil.ThrowException ("Only elements allowed", child);
116 string name = child.Name;
117 if (name == "clear") {
118 if (child.Attributes != null && child.Attributes.Count != 0)
119 HandlersUtil.ThrowException ("Unrecognized attribute", child);
121 cmd.Clear ();
122 continue;
125 //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
126 // if you use that. "address" is correct.
128 string address = HandlersUtil.ExtractAttributeValue ("address", child);
129 if (name == "add") {
130 string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
131 if (child.Attributes != null && child.Attributes.Count != 0)
132 HandlersUtil.ThrowException ("Unrecognized attribute", child);
134 cmd.Add (address, maxcnc);
135 continue;
138 if (name == "remove") {
139 if (child.Attributes != null && child.Attributes.Count != 0)
140 HandlersUtil.ThrowException ("Unrecognized attribute", child);
142 cmd.Remove (address);
143 continue;
146 HandlersUtil.ThrowException ("Unexpected element", child);
148 #endif
150 return cmd;
154 internal class HandlersUtil
156 private HandlersUtil ()
159 #if (XML_DEP)
160 static internal string ExtractAttributeValue (string attKey, XmlNode node)
162 return ExtractAttributeValue (attKey, node, false);
165 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
167 if (node.Attributes == null) {
168 if (optional)
169 return null;
171 ThrowException ("Required attribute not found: " + attKey, node);
174 XmlNode att = node.Attributes.RemoveNamedItem (attKey);
175 if (att == null) {
176 if (optional)
177 return null;
178 ThrowException ("Required attribute not found: " + attKey, node);
181 string value = att.Value;
182 if (value == String.Empty) {
183 string opt = optional ? "Optional" : "Required";
184 ThrowException (opt + " attribute is empty: " + attKey, node);
187 return value;
190 static internal void ThrowException (string msg, XmlNode node)
192 if (node != null && node.Name != String.Empty)
193 msg = msg + " (node name: " + node.Name + ") ";
194 throw new ConfigurationException (msg, node);
196 #endif