WebConfigurationManager.cs 15 KB

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