WebConfigurationManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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.Generic;
  34. using System.Collections.Specialized;
  35. using System.Reflection;
  36. #if MONOWEB_DEP
  37. using Mono.Web.Util;
  38. #endif
  39. using System.Xml;
  40. using System.Configuration;
  41. using System.Configuration.Internal;
  42. using _Configuration = System.Configuration.Configuration;
  43. using System.Web.Util;
  44. using System.Threading;
  45. namespace System.Web.Configuration {
  46. public static class WebConfigurationManager
  47. {
  48. const int SAVE_LOCATIONS_CHECK_INTERVAL = 6000; // milliseconds
  49. static readonly object suppressAppReloadLock = new object ();
  50. static readonly object saveLocationsCacheLock = new object ();
  51. #if !TARGET_J2EE
  52. static IInternalConfigConfigurationFactory configFactory;
  53. static Hashtable configurations = Hashtable.Synchronized (new Hashtable ());
  54. static Hashtable sectionCache = new Hashtable ();
  55. static Hashtable configPaths = Hashtable.Synchronized (new Hashtable ());
  56. static bool suppressAppReload;
  57. #else
  58. const string AppSettingsKey = "WebConfigurationManager.AppSettings";
  59. static internal IInternalConfigConfigurationFactory configFactory
  60. {
  61. get{
  62. IInternalConfigConfigurationFactory factory = (IInternalConfigConfigurationFactory)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory");
  63. if (factory == null){
  64. lock (AppDomain.CurrentDomain){
  65. object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory.initialized");
  66. if (initialized == null){
  67. PropertyInfo prop = typeof(ConfigurationManager).GetProperty("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
  68. if (prop != null){
  69. factory = prop.GetValue(null, null) as IInternalConfigConfigurationFactory;
  70. configFactory = factory;
  71. }
  72. }
  73. }
  74. }
  75. return factory != null ? factory : configFactory;
  76. }
  77. set{
  78. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory", value);
  79. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory.initialized", true);
  80. }
  81. }
  82. static internal Hashtable configurations
  83. {
  84. get{
  85. Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations");
  86. if (table == null){
  87. lock (AppDomain.CurrentDomain){
  88. object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations.initialized");
  89. if (initialized == null){
  90. table = Hashtable.Synchronized (new Hashtable (StringComparer.OrdinalIgnoreCase));
  91. configurations = table;
  92. }
  93. }
  94. }
  95. return table != null ? table : configurations;
  96. }
  97. set{
  98. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations", value);
  99. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations.initialized", true);
  100. }
  101. }
  102. static Hashtable sectionCache
  103. {
  104. get
  105. {
  106. Hashtable sectionCache = (Hashtable) AppDomain.CurrentDomain.GetData ("sectionCache");
  107. if (sectionCache == null) {
  108. sectionCache = new Hashtable (StringComparer.OrdinalIgnoreCase);
  109. AppDomain.CurrentDomain.SetData ("sectionCache", sectionCache);
  110. }
  111. return sectionCache;
  112. }
  113. set
  114. {
  115. AppDomain.CurrentDomain.SetData ("sectionCache", value);
  116. }
  117. }
  118. static internal Hashtable configPaths
  119. {
  120. get{
  121. Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configPaths");
  122. if (table == null){
  123. lock (AppDomain.CurrentDomain){
  124. object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configPaths.initialized");
  125. if (initialized == null){
  126. table = Hashtable.Synchronized (new Hashtable (StringComparer.OrdinalIgnoreCase));
  127. configPaths = table;
  128. }
  129. }
  130. }
  131. return table != null ? table : configPaths;
  132. }
  133. set{
  134. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configPaths", value);
  135. AppDomain.CurrentDomain.SetData("WebConfigurationManager.configPaths.initialized", true);
  136. }
  137. }
  138. #endif
  139. static Dictionary <string, DateTime> saveLocationsCache;
  140. static Timer saveLocationsTimer;
  141. static ArrayList extra_assemblies = null;
  142. static internal ArrayList ExtraAssemblies {
  143. get {
  144. if (extra_assemblies == null)
  145. extra_assemblies = new ArrayList();
  146. return extra_assemblies;
  147. }
  148. }
  149. static bool hasConfigErrors = false;
  150. static object hasConfigErrorsLock = new object ();
  151. static internal bool HasConfigErrors {
  152. get {
  153. lock (hasConfigErrorsLock) {
  154. return hasConfigErrors;
  155. }
  156. }
  157. }
  158. static WebConfigurationManager ()
  159. {
  160. configFactory = ConfigurationManager.ConfigurationFactory;
  161. _Configuration.SaveStart += ConfigurationSaveHandler;
  162. _Configuration.SaveEnd += ConfigurationSaveHandler;
  163. // Part of fix for bug #491531
  164. Type type = Type.GetType ("System.Configuration.CustomizableFileSettingsProvider, System", false);
  165. if (type != null) {
  166. FieldInfo fi = type.GetField ("webConfigurationFileMapType", BindingFlags.Static | BindingFlags.NonPublic);
  167. if (fi != null && fi.FieldType == Type.GetType ("System.Type"))
  168. fi.SetValue (null, typeof (ApplicationSettingsConfigurationFileMap));
  169. }
  170. }
  171. static void ReenableWatcherOnConfigLocation (object state)
  172. {
  173. string path = state as string;
  174. if (String.IsNullOrEmpty (path))
  175. return;
  176. DateTime lastWrite;
  177. lock (saveLocationsCacheLock) {
  178. if (!saveLocationsCache.TryGetValue (path, out lastWrite))
  179. lastWrite = DateTime.MinValue;
  180. }
  181. DateTime now = DateTime.Now;
  182. if (lastWrite == DateTime.MinValue || now.Subtract (lastWrite).TotalMilliseconds >= SAVE_LOCATIONS_CHECK_INTERVAL) {
  183. saveLocationsTimer.Dispose ();
  184. saveLocationsTimer = null;
  185. HttpApplicationFactory.EnableWatcher (VirtualPathUtility.RemoveTrailingSlash (HttpRuntime.AppDomainAppPath), "?eb.?onfig");
  186. } else
  187. saveLocationsTimer.Change (SAVE_LOCATIONS_CHECK_INTERVAL, SAVE_LOCATIONS_CHECK_INTERVAL);
  188. }
  189. static void ConfigurationSaveHandler (_Configuration sender, ConfigurationSaveEventArgs args)
  190. {
  191. lock (suppressAppReloadLock) {
  192. string rootConfigPath = WebConfigurationHost.GetWebConfigFileName (HttpRuntime.AppDomainAppPath);
  193. if (String.Compare (args.StreamPath, rootConfigPath, StringComparison.OrdinalIgnoreCase) == 0) {
  194. SuppressAppReload (args.Start);
  195. if (args.Start) {
  196. HttpApplicationFactory.DisableWatcher (VirtualPathUtility.RemoveTrailingSlash (HttpRuntime.AppDomainAppPath), "?eb.?onfig");
  197. lock (saveLocationsCacheLock) {
  198. if (saveLocationsCache == null)
  199. saveLocationsCache = new Dictionary <string, DateTime> (StringComparer.Ordinal);
  200. if (saveLocationsCache.ContainsKey (rootConfigPath))
  201. saveLocationsCache [rootConfigPath] = DateTime.Now;
  202. else
  203. saveLocationsCache.Add (rootConfigPath, DateTime.Now);
  204. if (saveLocationsTimer == null)
  205. saveLocationsTimer = new Timer (ReenableWatcherOnConfigLocation,
  206. rootConfigPath,
  207. SAVE_LOCATIONS_CHECK_INTERVAL,
  208. SAVE_LOCATIONS_CHECK_INTERVAL);
  209. }
  210. }
  211. }
  212. }
  213. }
  214. public static _Configuration OpenMachineConfiguration ()
  215. {
  216. return ConfigurationManager.OpenMachineConfiguration ();
  217. }
  218. [MonoLimitation ("locationSubPath is not handled")]
  219. public static _Configuration OpenMachineConfiguration (string locationSubPath)
  220. {
  221. return OpenMachineConfiguration ();
  222. }
  223. [MonoLimitation("Mono does not support remote configuration")]
  224. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  225. string server)
  226. {
  227. if (server == null)
  228. return OpenMachineConfiguration (locationSubPath);
  229. throw new NotSupportedException ("Mono doesn't support remote configuration");
  230. }
  231. [MonoLimitation("Mono does not support remote configuration")]
  232. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  233. string server,
  234. IntPtr userToken)
  235. {
  236. if (server == null)
  237. return OpenMachineConfiguration (locationSubPath);
  238. throw new NotSupportedException ("Mono doesn't support remote configuration");
  239. }
  240. [MonoLimitation("Mono does not support remote configuration")]
  241. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  242. string server,
  243. string userName,
  244. string password)
  245. {
  246. if (server == null)
  247. return OpenMachineConfiguration (locationSubPath);
  248. throw new NotSupportedException ("Mono doesn't support remote configuration");
  249. }
  250. public static _Configuration OpenWebConfiguration (string path)
  251. {
  252. return OpenWebConfiguration (path, null, null, null, null, null);
  253. }
  254. public static _Configuration OpenWebConfiguration (string path, string site)
  255. {
  256. return OpenWebConfiguration (path, site, null, null, null, null);
  257. }
  258. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
  259. {
  260. return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
  261. }
  262. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
  263. {
  264. return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
  265. }
  266. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
  267. {
  268. return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
  269. }
  270. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
  271. {
  272. return OpenWebConfiguration (path, site, locationSubPath, server, null, null, false);
  273. }
  274. static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password, bool fweb)
  275. {
  276. if (String.IsNullOrEmpty (path))
  277. path = "/";
  278. if (!fweb && !String.IsNullOrEmpty (path))
  279. path = FindWebConfig (path);
  280. string confKey = path + site + locationSubPath + server + userName + password;
  281. _Configuration conf = null;
  282. conf = (_Configuration) configurations [confKey];
  283. if (conf == null) {
  284. try {
  285. conf = ConfigurationFactory.Create (typeof (WebConfigurationHost), null, path, site, locationSubPath, server, userName, password);
  286. configurations [confKey] = conf;
  287. } catch (Exception ex) {
  288. lock (hasConfigErrorsLock) {
  289. hasConfigErrors = true;
  290. }
  291. throw ex;
  292. }
  293. }
  294. return conf;
  295. }
  296. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
  297. {
  298. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
  299. }
  300. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
  301. {
  302. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
  303. }
  304. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
  305. {
  306. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
  307. }
  308. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
  309. {
  310. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
  311. }
  312. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
  313. string locationSubPath)
  314. {
  315. return OpenMappedMachineConfiguration (fileMap);
  316. }
  317. internal static object SafeGetSection (string sectionName, Type configSectionType)
  318. {
  319. try {
  320. return GetSection (sectionName);
  321. } catch (Exception) {
  322. if (configSectionType != null)
  323. return Activator.CreateInstance (configSectionType);
  324. return null;
  325. }
  326. }
  327. internal static object SafeGetSection (string sectionName, string path, Type configSectionType)
  328. {
  329. try {
  330. return GetSection (sectionName, path);
  331. } catch (Exception) {
  332. if (configSectionType != null)
  333. return Activator.CreateInstance (configSectionType);
  334. return null;
  335. }
  336. }
  337. public static object GetSection (string sectionName)
  338. {
  339. HttpContext context = HttpContext.Current;
  340. return GetSection (sectionName, GetCurrentPath (context), context);
  341. }
  342. public static object GetSection (string sectionName, string path)
  343. {
  344. return GetSection (sectionName, path, HttpContext.Current);
  345. }
  346. internal static object GetSection (string sectionName, string path, HttpContext context)
  347. {
  348. string config_vdir = FindWebConfig (path);
  349. if (String.IsNullOrEmpty (config_vdir))
  350. config_vdir = "/";
  351. object cachedSection = sectionCache [GetSectionCacheKey (sectionName, config_vdir)];
  352. if (cachedSection != null)
  353. return cachedSection;
  354. HttpRequest req = context != null ? context.Request : null;
  355. _Configuration c = OpenWebConfiguration (config_vdir, //path, /* path */
  356. null, /* site */
  357. req != null ? VirtualPathUtility.GetDirectory (req.Path) : null, /* locationSubPath */
  358. null, /* server */
  359. null, /* userName */
  360. null, /* password */
  361. true /* path from FindWebConfig */);
  362. ConfigurationSection section = c.GetSection (sectionName);
  363. if (section == null)
  364. return null;
  365. #if TARGET_J2EE
  366. object value = get_runtime_object.Invoke (section, new object [0]);
  367. if (String.CompareOrdinal ("appSettings", sectionName) == 0) {
  368. NameValueCollection collection;
  369. collection = new KeyValueMergedCollection (HttpContext.Current, (NameValueCollection) value);
  370. value = collection;
  371. }
  372. AddSectionToCache (GetSectionCacheKey (sectionName, config_vdir), value);
  373. return value;
  374. #else
  375. #if MONOWEB_DEP
  376. object value = SettingsMappingManager.MapSection (get_runtime_object.Invoke (section, new object [0]));
  377. #else
  378. object value = null;
  379. #endif
  380. AddSectionToCache (GetSectionCacheKey (sectionName, config_vdir), value);
  381. return value;
  382. #endif
  383. }
  384. static string MapPath (HttpRequest req, string virtualPath)
  385. {
  386. if (req != null)
  387. return req.MapPath (virtualPath);
  388. string appRoot = HttpRuntime.AppDomainAppVirtualPath;
  389. if (!String.IsNullOrEmpty (appRoot) && virtualPath.StartsWith (appRoot, StringComparison.Ordinal)) {
  390. if (String.Compare (virtualPath, appRoot, StringComparison.Ordinal) == 0)
  391. return HttpRuntime.AppDomainAppPath;
  392. return UrlUtils.Combine (HttpRuntime.AppDomainAppPath, virtualPath.Substring (appRoot.Length));
  393. }
  394. return null;
  395. }
  396. static string GetParentDir (string rootPath, string curPath)
  397. {
  398. int len = curPath.Length - 1;
  399. if (len > 0 && curPath [len] == '/')
  400. curPath = curPath.Substring (0, len);
  401. if (String.Compare (curPath, rootPath, StringComparison.Ordinal) == 0)
  402. return null;
  403. int idx = curPath.LastIndexOf ('/');
  404. if (idx == -1)
  405. return curPath;
  406. if (idx == 0)
  407. return "/";
  408. return curPath.Substring (0, idx);
  409. }
  410. internal static string FindWebConfig (string path)
  411. {
  412. if (String.IsNullOrEmpty (path))
  413. return path;
  414. string dir;
  415. if (path [path.Length - 1] == '/')
  416. dir = path;
  417. else {
  418. dir = VirtualPathUtility.GetDirectory (path, false);
  419. if (dir == null)
  420. return path;
  421. }
  422. string curPath = configPaths [dir] as string;
  423. if (curPath != null)
  424. return curPath;
  425. HttpContext ctx = HttpContext.Current;
  426. HttpRequest req = ctx != null ? ctx.Request : null;
  427. if (req == null)
  428. return path;
  429. curPath = path;
  430. string rootPath = HttpRuntime.AppDomainAppVirtualPath;
  431. string physPath;
  432. while (String.Compare (curPath, rootPath, StringComparison.Ordinal) != 0) {
  433. physPath = MapPath (req, curPath);
  434. if (physPath == null) {
  435. curPath = rootPath;
  436. break;
  437. }
  438. if (WebConfigurationHost.GetWebConfigFileName (physPath) != null)
  439. break;
  440. curPath = GetParentDir (rootPath, curPath);
  441. if (curPath == null) {
  442. curPath = rootPath;
  443. break;
  444. }
  445. }
  446. configPaths [dir] = curPath;
  447. return curPath;
  448. }
  449. static string GetCurrentPath (HttpContext ctx)
  450. {
  451. HttpRequest req = ctx != null ? ctx.Request : null;
  452. return req != null ? req.Path : HttpRuntime.AppDomainAppVirtualPath;
  453. }
  454. internal static bool SuppressAppReload (bool newValue)
  455. {
  456. bool ret;
  457. lock (suppressAppReloadLock) {
  458. ret = suppressAppReload;
  459. suppressAppReload = newValue;
  460. }
  461. return ret;
  462. }
  463. internal static void RemoveConfigurationFromCache (HttpContext ctx)
  464. {
  465. configurations.Remove (GetCurrentPath (ctx));
  466. }
  467. #if TARGET_J2EE || MONOWEB_DEP
  468. readonly static MethodInfo get_runtime_object = typeof (ConfigurationSection).GetMethod ("GetRuntimeObject", BindingFlags.NonPublic | BindingFlags.Instance);
  469. #endif
  470. public static object GetWebApplicationSection (string sectionName)
  471. {
  472. HttpContext ctx = HttpContext.Current;
  473. HttpRequest req = ctx != null ? ctx.Request : null;
  474. string applicationPath = req != null ? req.ApplicationPath : null;
  475. return GetSection (sectionName, String.IsNullOrEmpty (applicationPath) ? String.Empty : applicationPath);
  476. }
  477. public static NameValueCollection AppSettings {
  478. get { return ConfigurationManager.AppSettings; }
  479. }
  480. public static ConnectionStringSettingsCollection ConnectionStrings {
  481. get { return ConfigurationManager.ConnectionStrings; }
  482. }
  483. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  484. get { return configFactory; }
  485. }
  486. static void AddSectionToCache (int key, object section)
  487. {
  488. if (sectionCache [key] != null)
  489. return;
  490. Hashtable tmpTable = (Hashtable) sectionCache.Clone ();
  491. if (tmpTable.Contains (key))
  492. return;
  493. tmpTable.Add (key, section);
  494. sectionCache = tmpTable;
  495. }
  496. static int GetSectionCacheKey (string sectionName, string path)
  497. {
  498. return (sectionName != null ? sectionName.GetHashCode () : 0) ^ ((path != null ? path.GetHashCode () : 0) + 37);
  499. }
  500. #region stuff copied from WebConfigurationSettings
  501. #if TARGET_J2EE
  502. static internal IConfigurationSystem oldConfig {
  503. get {
  504. return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
  505. }
  506. set {
  507. AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
  508. }
  509. }
  510. static Web20DefaultConfig config {
  511. get {
  512. return (Web20DefaultConfig) AppDomain.CurrentDomain.GetData ("Web20DefaultConfig.config");
  513. }
  514. set {
  515. AppDomain.CurrentDomain.SetData ("Web20DefaultConfig.config", value);
  516. }
  517. }
  518. static IInternalConfigSystem configSystem {
  519. get {
  520. return (IInternalConfigSystem) AppDomain.CurrentDomain.GetData ("IInternalConfigSystem.configSystem");
  521. }
  522. set {
  523. AppDomain.CurrentDomain.SetData ("IInternalConfigSystem.configSystem", value);
  524. }
  525. }
  526. #else
  527. static internal IConfigurationSystem oldConfig;
  528. static Web20DefaultConfig config;
  529. //static IInternalConfigSystem configSystem;
  530. #endif
  531. const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
  532. static readonly object lockobj = new object ();
  533. internal static void Init ()
  534. {
  535. lock (lockobj) {
  536. if (config != null)
  537. return;
  538. /* deal with the ConfigurationSettings stuff */
  539. {
  540. Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
  541. Type t = typeof (ConfigurationSettings);
  542. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  543. privStatic);
  544. if (changeConfig == null)
  545. throw new ConfigurationException ("Cannot find method CCS");
  546. object [] args = new object [] {settings};
  547. oldConfig = (IConfigurationSystem)changeConfig.Invoke (null, args);
  548. config = settings;
  549. config.Init ();
  550. }
  551. /* deal with the ConfigurationManager stuff */
  552. {
  553. HttpConfigurationSystem system = new HttpConfigurationSystem ();
  554. Type t = typeof (ConfigurationManager);
  555. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  556. privStatic);
  557. if (changeConfig == null)
  558. throw new ConfigurationException ("Cannot find method CCS");
  559. object [] args = new object [] {system};
  560. changeConfig.Invoke (null, args);
  561. //configSystem = system;
  562. }
  563. }
  564. }
  565. }
  566. class Web20DefaultConfig : IConfigurationSystem
  567. {
  568. #if TARGET_J2EE
  569. static Web20DefaultConfig instance {
  570. get {
  571. Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
  572. if (val == null) {
  573. val = new Web20DefaultConfig();
  574. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
  575. }
  576. return val;
  577. }
  578. set {
  579. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
  580. }
  581. }
  582. #else
  583. static Web20DefaultConfig instance;
  584. #endif
  585. static Web20DefaultConfig ()
  586. {
  587. instance = new Web20DefaultConfig ();
  588. }
  589. public static Web20DefaultConfig GetInstance ()
  590. {
  591. return instance;
  592. }
  593. public object GetConfig (string sectionName)
  594. {
  595. object o = WebConfigurationManager.GetWebApplicationSection (sectionName);
  596. if (o == null || o is IgnoreSection) {
  597. /* this can happen when the section
  598. * handler doesn't subclass from
  599. * ConfigurationSection. let's be
  600. * nice and try to load it using the
  601. * 1.x style routines in case there's
  602. * a 1.x section handler registered
  603. * for it.
  604. */
  605. object o1 = WebConfigurationManager.oldConfig.GetConfig (sectionName);
  606. if (o1 != null)
  607. return o1;
  608. }
  609. return o;
  610. }
  611. public void Init ()
  612. {
  613. // nothing. We need a context.
  614. }
  615. }
  616. #endregion
  617. }
  618. #endif