WebConfigurationManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 ("need to handle locationSubPath")]
  55. public static _Configuration OpenMachineConfiguration (string locationSubPath)
  56. {
  57. return OpenMachineConfiguration ();
  58. }
  59. [MonoTODO]
  60. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  61. string server)
  62. {
  63. if (server == null)
  64. return OpenMachineConfiguration (locationSubPath);
  65. throw new NotSupportedException ("Mono doesn't support remote configuration");
  66. }
  67. [MonoTODO]
  68. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  69. string server,
  70. IntPtr userToken)
  71. {
  72. if (server == null)
  73. return OpenMachineConfiguration (locationSubPath);
  74. throw new NotSupportedException ("Mono doesn't support remote configuration");
  75. }
  76. [MonoTODO]
  77. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  78. string server,
  79. string userName,
  80. string password)
  81. {
  82. if (server == null)
  83. return OpenMachineConfiguration (locationSubPath);
  84. throw new NotSupportedException ("Mono doesn't support remote configuration");
  85. }
  86. public static _Configuration OpenWebConfiguration (string path)
  87. {
  88. return OpenWebConfiguration (path, null, null, null, null, null);
  89. }
  90. public static _Configuration OpenWebConfiguration (string path, string site)
  91. {
  92. return OpenWebConfiguration (path, site, null, null, null, null);
  93. }
  94. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
  95. {
  96. return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
  97. }
  98. [MonoTODO]
  99. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
  104. {
  105. return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
  106. }
  107. [MonoTODO]
  108. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
  109. {
  110. if (path == null)
  111. path = "";
  112. string basePath = GetBasePath (path);
  113. _Configuration conf;
  114. lock (configurations) {
  115. conf = (_Configuration) configurations [basePath];
  116. if (conf == null) {
  117. conf = ConfigurationFactory.Create (typeof(WebConfigurationHost), null, basePath, site, locationSubPath, server, userName, password);
  118. configurations [basePath] = conf;
  119. }
  120. }
  121. if (basePath.Length < path.Length) {
  122. // If the path has a file name, look for a location specific configuration
  123. int dif = path.Length - basePath.Length;
  124. string file = path.Substring (path.Length - dif);
  125. int i=0;
  126. while (i < file.Length && file [i] == '/')
  127. i++;
  128. if (i != 0)
  129. file = file.Substring (i);
  130. if (file.Length != 0) {
  131. foreach (ConfigurationLocation loc in conf.Locations) {
  132. if (loc.Path == file)
  133. return loc.OpenConfiguration ();
  134. }
  135. }
  136. }
  137. return conf;
  138. }
  139. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
  140. {
  141. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
  142. }
  143. [MonoTODO ("Do something with the extra parameters")]
  144. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
  145. {
  146. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
  147. }
  148. [MonoTODO ("Do something with the extra parameters")]
  149. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
  150. {
  151. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
  152. }
  153. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
  154. {
  155. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
  156. }
  157. [MonoTODO ("need to handle locationSubPath")]
  158. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
  159. string locationSubPath)
  160. {
  161. return OpenMappedMachineConfiguration (fileMap);
  162. }
  163. [MonoTODO ("apparently this bad boy can return null, but GetWebApplicationSection doesn't")]
  164. public static object GetSection (string sectionName)
  165. {
  166. try {
  167. _Configuration c = OpenWebConfiguration (HttpContext.Current.Request.Path);
  168. return c.GetSection (sectionName);
  169. }
  170. catch {
  171. return GetWebApplicationSection (sectionName);
  172. }
  173. }
  174. [MonoTODO]
  175. public static object GetSection (string sectionName, string path)
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. static _Configuration GetWebApplicationConfiguration ()
  180. {
  181. _Configuration config;
  182. if (HttpContext.Current == null
  183. || HttpContext.Current.Request == null
  184. || HttpContext.Current.Request.ApplicationPath == null
  185. || HttpContext.Current.Request.ApplicationPath == "") {
  186. config = OpenWebConfiguration ("");
  187. }
  188. else {
  189. config = OpenWebConfiguration (HttpContext.Current.Request.ApplicationPath);
  190. }
  191. return config;
  192. }
  193. [MonoTODO]
  194. public static object GetWebApplicationSection (string sectionName)
  195. {
  196. _Configuration config = GetWebApplicationConfiguration ();
  197. ConfigurationSection section = config.GetSection (sectionName);
  198. return section;
  199. }
  200. public static NameValueCollection AppSettings {
  201. get { return ConfigurationManager.AppSettings; }
  202. }
  203. public static ConnectionStringSettingsCollection ConnectionStrings {
  204. get { return ConfigurationManager.ConnectionStrings; }
  205. }
  206. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  207. get { return configFactory; }
  208. }
  209. static string GetBasePath (string path)
  210. {
  211. if (path == "/" || path == "")
  212. return path;
  213. string pd = HttpContext.Current.Request.MapPath (path);
  214. if (!Directory.Exists (pd)) {
  215. int i = path.LastIndexOf ('/');
  216. path = path.Substring (0, i);
  217. }
  218. while (path [path.Length - 1] == '/')
  219. path = path.Substring (0, path.Length - 1);
  220. return path;
  221. }
  222. #region stuff copied from WebConfigurationSettings
  223. #if TARGET_J2EE
  224. static private IConfigurationSystem oldConfig {
  225. get {
  226. return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
  227. }
  228. set {
  229. AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
  230. }
  231. }
  232. static private Web20DefaultConfig config {
  233. get {
  234. return (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("WebConfigurationManager.config");
  235. }
  236. set {
  237. AppDomain.CurrentDomain.SetData("WebConfigurationManager.config", value);
  238. }
  239. }
  240. #else
  241. static Web20DefaultConfig config;
  242. static IInternalConfigSystem configSystem;
  243. #endif
  244. const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
  245. static readonly object lockobj = new object ();
  246. public static void Init ()
  247. {
  248. lock (lockobj) {
  249. if (config != null)
  250. return;
  251. /* deal with the ConfigurationSettings stuff */
  252. {
  253. Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
  254. Type t = typeof (ConfigurationSettings);
  255. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  256. privStatic);
  257. if (changeConfig == null)
  258. throw new ConfigurationException ("Cannot find method CCS");
  259. object [] args = new object [] {settings};
  260. changeConfig.Invoke (null, args);
  261. config = settings;
  262. config.Init ();
  263. }
  264. /* deal with the ConfigurationManager stuff */
  265. {
  266. HttpConfigurationSystem system = new HttpConfigurationSystem ();
  267. Type t = typeof (ConfigurationManager);
  268. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  269. privStatic);
  270. if (changeConfig == null)
  271. throw new ConfigurationException ("Cannot find method CCS");
  272. object [] args = new object [] {system};
  273. changeConfig.Invoke (null, args);
  274. configSystem = system;
  275. }
  276. }
  277. }
  278. }
  279. class Web20DefaultConfig : IConfigurationSystem
  280. {
  281. #if TARGET_J2EE
  282. static private Web20DefaultConfig instance {
  283. get {
  284. Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
  285. if (val == null) {
  286. val = new Web20DefaultConfig();
  287. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
  288. }
  289. return val;
  290. }
  291. set {
  292. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
  293. }
  294. }
  295. #else
  296. static Web20DefaultConfig instance;
  297. #endif
  298. static Web20DefaultConfig ()
  299. {
  300. instance = new Web20DefaultConfig ();
  301. }
  302. public static Web20DefaultConfig GetInstance ()
  303. {
  304. return instance;
  305. }
  306. public object GetConfig (string sectionName)
  307. {
  308. return WebConfigurationManager.GetWebApplicationSection (sectionName);
  309. }
  310. public void Init ()
  311. {
  312. // nothing. We need a context.
  313. }
  314. }
  315. #endregion
  316. }
  317. #endif