2
0

WebConfigurationManager.cs 12 KB

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