WebConfigurationManager.cs 16 KB

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