ConnectionManagementHandler.cs 3.9 KB

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