ModulesConfiguration.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // System.Web.Configuration.ModulesConfiguration
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  9. // (C) 2005 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. namespace System.Web.Configuration
  34. {
  35. class ModuleItem {
  36. public string Name;
  37. public Type Type;
  38. public ModuleItem (string name, Type type)
  39. {
  40. Name = name;
  41. Type = type;
  42. }
  43. }
  44. class ModulesConfiguration
  45. {
  46. ArrayList Modules;
  47. private ModulesConfiguration ()
  48. {
  49. }
  50. public ModulesConfiguration (ModulesConfiguration parent)
  51. {
  52. if (parent != null)
  53. Modules = new ArrayList (parent.Modules);
  54. else
  55. Modules = new ArrayList (8);
  56. }
  57. public void Add (string name, Type type)
  58. {
  59. Modules.Add (new ModuleItem (name, type));
  60. }
  61. public void Add (string name, string type)
  62. {
  63. Type item_type;
  64. try {
  65. item_type = Type.GetType (type, true);
  66. } catch (Exception e){
  67. throw new HttpException (
  68. String.Format ("Failed to load module `{0}' from type `{1}'", name, type));
  69. }
  70. if (!typeof (IHttpModule).IsAssignableFrom (item_type))
  71. throw new HttpException (
  72. String.Format ("The type {0} can not be assigned to IHttpHandler in the <httpModule> section",
  73. name));
  74. Add (name, item_type);
  75. }
  76. public ModuleItem Remove (string name)
  77. {
  78. int end = Modules.Count;
  79. for (int i = 0; i < end; i++) {
  80. ModuleItem item = (ModuleItem) Modules [i];
  81. if (item.Name == name){
  82. Modules.RemoveAt (i);
  83. return item;
  84. }
  85. }
  86. return null;
  87. }
  88. public void Clear ()
  89. {
  90. Modules.Clear ();
  91. }
  92. public HttpModuleCollection LoadModules (HttpApplication app)
  93. {
  94. HttpModuleCollection coll = new HttpModuleCollection ();
  95. foreach (ModuleItem item in Modules){
  96. IHttpModule module = (IHttpModule) Activator.CreateInstance (item.Type, true);
  97. module.Init (app);
  98. coll.AddModule (item.Name, module);
  99. }
  100. return coll;
  101. }
  102. }
  103. }