WebConfigurationManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 NotSupportedException ("Mono doesn't support remote configuration");
  64. }
  65. [MonoTODO]
  66. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  67. string server,
  68. IntPtr userToken)
  69. {
  70. throw new NotSupportedException ("Mono doesn't support remote configuration");
  71. }
  72. [MonoTODO]
  73. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  74. string server,
  75. string userName,
  76. string password)
  77. {
  78. throw new NotSupportedException ("Mono doesn't support remote configuration");
  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. public static object GetSection (string sectionName)
  156. {
  157. object section = GetWebApplicationSection (sectionName);
  158. if (section != null)
  159. return section;
  160. return ConfigurationManager.GetSection (sectionName);
  161. }
  162. [MonoTODO]
  163. public static object GetSection (string sectionName, string path)
  164. {
  165. throw new NotImplementedException ();
  166. }
  167. static _Configuration GetWebApplicationConfiguration ()
  168. {
  169. _Configuration config;
  170. if (HttpContext.Current == null
  171. || HttpContext.Current.Request == null
  172. || HttpContext.Current.Request.PhysicalApplicationPath == null)
  173. config = OpenMachineConfiguration ();
  174. else
  175. config = OpenWebConfiguration (HttpContext.Current.Request.PhysicalApplicationPath);
  176. return config;
  177. }
  178. [MonoTODO]
  179. public static object GetWebApplicationSection (string sectionName)
  180. {
  181. _Configuration config = GetWebApplicationConfiguration ();
  182. ConfigurationSection section = config.GetSection (sectionName);
  183. return section;
  184. }
  185. public static NameValueCollection AppSettings {
  186. get { return ConfigurationManager.AppSettings; }
  187. }
  188. public static ConnectionStringSettingsCollection ConnectionStrings {
  189. get { return ConfigurationManager.ConnectionStrings; }
  190. }
  191. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  192. get { return configFactory; }
  193. }
  194. static string GetBasePath (string path)
  195. {
  196. if (path == "/")
  197. return path;
  198. string pd = HttpContext.Current.Request.MapPath (path);
  199. if (!Directory.Exists (pd)) {
  200. int i = path.LastIndexOf ('/');
  201. path = path.Substring (0, i);
  202. }
  203. while (path [path.Length - 1] == '/')
  204. path = path.Substring (0, path.Length - 1);
  205. return path;
  206. }
  207. #region stuff copied from WebConfigurationSettings
  208. #if TARGET_J2EE
  209. static private IConfigurationSystem oldConfig {
  210. get {
  211. return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
  212. }
  213. set {
  214. AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
  215. }
  216. }
  217. static private Web20DefaultConfig config {
  218. get {
  219. return (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("WebConfigurationManager.config");
  220. }
  221. set {
  222. AppDomain.CurrentDomain.SetData("WebConfigurationManager.config", value);
  223. }
  224. }
  225. #else
  226. static Web20DefaultConfig config;
  227. #if NET_2_0
  228. static IInternalConfigSystem configSystem;
  229. #endif
  230. #endif
  231. const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
  232. static readonly object lockobj = new object ();
  233. public static void Init ()
  234. {
  235. lock (lockobj) {
  236. if (config != null)
  237. return;
  238. /* deal with the ConfigurationSettings stuff */
  239. {
  240. Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
  241. Type t = typeof (ConfigurationSettings);
  242. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  243. privStatic);
  244. if (changeConfig == null)
  245. throw new ConfigurationException ("Cannot find method CCS");
  246. object [] args = new object [] {settings};
  247. changeConfig.Invoke (null, args);
  248. config = settings;
  249. config.Init ();
  250. }
  251. #if NET_2_0
  252. /* deal with the ConfigurationManager stuff */
  253. {
  254. HttpConfigurationSystem system = new HttpConfigurationSystem ();
  255. Type t = typeof (ConfigurationManager);
  256. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  257. privStatic);
  258. if (changeConfig == null)
  259. throw new ConfigurationException ("Cannot find method CCS");
  260. object [] args = new object [] {system};
  261. changeConfig.Invoke (null, args);
  262. configSystem = system;
  263. }
  264. #endif
  265. }
  266. }
  267. }
  268. class Web20DefaultConfig : IConfigurationSystem
  269. {
  270. #if TARGET_J2EE
  271. static private Web20DefaultConfig instance {
  272. get {
  273. Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
  274. if (val == null) {
  275. val = new Web20DefaultConfig();
  276. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
  277. }
  278. return val;
  279. }
  280. set {
  281. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
  282. }
  283. }
  284. #else
  285. static Web20DefaultConfig instance;
  286. #endif
  287. static Web20DefaultConfig ()
  288. {
  289. instance = new Web20DefaultConfig ();
  290. }
  291. public static Web20DefaultConfig GetInstance ()
  292. {
  293. return instance;
  294. }
  295. public object GetConfig (string sectionName)
  296. {
  297. return WebConfigurationManager.GetSection (sectionName);
  298. }
  299. public void Init ()
  300. {
  301. // nothing. We need a context.
  302. }
  303. }
  304. #endregion
  305. }
  306. #endif