WebConfigurationManager.cs 12 KB

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