WebConfigurationManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //
  2. // System.Web.Configuration.WebConfigurationManager.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. // Chris Toshok ([email protected])
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.IO;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Reflection;
  35. using System.Xml;
  36. using System.Configuration;
  37. using System.Configuration.Internal;
  38. using _Configuration = System.Configuration.Configuration;
  39. namespace System.Web.Configuration {
  40. public static class WebConfigurationManager
  41. {
  42. static IInternalConfigConfigurationFactory configFactory;
  43. static Hashtable configurations = new Hashtable ();
  44. static WebConfigurationManager ()
  45. {
  46. PropertyInfo prop = typeof(ConfigurationManager).GetProperty ("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
  47. if (prop != null)
  48. configFactory = prop.GetValue (null, null) as IInternalConfigConfigurationFactory;
  49. }
  50. public static _Configuration OpenMachineConfiguration ()
  51. {
  52. return ConfigurationManager.OpenMachineConfiguration ();
  53. }
  54. [MonoTODO]
  55. public static _Configuration OpenMachineConfiguration (string locationSubPath)
  56. {
  57. throw new NotImplementedException ();
  58. }
  59. [MonoTODO]
  60. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  61. string server)
  62. {
  63. throw new NotImplementedException ();
  64. }
  65. [MonoTODO]
  66. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  67. string server,
  68. IntPtr userToken)
  69. {
  70. throw new NotImplementedException ();
  71. }
  72. [MonoTODO]
  73. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  74. string server,
  75. string userName,
  76. string password)
  77. {
  78. throw new NotImplementedException ();
  79. }
  80. public static _Configuration OpenWebConfiguration (string path)
  81. {
  82. return OpenWebConfiguration (path, null, null, null, null, null);
  83. }
  84. public static _Configuration OpenWebConfiguration (string path, string site)
  85. {
  86. return OpenWebConfiguration (path, site, null, null, null, null);
  87. }
  88. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
  89. {
  90. return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
  91. }
  92. [MonoTODO]
  93. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
  94. {
  95. throw new NotImplementedException ();
  96. }
  97. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
  98. {
  99. return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
  100. }
  101. [MonoTODO]
  102. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
  103. {
  104. string basePath = GetBasePath (path);
  105. _Configuration conf;
  106. lock (configurations) {
  107. conf = (_Configuration) configurations [basePath];
  108. if (conf == null) {
  109. conf = ConfigurationFactory.Create (typeof(WebConfigurationHost), null, path, site, locationSubPath, server, userName, password);
  110. configurations [basePath] = conf;
  111. }
  112. }
  113. if (basePath.Length < path.Length) {
  114. // If the path has a file name, look for a location specific configuration
  115. int dif = path.Length - basePath.Length;
  116. string file = path.Substring (path.Length - dif);
  117. int i=0;
  118. while (i < file.Length && file [i] == '/')
  119. i++;
  120. if (i != 0)
  121. file = file.Substring (i);
  122. if (file.Length != 0) {
  123. foreach (ConfigurationLocation loc in conf.Locations) {
  124. if (loc.Path == file)
  125. return loc.OpenConfiguration ();
  126. }
  127. }
  128. }
  129. return conf;
  130. }
  131. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
  132. {
  133. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
  134. }
  135. [MonoTODO ("Do something with the extra parameters")]
  136. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
  137. {
  138. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
  139. }
  140. [MonoTODO ("Do something with the extra parameters")]
  141. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
  142. {
  143. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
  144. }
  145. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
  146. {
  147. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
  148. }
  149. [MonoTODO]
  150. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
  151. string locationSubPath)
  152. {
  153. throw new NotImplementedException ();
  154. }
  155. [MonoTODO ("this shouldn't call ConfigurationManager.GetSection")]
  156. public static object GetSection (string sectionName)
  157. {
  158. return ConfigurationManager.GetSection (sectionName);
  159. }
  160. [MonoTODO]
  161. public static object GetSection (string sectionName, string path)
  162. {
  163. throw new NotImplementedException ();
  164. }
  165. [MonoTODO]
  166. public static object GetWebApplicationSection (string sectionName)
  167. {
  168. _Configuration config;
  169. if (HttpContext.Current == null
  170. || HttpContext.Current.Request == null
  171. || HttpContext.Current.Request.PhysicalApplicationPath == null)
  172. config = OpenMachineConfiguration ();
  173. else
  174. config = OpenWebConfiguration (HttpContext.Current.Request.PhysicalApplicationPath);
  175. ConfigurationSection section = config.GetSection (sectionName);
  176. return section;
  177. }
  178. [MonoTODO]
  179. public static NameValueCollection AppSettings {
  180. get {
  181. throw new NotImplementedException ();
  182. }
  183. }
  184. [MonoTODO]
  185. public static ConnectionStringSettingsCollection ConnectionStrings {
  186. get {
  187. throw new NotImplementedException ();
  188. }
  189. }
  190. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  191. get { return configFactory; }
  192. }
  193. static string GetBasePath (string path)
  194. {
  195. if (path == "/")
  196. return path;
  197. string pd = HttpContext.Current.Request.MapPath (path);
  198. if (!Directory.Exists (pd)) {
  199. int i = path.LastIndexOf ('/');
  200. path = path.Substring (0, i);
  201. }
  202. while (path [path.Length - 1] == '/')
  203. path = path.Substring (0, path.Length - 1);
  204. return path;
  205. }
  206. }
  207. }
  208. #endif