WebConfigurationManager.cs 16 KB

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