WebConfigurationManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. static IInternalConfigConfigurationFactory configFactory;
  43. static Hashtable configurations = new Hashtable ();
  44. static WebConfigurationManager ()
  45. {
  46. PropertyInfo prop = typeof(ConfigurationManager).GetProperty ("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
  47. if (prop != null)
  48. configFactory = prop.GetValue (null, null) as IInternalConfigConfigurationFactory;
  49. }
  50. public static _Configuration OpenMachineConfiguration ()
  51. {
  52. return ConfigurationManager.OpenMachineConfiguration ();
  53. }
  54. [MonoTODO]
  55. public static _Configuration OpenMachineConfiguration (string locationSubPath)
  56. {
  57. throw new NotImplementedException ();
  58. }
  59. [MonoTODO]
  60. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  61. string server)
  62. {
  63. throw new NotImplementedException ();
  64. }
  65. [MonoTODO]
  66. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  67. string server,
  68. IntPtr userToken)
  69. {
  70. throw new NotImplementedException ();
  71. }
  72. [MonoTODO]
  73. public static _Configuration OpenMachineConfiguration (string locationSubPath,
  74. string server,
  75. string userName,
  76. string password)
  77. {
  78. throw new NotImplementedException ();
  79. }
  80. public static _Configuration OpenWebConfiguration (string path)
  81. {
  82. return OpenWebConfiguration (path, null, null, null, null, null);
  83. }
  84. public static _Configuration OpenWebConfiguration (string path, string site)
  85. {
  86. return OpenWebConfiguration (path, site, null, null, null, null);
  87. }
  88. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
  89. {
  90. return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
  91. }
  92. [MonoTODO]
  93. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
  94. {
  95. throw new NotImplementedException ();
  96. }
  97. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
  98. {
  99. return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
  100. }
  101. [MonoTODO]
  102. public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
  103. {
  104. string basePath = GetBasePath (path);
  105. _Configuration conf;
  106. lock (configurations) {
  107. conf = (_Configuration) configurations [basePath];
  108. if (conf == null) {
  109. conf = ConfigurationFactory.Create (typeof(WebConfigurationHost), null, path, site, locationSubPath, server, userName, password);
  110. configurations [basePath] = conf;
  111. }
  112. }
  113. if (basePath.Length < path.Length) {
  114. // If the path has a file name, look for a location specific configuration
  115. int dif = path.Length - basePath.Length;
  116. string file = path.Substring (path.Length - dif);
  117. int i=0;
  118. while (i < file.Length && file [i] == '/')
  119. i++;
  120. if (i != 0)
  121. file = file.Substring (i);
  122. if (file.Length != 0) {
  123. foreach (ConfigurationLocation loc in conf.Locations) {
  124. if (loc.Path == file)
  125. return loc.OpenConfiguration ();
  126. }
  127. }
  128. }
  129. return conf;
  130. }
  131. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
  132. {
  133. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
  134. }
  135. [MonoTODO ("Do something with the extra parameters")]
  136. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
  137. {
  138. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
  139. }
  140. [MonoTODO ("Do something with the extra parameters")]
  141. public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
  142. {
  143. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
  144. }
  145. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
  146. {
  147. return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
  148. }
  149. [MonoTODO]
  150. public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
  151. string locationSubPath)
  152. {
  153. throw new NotImplementedException ();
  154. }
  155. public static object GetSection (string sectionName)
  156. {
  157. object section = GetWebApplicationSection (sectionName);
  158. if (section != null)
  159. return section;
  160. return ConfigurationManager.GetSection (sectionName);
  161. }
  162. [MonoTODO]
  163. public static object GetSection (string sectionName, string path)
  164. {
  165. throw new NotImplementedException ();
  166. }
  167. static _Configuration GetWebApplicationConfiguration ()
  168. {
  169. _Configuration config;
  170. if (HttpContext.Current == null
  171. || HttpContext.Current.Request == null
  172. || HttpContext.Current.Request.PhysicalApplicationPath == null)
  173. config = OpenMachineConfiguration ();
  174. else
  175. config = OpenWebConfiguration (HttpContext.Current.Request.PhysicalApplicationPath);
  176. return config;
  177. }
  178. [MonoTODO]
  179. public static object GetWebApplicationSection (string sectionName)
  180. {
  181. _Configuration config = GetWebApplicationConfiguration ();
  182. ConfigurationSection section = config.GetSection (sectionName);
  183. return section;
  184. }
  185. static _Configuration webConfiguration;
  186. static NameValueCollection appSettings;
  187. [MonoTODO]
  188. public static NameValueCollection AppSettings {
  189. get {
  190. if (appSettings == null) {
  191. if (webConfiguration == null)
  192. webConfiguration = OpenWebConfiguration ("~");
  193. AppSettingsSection section = (AppSettingsSection)webConfiguration.GetSection ("appSettings");
  194. appSettings = new NameValueCollection ();
  195. foreach (string key in section.Settings.AllKeys) {
  196. KeyValueConfigurationElement ele = section.Settings[key];
  197. appSettings.Add (ele.Key, ele.Value);
  198. }
  199. }
  200. return appSettings;
  201. }
  202. }
  203. [MonoTODO]
  204. public static ConnectionStringSettingsCollection ConnectionStrings {
  205. get {
  206. throw new NotImplementedException ();
  207. }
  208. }
  209. internal static IInternalConfigConfigurationFactory ConfigurationFactory {
  210. get { return configFactory; }
  211. }
  212. static string GetBasePath (string path)
  213. {
  214. if (path == "/")
  215. return path;
  216. string pd = HttpContext.Current.Request.MapPath (path);
  217. if (!Directory.Exists (pd)) {
  218. int i = path.LastIndexOf ('/');
  219. path = path.Substring (0, i);
  220. }
  221. while (path [path.Length - 1] == '/')
  222. path = path.Substring (0, path.Length - 1);
  223. return path;
  224. }
  225. #region stuff copied from WebConfigurationSettings
  226. #if TARGET_J2EE
  227. static private IConfigurationSystem oldConfig {
  228. get {
  229. return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
  230. }
  231. set {
  232. AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
  233. }
  234. }
  235. static private Web20DefaultConfig config {
  236. get {
  237. return (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("WebConfigurationManager.config");
  238. }
  239. set {
  240. AppDomain.CurrentDomain.SetData("WebConfigurationManager.config", value);
  241. }
  242. }
  243. #else
  244. static IConfigurationSystem oldConfig;
  245. static Web20DefaultConfig config;
  246. #endif
  247. const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
  248. static readonly object lockobj = new object ();
  249. public static void Init ()
  250. {
  251. lock (lockobj) {
  252. if (config != null)
  253. return;
  254. Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
  255. Type t = typeof (ConfigurationSettings);
  256. MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
  257. privStatic);
  258. if (changeConfig == null)
  259. throw new ConfigurationException ("Cannot find method CCS");
  260. object [] args = new object [] {settings};
  261. oldConfig = (IConfigurationSystem) changeConfig.Invoke (null, args);
  262. config = settings;
  263. config.Init ();
  264. }
  265. }
  266. }
  267. class Web20DefaultConfig : IConfigurationSystem
  268. {
  269. #if TARGET_J2EE
  270. static private Web20DefaultConfig instance {
  271. get {
  272. Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
  273. if (val == null) {
  274. val = new Web20DefaultConfig();
  275. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
  276. }
  277. return val;
  278. }
  279. set {
  280. AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
  281. }
  282. }
  283. #else
  284. static Web20DefaultConfig instance;
  285. #endif
  286. static Web20DefaultConfig ()
  287. {
  288. instance = new Web20DefaultConfig ();
  289. }
  290. public static Web20DefaultConfig GetInstance ()
  291. {
  292. return instance;
  293. }
  294. public object GetConfig (string sectionName)
  295. {
  296. return WebConfigurationManager.GetSection (sectionName);
  297. }
  298. public void Init ()
  299. {
  300. // nothing. We need a context.
  301. }
  302. }
  303. #endregion
  304. }
  305. #endif