2
0

ConnectionManagementHandler.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // System.Net.Configuration.ConnectionManagementHandler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.Configuration;
  31. #if (XML_DEP)
  32. using System.Xml;
  33. #endif
  34. namespace System.Net.Configuration
  35. {
  36. class ConnectionManagementData
  37. {
  38. Hashtable data; // key -> address, value -> maxconnections
  39. const int defaultMaxConnections = 2;
  40. public ConnectionManagementData (object parent)
  41. {
  42. data = new Hashtable (CaseInsensitiveHashCodeProvider.Default,
  43. CaseInsensitiveComparer.Default);
  44. if (parent != null && parent is ConnectionManagementData) {
  45. ConnectionManagementData p = (ConnectionManagementData) parent;
  46. foreach (string k in p.data.Keys)
  47. data [k] = p.data [k];
  48. }
  49. }
  50. public void Add (string address, string nconns)
  51. {
  52. if (nconns == null || nconns == "")
  53. nconns = "2";
  54. // Adding duplicates works fine under MS, so...
  55. data [address] = UInt32.Parse (nconns);
  56. }
  57. public void Remove (string address)
  58. {
  59. // Removing non-existent address is fine.
  60. data.Remove (address);
  61. }
  62. public void Clear ()
  63. {
  64. data.Clear ();
  65. }
  66. public uint GetMaxConnections (string hostOrIP)
  67. {
  68. object o = data [hostOrIP];
  69. if (o == null)
  70. o = data ["*"];
  71. if (o == null)
  72. return defaultMaxConnections;
  73. return (uint) o;
  74. }
  75. public Hashtable Data {
  76. get { return data; }
  77. }
  78. }
  79. class ConnectionManagementHandler : IConfigurationSectionHandler
  80. {
  81. #if (XML_DEP)
  82. public virtual object Create (object parent, object configContext, XmlNode section)
  83. {
  84. ConnectionManagementData cmd = new ConnectionManagementData (parent);
  85. if (section.Attributes != null && section.Attributes.Count != 0)
  86. HandlersUtil.ThrowException ("Unrecognized attribute", section);
  87. XmlNodeList httpHandlers = section.ChildNodes;
  88. foreach (XmlNode child in httpHandlers) {
  89. XmlNodeType ntype = child.NodeType;
  90. if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
  91. continue;
  92. if (ntype != XmlNodeType.Element)
  93. HandlersUtil.ThrowException ("Only elements allowed", child);
  94. string name = child.Name;
  95. if (name == "clear") {
  96. if (child.Attributes != null && child.Attributes.Count != 0)
  97. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  98. cmd.Clear ();
  99. continue;
  100. }
  101. //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
  102. // if you use that. "address" is correct.
  103. string address = HandlersUtil.ExtractAttributeValue ("address", child);
  104. if (name == "add") {
  105. string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
  106. if (child.Attributes != null && child.Attributes.Count != 0)
  107. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  108. cmd.Add (address, maxcnc);
  109. continue;
  110. }
  111. if (name == "remove") {
  112. if (child.Attributes != null && child.Attributes.Count != 0)
  113. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  114. cmd.Remove (address);
  115. continue;
  116. }
  117. HandlersUtil.ThrowException ("Unexpected element", child);
  118. }
  119. return cmd;
  120. }
  121. #endif
  122. }
  123. internal class HandlersUtil
  124. {
  125. private HandlersUtil ()
  126. {
  127. }
  128. #if (XML_DEP)
  129. static internal string ExtractAttributeValue (string attKey, XmlNode node)
  130. {
  131. return ExtractAttributeValue (attKey, node, false);
  132. }
  133. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
  134. {
  135. if (node.Attributes == null) {
  136. if (optional)
  137. return null;
  138. ThrowException ("Required attribute not found: " + attKey, node);
  139. }
  140. XmlNode att = node.Attributes.RemoveNamedItem (attKey);
  141. if (att == null) {
  142. if (optional)
  143. return null;
  144. ThrowException ("Required attribute not found: " + attKey, node);
  145. }
  146. string value = att.Value;
  147. if (value == String.Empty) {
  148. string opt = optional ? "Optional" : "Required";
  149. ThrowException (opt + " attribute is empty: " + attKey, node);
  150. }
  151. return value;
  152. }
  153. static internal void ThrowException (string msg, XmlNode node)
  154. {
  155. if (node != null && node.Name != String.Empty)
  156. msg = msg + " (node name: " + node.Name + ") ";
  157. throw new ConfigurationException (msg, node);
  158. }
  159. #endif
  160. }
  161. }