WebConfigurationManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. public static object GetSection (string sectionName)
  164. {
  165. _Configuration c;
  166. if (HttpContext.Current != null
  167. && HttpContext.Current.Request != null)
  168. c = OpenWebConfiguration (HttpContext.Current.Request.Path);
  169. else
  170. c = OpenWebConfiguration (HttpRuntime.AppDomainAppVirtualPath);
  171. if (c == null)
  172. return null;
  173. else
  174. return c.GetSection (sectionName);
  175. }
  176. [MonoTODO]
  177. public static object GetSection (string sectionName, string path)
  178. {
  179. try {
  180. _Configuration c = OpenWebConfiguration (path);
  181. return c.GetSection (sectionName);
  182. }
  183. catch {
  184. return null;
  185. }
  186. }
  187. static _Configuration GetWebApplicationConfiguration ()
  188. {
  189. _Configuration config;
  190. if (HttpContext.Current == null
  191. || HttpContext.Current.Request == null
  192. || HttpContext.Current.Request.ApplicationPath == null
  193. || HttpContext.Current.Request.ApplicationPath == "") {
  194. config = OpenWebConfiguration ("");
  195. }
  196. else {
  197. config = OpenWebConfiguration (HttpContext.Current.Request.ApplicationPath);
  198. }
  199. return config;
  200. }
  201. static MethodInfo get_runtime_object = typeof (ConfigurationSection).GetMethod ("GetRuntimeObject", BindingFlags.NonPublic | BindingFlags.Instance);
  202. [MonoTODO]
  203. public static object GetWebApplicationSection (string sectionName)
  204. {
  205. _Configuration config = GetWebApplicationConfiguration ();
  206. ConfigurationSection section = config.GetSection (sectionName);
  207. return get_runtime_object.Invoke (section, new object [0]);
  208. }
  209. public static NameValueCollection AppSettings {
  210. get { return ConfigurationManager.AppSettings; }
  211. }
  212. public static ConnectionStringSettingsCollection ConnectionStrings {
  213. get { return ConfigurationManager.ConnectionStrings; }
  214. }
  215. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  216. get { return configFactory; }
  217. }
  218. static string GetBasePath (string path)
  219. {
  220. if (path == "/" || path == "")
  221. return path;
  222. /* first if we can, map it to a physical path
  223. * to see if it corresponds to a file */
  224. if (HttpContext.Current != null
  225. && HttpContext.Current.Request != null) {
  226. string pd = HttpContext.Current.Request.MapPath (path);
  227. if (!Directory.Exists (pd)) {
  228. /* if it does, remove the file from the url */
  229. int i = path.LastIndexOf ('/');
  230. path = path.Substring (0, i);
  231. }
  232. }
  233. if (path.Length == 0)
  234. return path;
  235. /* remove excess /'s from the end of the virtual path */
  236. while (path [path.Length - 1] == '/')
  237. path = path.Substring (0, path.Length - 1);
  238. return path;
  239. }
  240. #region stuff copied from WebConfigurationSettings
  241. #if TARGET_J2EE
  242. static internal IConfigurationSystem oldConfig {
  243. get {
  244. return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
  245. }
  246. set {
  247. AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
  248. }
  249. }
  250. static private Web20DefaultConfig config {
  251. get {
  252. return (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("WebConfigurationManager.config");
  253. }
  254. set {
  255. AppDomain.CurrentDomain.SetData("WebConfigurationManager.config", value);
  256. }
  257. }
  258. #else
  259. static internal IConfigurationSystem oldConfig;
  260. static Web20DefaultConfig config;
  261. static IInternalConfigSystem configSystem;
  262. #endif
  263. const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
  264. static readonly object lockobj = new object ();
  265. internal static void Init ()
  266. {
  267. lock (lockobj) {
  268. if (config != null)
  269. return;
  270. /* deal with the ConfigurationSettings stuff */
  271. {
  272. Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
  273. Type t = typeof (ConfigurationSettings);
  274. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  275. privStatic);
  276. if (changeConfig == null)
  277. throw new ConfigurationException ("Cannot find method CCS");
  278. object [] args = new object [] {settings};
  279. oldConfig = (IConfigurationSystem)changeConfig.Invoke (null, args);
  280. config = settings;
  281. config.Init ();
  282. }
  283. /* deal with the ConfigurationManager stuff */
  284. {
  285. HttpConfigurationSystem system = new HttpConfigurationSystem ();
  286. Type t = typeof (ConfigurationManager);
  287. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  288. privStatic);
  289. if (changeConfig == null)
  290. throw new ConfigurationException ("Cannot find method CCS");
  291. object [] args = new object [] {system};
  292. changeConfig.Invoke (null, args);
  293. configSystem = system;
  294. }
  295. }
  296. }
  297. }
  298. class Web20DefaultConfig : IConfigurationSystem
  299. {
  300. #if TARGET_J2EE
  301. static private Web20DefaultConfig instance {
  302. get {
  303. Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
  304. if (val == null) {
  305. val = new Web20DefaultConfig();
  306. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
  307. }
  308. return val;
  309. }
  310. set {
  311. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
  312. }
  313. }
  314. #else
  315. static Web20DefaultConfig instance;
  316. #endif
  317. static Web20DefaultConfig ()
  318. {
  319. instance = new Web20DefaultConfig ();
  320. }
  321. public static Web20DefaultConfig GetInstance ()
  322. {
  323. return instance;
  324. }
  325. public object GetConfig (string sectionName)
  326. {
  327. object o = WebConfigurationManager.GetWebApplicationSection (sectionName);
  328. if (o == null || o is IgnoreSection) {
  329. /* this can happen when the section
  330. * handler doesn't subclass from
  331. * ConfigurationSection. let's be
  332. * nice and try to load it using the
  333. * 1.x style routines in case there's
  334. * a 1.x section handler registered
  335. * for it.
  336. */
  337. object o1 = WebConfigurationManager.oldConfig.GetConfig (sectionName);
  338. if (o1 != null)
  339. return o1;
  340. }
  341. return o;
  342. }
  343. public void Init ()
  344. {
  345. // nothing. We need a context.
  346. }
  347. }
  348. #endregion
  349. }
  350. #endif