2 // System.Web.Configuration.WebConfigurationManager.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
6 // Chris Toshok (toshok@ximian.com)
7 // Marek Habersack <mhabersack@novell.com>
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
35 using System.Collections;
36 using System.Collections.Generic;
37 using System.Collections.Specialized;
38 using System.Reflection;
43 using System.Configuration;
44 using System.Configuration.Internal;
45 using _Configuration = System.Configuration.Configuration;
46 using System.Web.Util;
47 using System.Threading;
49 namespace System.Web.Configuration {
51 public static class WebConfigurationManager
53 const int SAVE_LOCATIONS_CHECK_INTERVAL = 6000; // milliseconds
55 static readonly char[] pathTrimChars = { '/' };
56 static readonly object suppressAppReloadLock = new object ();
57 static readonly object saveLocationsCacheLock = new object ();
58 static readonly ReaderWriterLockSlim sectionCacheLock;
61 static IInternalConfigConfigurationFactory configFactory;
62 static Hashtable configurations = Hashtable.Synchronized (new Hashtable ());
63 static Dictionary <int, object> sectionCache = new Dictionary <int, object> ();
64 static Hashtable configPaths = Hashtable.Synchronized (new Hashtable ());
65 static bool suppressAppReload;
67 const string AppSettingsKey = "WebConfigurationManager.AppSettings";
69 static internal IInternalConfigConfigurationFactory configFactory
72 IInternalConfigConfigurationFactory factory = (IInternalConfigConfigurationFactory)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory");
74 lock (AppDomain.CurrentDomain){
75 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory.initialized");
76 if (initialized == null){
77 PropertyInfo prop = typeof(ConfigurationManager).GetProperty("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
79 factory = prop.GetValue(null, null) as IInternalConfigConfigurationFactory;
80 configFactory = factory;
85 return factory != null ? factory : configFactory;
88 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory", value);
89 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory.initialized", true);
93 static internal Hashtable configurations
96 Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations");
98 lock (AppDomain.CurrentDomain){
99 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations.initialized");
100 if (initialized == null){
101 table = Hashtable.Synchronized (new Hashtable (StringComparer.OrdinalIgnoreCase));
102 configurations = table;
106 return table != null ? table : configurations;
110 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations", value);
111 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations.initialized", true);
115 static Dictionary <int, object> sectionCache
119 Dictionary <int, object> sectionCache = AppDomain.CurrentDomain.GetData ("sectionCache") as Dictionary <int, object>;
120 if (sectionCache == null) {
121 sectionCache = new Dictionary <int, object> ();
122 AppDomain.CurrentDomain.SetData ("sectionCache", sectionCache);
128 AppDomain.CurrentDomain.SetData ("sectionCache", value);
132 static internal Hashtable configPaths
135 Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configPaths");
137 lock (AppDomain.CurrentDomain){
138 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configPaths.initialized");
139 if (initialized == null){
140 table = Hashtable.Synchronized (new Hashtable (StringComparer.OrdinalIgnoreCase));
145 return table != null ? table : configPaths;
149 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configPaths", value);
150 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configPaths.initialized", true);
154 static Dictionary <string, DateTime> saveLocationsCache;
155 static Timer saveLocationsTimer;
157 static ArrayList extra_assemblies = null;
158 static internal ArrayList ExtraAssemblies {
160 if (extra_assemblies == null)
161 extra_assemblies = new ArrayList();
162 return extra_assemblies;
166 static bool hasConfigErrors = false;
167 static object hasConfigErrorsLock = new object ();
168 static internal bool HasConfigErrors {
170 lock (hasConfigErrorsLock) {
171 return hasConfigErrors;
176 static WebConfigurationManager ()
178 configFactory = ConfigurationManager.ConfigurationFactory;
179 _Configuration.SaveStart += ConfigurationSaveHandler;
180 _Configuration.SaveEnd += ConfigurationSaveHandler;
182 // Part of fix for bug #491531
183 Type type = Type.GetType ("System.Configuration.CustomizableFileSettingsProvider, System", false);
185 FieldInfo fi = type.GetField ("webConfigurationFileMapType", BindingFlags.Static | BindingFlags.NonPublic);
186 if (fi != null && fi.FieldType == Type.GetType ("System.Type"))
187 fi.SetValue (null, typeof (ApplicationSettingsConfigurationFileMap));
190 sectionCacheLock = new ReaderWriterLockSlim ();
193 static void ReenableWatcherOnConfigLocation (object state)
195 string path = state as string;
196 if (String.IsNullOrEmpty (path))
200 lock (saveLocationsCacheLock) {
201 if (!saveLocationsCache.TryGetValue (path, out lastWrite))
202 lastWrite = DateTime.MinValue;
205 DateTime now = DateTime.Now;
206 if (lastWrite == DateTime.MinValue || now.Subtract (lastWrite).TotalMilliseconds >= SAVE_LOCATIONS_CHECK_INTERVAL) {
207 saveLocationsTimer.Dispose ();
208 saveLocationsTimer = null;
209 HttpApplicationFactory.EnableWatcher (VirtualPathUtility.RemoveTrailingSlash (HttpRuntime.AppDomainAppPath), "?eb.?onfig");
211 saveLocationsTimer.Change (SAVE_LOCATIONS_CHECK_INTERVAL, SAVE_LOCATIONS_CHECK_INTERVAL);
214 static void ConfigurationSaveHandler (_Configuration sender, ConfigurationSaveEventArgs args)
218 sectionCacheLock.EnterWriteLock ();
220 sectionCache.Clear ();
223 sectionCacheLock.ExitWriteLock ();
226 lock (suppressAppReloadLock) {
227 string rootConfigPath = WebConfigurationHost.GetWebConfigFileName (HttpRuntime.AppDomainAppPath);
228 if (String.Compare (args.StreamPath, rootConfigPath, StringComparison.OrdinalIgnoreCase) == 0) {
229 SuppressAppReload (args.Start);
231 HttpApplicationFactory.DisableWatcher (VirtualPathUtility.RemoveTrailingSlash (HttpRuntime.AppDomainAppPath), "?eb.?onfig");
233 lock (saveLocationsCacheLock) {
234 if (saveLocationsCache == null)
235 saveLocationsCache = new Dictionary <string, DateTime> (StringComparer.Ordinal);
236 if (saveLocationsCache.ContainsKey (rootConfigPath))
237 saveLocationsCache [rootConfigPath] = DateTime.Now;
239 saveLocationsCache.Add (rootConfigPath, DateTime.Now);
241 if (saveLocationsTimer == null)
242 saveLocationsTimer = new Timer (ReenableWatcherOnConfigLocation,
244 SAVE_LOCATIONS_CHECK_INTERVAL,
245 SAVE_LOCATIONS_CHECK_INTERVAL);
252 public static _Configuration OpenMachineConfiguration ()
254 return ConfigurationManager.OpenMachineConfiguration ();
257 [MonoLimitation ("locationSubPath is not handled")]
258 public static _Configuration OpenMachineConfiguration (string locationSubPath)
260 return OpenMachineConfiguration ();
263 [MonoLimitation("Mono does not support remote configuration")]
264 public static _Configuration OpenMachineConfiguration (string locationSubPath,
268 return OpenMachineConfiguration (locationSubPath);
270 throw new NotSupportedException ("Mono doesn't support remote configuration");
273 [MonoLimitation("Mono does not support remote configuration")]
274 public static _Configuration OpenMachineConfiguration (string locationSubPath,
279 return OpenMachineConfiguration (locationSubPath);
280 throw new NotSupportedException ("Mono doesn't support remote configuration");
283 [MonoLimitation("Mono does not support remote configuration")]
284 public static _Configuration OpenMachineConfiguration (string locationSubPath,
290 return OpenMachineConfiguration (locationSubPath);
291 throw new NotSupportedException ("Mono doesn't support remote configuration");
294 public static _Configuration OpenWebConfiguration (string path)
296 return OpenWebConfiguration (path, null, null, null, null, null);
299 public static _Configuration OpenWebConfiguration (string path, string site)
301 return OpenWebConfiguration (path, site, null, null, null, null);
304 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
306 return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
309 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
311 return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
314 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
316 return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
319 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
321 return OpenWebConfiguration (path, site, locationSubPath, server, null, null, false);
324 static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password, bool fweb)
326 if (String.IsNullOrEmpty (path))
329 if (!fweb && !String.IsNullOrEmpty (path))
330 path = FindWebConfig (path);
332 string confKey = path + site + locationSubPath + server + userName + password;
333 _Configuration conf = null;
334 conf = (_Configuration) configurations [confKey];
337 conf = ConfigurationFactory.Create (typeof (WebConfigurationHost), null, path, site, locationSubPath, server, userName, password);
338 configurations [confKey] = conf;
339 } catch (Exception ex) {
340 lock (hasConfigErrorsLock) {
341 hasConfigErrors = true;
349 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
351 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
354 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
356 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
359 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
361 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
364 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
366 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
369 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
370 string locationSubPath)
372 return OpenMappedMachineConfiguration (fileMap);
375 internal static object SafeGetSection (string sectionName, Type configSectionType)
378 return GetSection (sectionName);
379 } catch (Exception) {
380 if (configSectionType != null)
381 return Activator.CreateInstance (configSectionType);
386 internal static object SafeGetSection (string sectionName, string path, Type configSectionType)
389 return GetSection (sectionName, path);
390 } catch (Exception) {
391 if (configSectionType != null)
392 return Activator.CreateInstance (configSectionType);
397 public static object GetSection (string sectionName)
399 HttpContext context = HttpContext.Current;
400 return GetSection (sectionName, GetCurrentPath (context), context);
403 public static object GetSection (string sectionName, string path)
405 return GetSection (sectionName, path, HttpContext.Current);
408 static bool LookUpLocation (string relativePath, ref _Configuration defaultConfiguration)
410 if (String.IsNullOrEmpty (relativePath))
413 _Configuration cnew = defaultConfiguration.FindLocationConfiguration (relativePath, defaultConfiguration);
414 if (cnew == defaultConfiguration)
417 defaultConfiguration = cnew;
421 internal static object GetSection (string sectionName, string path, HttpContext context)
423 if (String.IsNullOrEmpty (sectionName))
426 _Configuration c = OpenWebConfiguration (path, null, null, null, null, null, false);
427 string configPath = c.ConfigPath;
428 int baseCacheKey = 0;
430 bool pathPresent = !String.IsNullOrEmpty (path);
431 string locationPath = null;
435 locationPath = "location_" + path;
437 baseCacheKey = sectionName.GetHashCode ();
438 if (configPath != null)
439 baseCacheKey ^= configPath.GetHashCode ();
442 sectionCacheLock.EnterReadLock ();
447 cacheKey = baseCacheKey ^ locationPath.GetHashCode ();
448 if (sectionCache.TryGetValue (cacheKey, out o))
451 cacheKey = baseCacheKey ^ path.GetHashCode ();
452 if (sectionCache.TryGetValue (cacheKey, out o))
456 if (sectionCache.TryGetValue (baseCacheKey, out o))
460 sectionCacheLock.ExitReadLock ();
463 string cachePath = null;
467 if (VirtualPathUtility.IsRooted (path)) {
469 relPath = path.Substring (2);
470 else if (path [0] == '/')
471 relPath = path.Substring (1);
477 HttpRequest req = context != null ? context.Request : null;
479 string vdir = VirtualPathUtility.GetDirectory (req.PathNoValidation);
481 vdir = vdir.TrimEnd (pathTrimChars);
482 if (String.Compare (c.ConfigPath, vdir, StringComparison.Ordinal) != 0 && LookUpLocation (vdir.Trim (pathTrimChars), ref c))
487 if (LookUpLocation (relPath, ref c))
488 cachePath = locationPath;
493 ConfigurationSection section = c.GetSection (sectionName);
498 object value = get_runtime_object.Invoke (section, new object [0]);
499 if (String.CompareOrdinal ("appSettings", sectionName) == 0) {
500 NameValueCollection collection;
501 collection = new KeyValueMergedCollection (HttpContext.Current, (NameValueCollection) value);
506 object value = SettingsMappingManager.MapSection (get_runtime_object.Invoke (section, new object [0]));
511 if (cachePath != null)
512 cacheKey = baseCacheKey ^ cachePath.GetHashCode ();
514 cacheKey = baseCacheKey;
516 AddSectionToCache (cacheKey, value);
520 static string MapPath (HttpRequest req, string virtualPath)
523 return req.MapPath (virtualPath);
525 string appRoot = HttpRuntime.AppDomainAppVirtualPath;
526 if (!String.IsNullOrEmpty (appRoot) && virtualPath.StartsWith (appRoot, StringComparison.Ordinal)) {
527 if (String.Compare (virtualPath, appRoot, StringComparison.Ordinal) == 0)
528 return HttpRuntime.AppDomainAppPath;
529 return UrlUtils.Combine (HttpRuntime.AppDomainAppPath, virtualPath.Substring (appRoot.Length));
535 static string GetParentDir (string rootPath, string curPath)
537 int len = curPath.Length - 1;
538 if (len > 0 && curPath [len] == '/')
539 curPath = curPath.Substring (0, len);
541 if (String.Compare (curPath, rootPath, StringComparison.Ordinal) == 0)
544 int idx = curPath.LastIndexOf ('/');
551 return curPath.Substring (0, idx);
554 internal static string FindWebConfig (string path)
556 if (String.IsNullOrEmpty (path))
560 if (path [path.Length - 1] == '/')
563 dir = VirtualPathUtility.GetDirectory (path, false);
568 string curPath = configPaths [dir] as string;
572 HttpContext ctx = HttpContext.Current;
573 HttpRequest req = ctx != null ? ctx.Request : null;
578 string rootPath = HttpRuntime.AppDomainAppVirtualPath;
581 while (String.Compare (curPath, rootPath, StringComparison.Ordinal) != 0) {
582 physPath = MapPath (req, curPath);
583 if (physPath == null) {
588 if (WebConfigurationHost.GetWebConfigFileName (physPath) != null)
591 curPath = GetParentDir (rootPath, curPath);
592 if (curPath == null || curPath == "~") {
598 configPaths [dir] = curPath;
602 static string GetCurrentPath (HttpContext ctx)
604 HttpRequest req = ctx != null ? ctx.Request : null;
605 return req != null ? req.PathNoValidation : HttpRuntime.AppDomainAppVirtualPath;
608 internal static bool SuppressAppReload (bool newValue)
612 lock (suppressAppReloadLock) {
613 ret = suppressAppReload;
614 suppressAppReload = newValue;
620 internal static void RemoveConfigurationFromCache (HttpContext ctx)
622 configurations.Remove (GetCurrentPath (ctx));
625 #if TARGET_J2EE || MONOWEB_DEP
626 readonly static MethodInfo get_runtime_object = typeof (ConfigurationSection).GetMethod ("GetRuntimeObject", BindingFlags.NonPublic | BindingFlags.Instance);
629 public static object GetWebApplicationSection (string sectionName)
631 HttpContext ctx = HttpContext.Current;
632 HttpRequest req = ctx != null ? ctx.Request : null;
633 string applicationPath = req != null ? req.ApplicationPath : null;
634 return GetSection (sectionName, String.IsNullOrEmpty (applicationPath) ? String.Empty : applicationPath);
637 public static NameValueCollection AppSettings {
638 get { return ConfigurationManager.AppSettings; }
641 public static ConnectionStringSettingsCollection ConnectionStrings {
642 get { return ConfigurationManager.ConnectionStrings; }
645 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
646 get { return configFactory; }
649 static void AddSectionToCache (int key, object section)
651 object cachedSection;
655 sectionCacheLock.EnterUpgradeableReadLock ();
658 if (sectionCache.TryGetValue (key, out cachedSection) && cachedSection != null)
661 bool innerLocked = false;
663 sectionCacheLock.EnterWriteLock ();
665 sectionCache.Add (key, section);
668 sectionCacheLock.ExitWriteLock ();
672 sectionCacheLock.ExitUpgradeableReadLock ();
676 #region stuff copied from WebConfigurationSettings
678 static internal IConfigurationSystem oldConfig {
680 return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
683 AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
687 static Web20DefaultConfig config {
689 return (Web20DefaultConfig) AppDomain.CurrentDomain.GetData ("Web20DefaultConfig.config");
692 AppDomain.CurrentDomain.SetData ("Web20DefaultConfig.config", value);
696 static IInternalConfigSystem configSystem {
698 return (IInternalConfigSystem) AppDomain.CurrentDomain.GetData ("IInternalConfigSystem.configSystem");
701 AppDomain.CurrentDomain.SetData ("IInternalConfigSystem.configSystem", value);
705 static internal IConfigurationSystem oldConfig;
706 static Web20DefaultConfig config;
707 //static IInternalConfigSystem configSystem;
709 const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
710 static readonly object lockobj = new object ();
712 internal static void Init ()
718 /* deal with the ConfigurationSettings stuff */
720 Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
721 Type t = typeof (ConfigurationSettings);
722 MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
725 if (changeConfig == null)
726 throw new ConfigurationException ("Cannot find method CCS");
728 object [] args = new object [] {settings};
729 oldConfig = (IConfigurationSystem)changeConfig.Invoke (null, args);
735 /* deal with the ConfigurationManager stuff */
737 HttpConfigurationSystem system = new HttpConfigurationSystem ();
738 Type t = typeof (ConfigurationManager);
739 MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
742 if (changeConfig == null)
743 throw new ConfigurationException ("Cannot find method CCS");
745 object [] args = new object [] {system};
746 changeConfig.Invoke (null, args);
747 //configSystem = system;
753 class Web20DefaultConfig : IConfigurationSystem
756 static Web20DefaultConfig instance {
758 Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
760 val = new Web20DefaultConfig();
761 AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
766 AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
770 static Web20DefaultConfig instance;
773 static Web20DefaultConfig ()
775 instance = new Web20DefaultConfig ();
778 public static Web20DefaultConfig GetInstance ()
783 public object GetConfig (string sectionName)
785 object o = WebConfigurationManager.GetWebApplicationSection (sectionName);
787 if (o == null || o is IgnoreSection) {
788 /* this can happen when the section
789 * handler doesn't subclass from
790 * ConfigurationSection. let's be
791 * nice and try to load it using the
792 * 1.x style routines in case there's
793 * a 1.x section handler registered
796 object o1 = WebConfigurationManager.oldConfig.GetConfig (sectionName);
806 // nothing. We need a context.