ModulesConfiguration.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 IHttpModule Instance;
  39. public ModuleItem (string name, Type type)
  40. {
  41. Name = name;
  42. Type = type;
  43. }
  44. }
  45. class ModulesConfiguration
  46. {
  47. public ArrayList Modules;
  48. public ModulesConfiguration (ModulesConfiguration parent)
  49. {
  50. if (parent != null)
  51. Modules = new ArrayList (parent.Modules);
  52. else
  53. Modules = new ArrayList (8);
  54. }
  55. public void Add (string name, Type type)
  56. {
  57. Modules.Add (new ModuleItem (name, type));
  58. }
  59. public void Add (string name, string type)
  60. {
  61. Type item_type;
  62. try {
  63. item_type = Type.GetType (type, true);
  64. } catch (Exception e){
  65. throw new HttpException (
  66. String.Format ("Failed to load module `{0}' from type `{1}'", name, type));
  67. }
  68. if (!typeof (IHttpModule).IsAssignableFrom (item_type))
  69. throw new HttpException (
  70. String.Format ("The type {0} can not be assigned to IHttpHandler in the <httpModule> section",
  71. name));
  72. Add (name, item_type);
  73. }
  74. public ModuleItem Remove (string name)
  75. {
  76. int end = Modules.Count;
  77. for (int i = 0; i < end; i++) {
  78. ModuleItem item = (ModuleItem) Modules [i];
  79. if (item.Name == name){
  80. Modules.RemoveAt (i);
  81. return item;
  82. }
  83. }
  84. return null;
  85. }
  86. public void Clear ()
  87. {
  88. Modules.Clear ();
  89. }
  90. public void LoadModules (HttpApplication app)
  91. {
  92. foreach (ModuleItem item in Modules){
  93. item.Instance = (IHttpModule) Activator.CreateInstance (item.Type, true);
  94. item.Instance.Init (app);
  95. }
  96. }
  97. public void StopModules ()
  98. {
  99. foreach (ModuleItem item in Modules)
  100. item.Instance.Dispose ();
  101. }
  102. }
  103. }