ConnectionManagementHandler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. using System.Collections;
  10. using System.Configuration;
  11. using System.Xml;
  12. namespace System.Net.Configuration
  13. {
  14. class ConnectionManagementData
  15. {
  16. Hashtable data; // key -> address, value -> maxconnections
  17. const int defaultMaxConnections = 2;
  18. public ConnectionManagementData (object parent)
  19. {
  20. data = new Hashtable ();
  21. if (parent != null && parent is ConnectionManagementData) {
  22. ConnectionManagementData p = (ConnectionManagementData) parent;
  23. foreach (string k in p.data.Keys)
  24. data [k] = p.data [k];
  25. }
  26. }
  27. public void Add (string address, string nconns)
  28. {
  29. if (nconns == null || nconns == "")
  30. nconns = "2";
  31. // Adding duplicates works fine under MS, so...
  32. data [address] = UInt32.Parse (nconns);
  33. }
  34. public void Remove (string address)
  35. {
  36. // Removing non-existent address is fine.
  37. data.Remove (address);
  38. }
  39. public void Clear ()
  40. {
  41. data.Clear ();
  42. }
  43. public uint GetMaxConnections (string hostOrIP)
  44. {
  45. object o = data [hostOrIP];
  46. if (o == null)
  47. o = data ["*"];
  48. if (o == null)
  49. return defaultMaxConnections;
  50. return (uint) o;
  51. }
  52. public Hashtable Data {
  53. get { return data; }
  54. }
  55. }
  56. class ConnectionManagementHandler : IConfigurationSectionHandler
  57. {
  58. public virtual object Create (object parent, object configContext, XmlNode section)
  59. {
  60. ConnectionManagementData cmd = new ConnectionManagementData (parent);
  61. if (section.Attributes != null && section.Attributes.Count != 0)
  62. 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)
  67. continue;
  68. if (ntype != XmlNodeType.Element)
  69. HandlersUtil.ThrowException ("Only elements allowed", child);
  70. string name = child.Name;
  71. if (name == "clear") {
  72. if (child.Attributes != null && child.Attributes.Count != 0)
  73. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  74. cmd.Clear ();
  75. continue;
  76. }
  77. //LAMESPEC: the MS doc says that <remove name="..."/> but they throw an exception
  78. // if you use that. "address" is correct.
  79. string address = HandlersUtil.ExtractAttributeValue ("address", child);
  80. if (name == "add") {
  81. string maxcnc = HandlersUtil.ExtractAttributeValue ("maxconnection", child, true);
  82. if (child.Attributes != null && child.Attributes.Count != 0)
  83. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  84. cmd.Add (address, maxcnc);
  85. continue;
  86. }
  87. if (name == "remove") {
  88. if (child.Attributes != null && child.Attributes.Count != 0)
  89. HandlersUtil.ThrowException ("Unrecognized attribute", child);
  90. cmd.Remove (address);
  91. continue;
  92. }
  93. HandlersUtil.ThrowException ("Unexpected element", child);
  94. }
  95. return cmd;
  96. }
  97. }
  98. internal class HandlersUtil
  99. {
  100. private HandlersUtil ()
  101. {
  102. }
  103. static internal string ExtractAttributeValue (string attKey, XmlNode node)
  104. {
  105. return ExtractAttributeValue (attKey, node, false);
  106. }
  107. static internal string ExtractAttributeValue (string attKey, XmlNode node, bool optional)
  108. {
  109. if (node.Attributes == null) {
  110. if (optional)
  111. return null;
  112. ThrowException ("Required attribute not found: " + attKey, node);
  113. }
  114. XmlNode att = node.Attributes.RemoveNamedItem (attKey);
  115. if (att == null) {
  116. if (optional)
  117. return null;
  118. ThrowException ("Required attribute not found: " + attKey, node);
  119. }
  120. string value = att.Value;
  121. if (value == String.Empty) {
  122. string opt = optional ? "Optional" : "Required";
  123. ThrowException (opt + " attribute is empty: " + attKey, node);
  124. }
  125. return value;
  126. }
  127. static internal void ThrowException (string msg, XmlNode node)
  128. {
  129. if (node != null && node.Name != String.Empty)
  130. msg = msg + " (node name: " + node.Name + ") ";
  131. throw new ConfigurationException (msg, node);
  132. }
  133. }
  134. }