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 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
== "")
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
);
81 public uint GetMaxConnections (string hostOrIP
)
83 object o
= data
[hostOrIP
];
88 return defaultMaxConnections
;
93 public Hashtable Data
{
98 class ConnectionManagementHandler
: IConfigurationSectionHandler
100 public virtual object Create (object parent
, object configContext
, XmlNode section
)
102 ConnectionManagementData cmd
= new ConnectionManagementData (parent
);
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
)
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
);
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
);
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
);
138 if (name
== "remove") {
139 if (child
.Attributes
!= null && child
.Attributes
.Count
!= 0)
140 HandlersUtil
.ThrowException ("Unrecognized attribute", child
);
142 cmd
.Remove (address
);
146 HandlersUtil
.ThrowException ("Unexpected element", child
);
154 internal class HandlersUtil
156 private HandlersUtil ()
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) {
171 ThrowException ("Required attribute not found: " + attKey
, node
);
174 XmlNode att
= node
.Attributes
.RemoveNamedItem (attKey
);
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
);
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
);