WebConfigurationManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // System.Web.Configuration.WebConfigurationManager.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Reflection;
  34. using System.Xml;
  35. using System.Configuration;
  36. using System.Configuration.Internal;
  37. using _Configuration = System.Configuration.Configuration;
  38. namespace System.Web.Configuration {
  39. public abstract class WebConfigurationManager
  40. {
  41. static IInternalConfigConfigurationFactory configFactory;
  42. static Hashtable configurations = new Hashtable ();
  43. static WebConfigurationManager ()
  44. {
  45. PropertyInfo prop = typeof(ConfigurationManager).GetProperty ("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
  46. if (prop != null)
  47. configFactory = prop.GetValue (null, null) as IInternalConfigConfigurationFactory;
  48. }
  49. WebConfigurationManager ()
  50. {
  51. }
  52. public static _Configuration OpenWebConfiguration (string path)
  53. {
  54. return OpenWebConfiguration (path, null, null, null, IntPtr.Zero, null);
  55. }
  56. public static _Configuration OpenWebConfiguration (string path, string site)
  57. {
  58. return OpenWebConfiguration (path, site, null, null, IntPtr.Zero, null);
  59. }
  60. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
  61. {
  62. return OpenWebConfiguration (path, site, locationSubPath, null, IntPtr.Zero, null);
  63. }
  64. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
  65. {
  66. return OpenWebConfiguration (path, site, locationSubPath, server, userToken, null);
  67. }
  68. [MonoTODO ("Do something with the extra parameters")]
  69. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken, string password)
  70. {
  71. string basePath = GetBasePath (path);
  72. _Configuration conf;
  73. lock (configurations) {
  74. conf = (_Configuration) configurations [basePath];
  75. if (conf == null) {
  76. conf = ConfigurationFactory.Create (typeof(WebConfigurationHost), null, path, site, locationSubPath, server, userToken, password);
  77. configurations [basePath] = conf;
  78. }
  79. }
  80. if (basePath.Length < path.Length) {
  81. // If the path has a file name, look for a location specific configuration
  82. int dif = path.Length - basePath.Length;
  83. string file = path.Substring (path.Length - dif);
  84. int i=0;
  85. while (i < file.Length && file [i] == '/')
  86. i++;
  87. if (i != 0)
  88. file = file.Substring (i);
  89. if (file.Length != 0) {
  90. foreach (ConfigurationLocation loc in conf.Locations) {
  91. if (loc.Path == file)
  92. return loc.OpenConfiguration ();
  93. }
  94. }
  95. }
  96. return conf;
  97. }
  98. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
  99. {
  100. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
  101. }
  102. [MonoTODO ("Do something with the extra parameters")]
  103. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
  104. {
  105. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
  106. }
  107. [MonoTODO ("Do something with the extra parameters")]
  108. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
  109. {
  110. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
  111. }
  112. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
  113. {
  114. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
  115. }
  116. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  117. get { return configFactory; }
  118. }
  119. static string GetBasePath (string path)
  120. {
  121. if (path == "/")
  122. return path;
  123. string pd = HttpContext.Current.Request.MapPath (path);
  124. if (!Directory.Exists (pd)) {
  125. int i = path.LastIndexOf ('/');
  126. path = path.Substring (0, i);
  127. }
  128. while (path [path.Length - 1] == '/')
  129. path = path.Substring (0, path.Length - 1);
  130. return path;
  131. }
  132. }
  133. }
  134. #endif