WebConfigurationManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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.Web.Util;
  36. using System.Xml;
  37. using System.Configuration;
  38. using System.Configuration.Internal;
  39. using _Configuration = System.Configuration.Configuration;
  40. namespace System.Web.Configuration {
  41. public static class WebConfigurationManager
  42. {
  43. #if !TARGET_J2EE
  44. static IInternalConfigConfigurationFactory configFactory;
  45. static Hashtable configurations = Hashtable.Synchronized (new Hashtable ());
  46. #else
  47. static internal IInternalConfigConfigurationFactory configFactory
  48. {
  49. get{
  50. IInternalConfigConfigurationFactory factory = (IInternalConfigConfigurationFactory)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory");
  51. if (factory == null){
  52. lock (AppDomain.CurrentDomain){
  53. object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory.initialized");
  54. if (initialized == null){
  55. PropertyInfo prop = typeof(ConfigurationManager).GetProperty("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
  56. if (prop != null){
  57. factory = prop.GetValue(null, null) as IInternalConfigConfigurationFactory;
  58. configFactory = factory;
  59. }
  60. }
  61. }
  62. }
  63. return factory != null ? factory : configFactory;
  64. }
  65. set{
  66. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory", value);
  67. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory.initialized", true);
  68. }
  69. }
  70. static internal Hashtable configurations
  71. {
  72. get{
  73. Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations");
  74. if (table == null){
  75. lock (AppDomain.CurrentDomain){
  76. object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations.initialized");
  77. if (initialized == null){
  78. table = Hashtable.Synchronized (new Hashtable (StringComparer.OrdinalIgnoreCase));
  79. configurations = table;
  80. }
  81. }
  82. }
  83. return table != null ? table : configurations;
  84. }
  85. set{
  86. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations", value);
  87. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations.initialized", true);
  88. }
  89. }
  90. #endif
  91. static ArrayList extra_assemblies = null;
  92. static internal ArrayList ExtraAssemblies {
  93. get {
  94. if (extra_assemblies == null)
  95. extra_assemblies = new ArrayList();
  96. return extra_assemblies;
  97. }
  98. }
  99. static bool hasConfigErrors = false;
  100. static object hasConfigErrorsLock = new object ();
  101. static internal bool HasConfigErrors {
  102. get {
  103. lock (hasConfigErrorsLock) {
  104. return hasConfigErrors;
  105. }
  106. }
  107. }
  108. static WebConfigurationManager ()
  109. {
  110. PropertyInfo prop = typeof(ConfigurationManager).GetProperty ("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
  111. if (prop != null)
  112. configFactory = prop.GetValue (null, null) as IInternalConfigConfigurationFactory;
  113. }
  114. public static _Configuration OpenMachineConfiguration ()
  115. {
  116. return ConfigurationManager.OpenMachineConfiguration ();
  117. }
  118. [MonoLimitation ("locationSubPath is not handled")]
  119. public static _Configuration OpenMachineConfiguration (string locationSubPath)
  120. {
  121. return OpenMachineConfiguration ();
  122. }
  123. [MonoLimitation("Mono does not support remote configuration")]
  124. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  125. string server)
  126. {
  127. if (server == null)
  128. return OpenMachineConfiguration (locationSubPath);
  129. throw new NotSupportedException ("Mono doesn't support remote configuration");
  130. }
  131. [MonoLimitation("Mono does not support remote configuration")]
  132. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  133. string server,
  134. IntPtr userToken)
  135. {
  136. if (server == null)
  137. return OpenMachineConfiguration (locationSubPath);
  138. throw new NotSupportedException ("Mono doesn't support remote configuration");
  139. }
  140. [MonoLimitation("Mono does not support remote configuration")]
  141. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  142. string server,
  143. string userName,
  144. string password)
  145. {
  146. if (server == null)
  147. return OpenMachineConfiguration (locationSubPath);
  148. throw new NotSupportedException ("Mono doesn't support remote configuration");
  149. }
  150. public static _Configuration OpenWebConfiguration (string path)
  151. {
  152. return OpenWebConfiguration (path, null, null, null, null, null);
  153. }
  154. public static _Configuration OpenWebConfiguration (string path, string site)
  155. {
  156. return OpenWebConfiguration (path, site, null, null, null, null);
  157. }
  158. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
  159. {
  160. return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
  161. }
  162. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
  163. {
  164. return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
  165. }
  166. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
  167. {
  168. return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
  169. }
  170. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
  171. {
  172. if (path == null || path.Length == 0)
  173. path = "/";
  174. _Configuration conf;
  175. conf = (_Configuration) configurations [path];
  176. if (conf == null) {
  177. try {
  178. conf = ConfigurationFactory.Create (typeof (WebConfigurationHost), null, path, site, locationSubPath, server, userName, password);
  179. configurations [path] = conf;
  180. } catch (Exception ex) {
  181. lock (hasConfigErrorsLock) {
  182. hasConfigErrors = true;
  183. }
  184. throw ex;
  185. }
  186. }
  187. return conf;
  188. }
  189. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
  190. {
  191. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
  192. }
  193. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
  194. {
  195. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
  196. }
  197. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
  198. {
  199. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
  200. }
  201. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
  202. {
  203. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
  204. }
  205. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
  206. string locationSubPath)
  207. {
  208. return OpenMappedMachineConfiguration (fileMap);
  209. }
  210. internal static object SafeGetSection (string sectionName, Type configSectionType)
  211. {
  212. try {
  213. return GetSection (sectionName);
  214. } catch (Exception) {
  215. if (configSectionType != null)
  216. return Activator.CreateInstance (configSectionType);
  217. return null;
  218. }
  219. }
  220. internal static object SafeGetSection (string sectionName, string path, Type configSectionType)
  221. {
  222. try {
  223. return GetSection (sectionName, path);
  224. } catch (Exception) {
  225. if (configSectionType != null)
  226. return Activator.CreateInstance (configSectionType);
  227. return null;
  228. }
  229. }
  230. public static object GetSection (string sectionName)
  231. {
  232. return GetSection (sectionName, GetCurrentPath (HttpContext.Current));
  233. }
  234. public static object GetSection (string sectionName, string path)
  235. {
  236. _Configuration c = OpenWebConfiguration (path);
  237. ConfigurationSection section = c.GetSection (sectionName);
  238. if (section == null)
  239. return null;
  240. return SettingsMappingManager.MapSection (get_runtime_object.Invoke (section, new object [0]));
  241. }
  242. static string GetCurrentPath (HttpContext ctx)
  243. {
  244. return (ctx != null && ctx.Request != null) ? ctx.Request.Path : HttpRuntime.AppDomainAppVirtualPath;
  245. }
  246. internal static void RemoveConfigurationFromCache (HttpContext ctx)
  247. {
  248. configurations.Remove (GetCurrentPath (ctx));
  249. }
  250. readonly static MethodInfo get_runtime_object = typeof (ConfigurationSection).GetMethod ("GetRuntimeObject", BindingFlags.NonPublic | BindingFlags.Instance);
  251. public static object GetWebApplicationSection (string sectionName)
  252. {
  253. string path = (HttpContext.Current == null
  254. || HttpContext.Current.Request == null
  255. || HttpContext.Current.Request.ApplicationPath == null
  256. || HttpContext.Current.Request.ApplicationPath == "") ?
  257. String.Empty : HttpContext.Current.Request.ApplicationPath;
  258. return GetSection (sectionName, path);
  259. }
  260. public static NameValueCollection AppSettings {
  261. get { return ConfigurationManager.AppSettings; }
  262. }
  263. public static ConnectionStringSettingsCollection ConnectionStrings {
  264. get { return ConfigurationManager.ConnectionStrings; }
  265. }
  266. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  267. get { return configFactory; }
  268. }
  269. #region stuff copied from WebConfigurationSettings
  270. #if TARGET_J2EE
  271. static internal IConfigurationSystem oldConfig {
  272. get {
  273. return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
  274. }
  275. set {
  276. AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
  277. }
  278. }
  279. static private Web20DefaultConfig config {
  280. get {
  281. return (Web20DefaultConfig) AppDomain.CurrentDomain.GetData ("Web20DefaultConfig.config");
  282. }
  283. set {
  284. AppDomain.CurrentDomain.SetData ("Web20DefaultConfig.config", value);
  285. }
  286. }
  287. static private IInternalConfigSystem configSystem {
  288. get {
  289. return (IInternalConfigSystem) AppDomain.CurrentDomain.GetData ("IInternalConfigSystem.configSystem");
  290. }
  291. set {
  292. AppDomain.CurrentDomain.SetData ("IInternalConfigSystem.configSystem", value);
  293. }
  294. }
  295. #else
  296. static internal IConfigurationSystem oldConfig;
  297. static Web20DefaultConfig config;
  298. //static IInternalConfigSystem configSystem;
  299. #endif
  300. const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
  301. static readonly object lockobj = new object ();
  302. internal static void Init ()
  303. {
  304. lock (lockobj) {
  305. if (config != null)
  306. return;
  307. /* deal with the ConfigurationSettings stuff */
  308. {
  309. Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
  310. Type t = typeof (ConfigurationSettings);
  311. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  312. privStatic);
  313. if (changeConfig == null)
  314. throw new ConfigurationException ("Cannot find method CCS");
  315. object [] args = new object [] {settings};
  316. oldConfig = (IConfigurationSystem)changeConfig.Invoke (null, args);
  317. config = settings;
  318. config.Init ();
  319. }
  320. /* deal with the ConfigurationManager stuff */
  321. {
  322. HttpConfigurationSystem system = new HttpConfigurationSystem ();
  323. Type t = typeof (ConfigurationManager);
  324. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  325. privStatic);
  326. if (changeConfig == null)
  327. throw new ConfigurationException ("Cannot find method CCS");
  328. object [] args = new object [] {system};
  329. changeConfig.Invoke (null, args);
  330. //configSystem = system;
  331. }
  332. }
  333. }
  334. }
  335. class Web20DefaultConfig : IConfigurationSystem
  336. {
  337. #if TARGET_J2EE
  338. static private Web20DefaultConfig instance {
  339. get {
  340. Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
  341. if (val == null) {
  342. val = new Web20DefaultConfig();
  343. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
  344. }
  345. return val;
  346. }
  347. set {
  348. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
  349. }
  350. }
  351. #else
  352. static Web20DefaultConfig instance;
  353. #endif
  354. static Web20DefaultConfig ()
  355. {
  356. instance = new Web20DefaultConfig ();
  357. }
  358. public static Web20DefaultConfig GetInstance ()
  359. {
  360. return instance;
  361. }
  362. public object GetConfig (string sectionName)
  363. {
  364. object o = WebConfigurationManager.GetWebApplicationSection (sectionName);
  365. if (o == null || o is IgnoreSection) {
  366. /* this can happen when the section
  367. * handler doesn't subclass from
  368. * ConfigurationSection. let's be
  369. * nice and try to load it using the
  370. * 1.x style routines in case there's
  371. * a 1.x section handler registered
  372. * for it.
  373. */
  374. object o1 = WebConfigurationManager.oldConfig.GetConfig (sectionName);
  375. if (o1 != null)
  376. return o1;
  377. }
  378. return o;
  379. }
  380. public void Init ()
  381. {
  382. // nothing. We need a context.
  383. }
  384. }
  385. #endregion
  386. }
  387. #endif