WebConfigurationManager.cs 15 KB

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