Use lower-case names for Windows headers.
[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 #else
36 using XmlNode = System.Object;
37 #endif
38
39 namespace System.Net.Configuration
40 {
41         class ConnectionManagementData
42         {
43                 Hashtable data; // key -> address, value -> maxconnections
44                 const int defaultMaxConnections = 2;
45                 
46                 public ConnectionManagementData (object parent)
47                 {
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];  
54                         }
55                 }
56
57                 public void Add (string address, string nconns)
58                 {
59                         if (nconns == null || nconns == "")
60                                 nconns = "2";
61                         // Adding duplicates works fine under MS, so...
62                         data [address] = UInt32.Parse (nconns);
63                 }
64
65                 public void Add (string address, int nconns)
66                 {
67                         data [address] = (uint) nconns;
68                 }
69
70                 public void Remove (string address)
71                 {
72                         // Removing non-existent address is fine.
73                         data.Remove (address);
74                 }
75
76                 public void Clear ()
77                 {
78                         data.Clear ();
79                 }
80
81                 public uint GetMaxConnections (string hostOrIP)
82                 {
83                         object o = data [hostOrIP];
84                         if (o == null)
85                                 o = data ["*"];
86
87                         if (o == null)
88                                 return defaultMaxConnections;
89
90                         return (uint) o;
91                 }
92
93                 public Hashtable Data {
94                         get { return data; }
95                 }
96         }
97
98         class ConnectionManagementHandler : IConfigurationSectionHandler
99         {
100                 public virtual object Create (object parent, object configContext, XmlNode section)
101                 {
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);
106
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;
112
113                                 if (ntype != XmlNodeType.Element)
114                                         HandlersUtil.ThrowException ("Only elements allowed", child);
115                                 
116                                 string name = child.Name;
117                                 if (name == "clear") {
118                                         if (child.Attributes != null && child.Attributes.Count != 0)
119                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
120
121                                         cmd.Clear ();
122                                         continue;
123                                 }
124
125                                 //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
126                                 // if you use that. "address" is correct.
127
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);
133
134                                         cmd.Add (address, maxcnc);
135                                         continue;
136                                 }
137
138                                 if (name == "remove") {
139                                         if (child.Attributes != null && child.Attributes.Count != 0)
140                                                 HandlersUtil.ThrowException ("Unrecognized attribute", child);
141
142                                         cmd.Remove (address);
143                                         continue;
144                                 }
145
146                                 HandlersUtil.ThrowException ("Unexpected element", child);
147                         }
148 #endif                  
149
150                         return cmd;
151                 }
152         }
153
154         internal class HandlersUtil
155         {
156                 private HandlersUtil ()
157                 {
158                 }
159 #if (XML_DEP)
160                 static internal string ExtractAttributeValue (string attKey, XmlNode node)
161                 {
162                         return ExtractAttributeValue (attKey, node, false);
163                 }
164                         
165                 static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
166                 {
167                         if (node.Attributes == null) {
168                                 if (optional)
169                                         return null;
170
171                                 ThrowException ("Required attribute not found: " + attKey, node);
172                         }
173
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);
179                         }
180
181                         string value = att.Value;
182                         if (value == String.Empty) {
183                                 string opt = optional ? "Optional" : "Required";
184                                 ThrowException (opt + " attribute is empty: " + attKey, node);
185                         }
186
187                         return value;
188                 }
189
190                 static internal void ThrowException (string msg, XmlNode node)
191                 {
192                         if (node != null && node.Name != String.Empty)
193                                 msg = msg + " (node name: " + node.Name + ") ";
194                         throw new ConfigurationException (msg, node);
195                 }
196 #endif
197         }
198 }
199