2
0

WebConfigurationManager.cs 19 KB

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