HandlerFactoryConfiguration.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // System.Web.Configuration.HandlerFactoryConfiguration
  3. //
  4. // Authors:
  5. // Patrik Torstensson ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections;
  31. namespace System.Web.Configuration
  32. {
  33. class HandlerFactoryConfiguration
  34. {
  35. ArrayList mappings;
  36. Hashtable _cache;
  37. int ownIndex;
  38. public HandlerFactoryConfiguration () : this (null)
  39. {
  40. }
  41. public HandlerFactoryConfiguration (HandlerFactoryConfiguration parent)
  42. {
  43. if (parent != null)
  44. mappings = new ArrayList (parent.mappings);
  45. else
  46. mappings = new ArrayList ();
  47. _cache = Hashtable.Synchronized(new Hashtable());
  48. ownIndex = mappings.Count;
  49. }
  50. public void Add (HandlerItem mapping)
  51. {
  52. mappings.Add (mapping);
  53. }
  54. public HandlerItem Remove (string verb, string path)
  55. {
  56. int i = GetIndex (verb, path);
  57. if (i == -1)
  58. return null;
  59. HandlerItem item = (HandlerItem) mappings [i];
  60. mappings.RemoveAt (i);
  61. if (_cache.ContainsKey(verb+"+"+path))
  62. _cache.Remove(verb+"+"+path);
  63. return item;
  64. }
  65. public void Clear ()
  66. {
  67. mappings.Clear ();
  68. _cache.Clear();
  69. }
  70. public HandlerItem FindHandler (string verb, string path)
  71. {
  72. int i = GetIndex (verb, path);
  73. if (i == -1)
  74. return null;
  75. return (HandlerItem) mappings [i];
  76. }
  77. int GetIndex (string verb, string path)
  78. {
  79. string cahceKey = verb+"+"+path;
  80. object answer = _cache[cahceKey];
  81. if (answer != null)
  82. return (int)answer;
  83. int end = mappings.Count;
  84. for (int i = ownIndex; i < end; i++) {
  85. HandlerItem item = (HandlerItem) mappings [i];
  86. if (item.IsMatch (verb, path))
  87. {
  88. _cache[cahceKey] = i;
  89. return i;
  90. }
  91. }
  92. // parent mappings
  93. end = ownIndex;
  94. for (int i = 0; i < end; i++) {
  95. HandlerItem item = (HandlerItem) mappings [i];
  96. if (item.IsMatch (verb, path))
  97. {
  98. _cache[cahceKey] = i;
  99. return i;
  100. }
  101. }
  102. _cache[cahceKey] = -1;
  103. return -1;
  104. }
  105. }
  106. }