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 sealed class ConfigPath
56 public bool InAnotherApp;
58 public ConfigPath (string path, bool inAnotherApp)
61 this.InAnotherApp = inAnotherApp;
65 const int SAVE_LOCATIONS_CHECK_INTERVAL = 6000; // milliseconds
67 static readonly char[] pathTrimChars = { '/' };
68 static readonly object suppressAppReloadLock = new object ();
69 static readonly object saveLocationsCacheLock = new object ();
70 static readonly ReaderWriterLockSlim sectionCacheLock;
73 static IInternalConfigConfigurationFactory configFactory;
74 static Hashtable configurations = Hashtable.Synchronized (new Hashtable ());
75 static Dictionary <int, object> sectionCache = new Dictionary <int, object> ();
76 static Hashtable configPaths = Hashtable.Synchronized (new Hashtable ());
77 static bool suppressAppReload;
79 const string AppSettingsKey = "WebConfigurationManager.AppSettings";
81 static internal IInternalConfigConfigurationFactory configFactory
84 IInternalConfigConfigurationFactory factory = (IInternalConfigConfigurationFactory)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory");
86 lock (AppDomain.CurrentDomain){
87 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory.initialized");
88 if (initialized == null){
89 PropertyInfo prop = typeof(ConfigurationManager).GetProperty("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
91 factory = prop.GetValue(null, null) as IInternalConfigConfigurationFactory;
92 configFactory = factory;
97 return factory != null ? factory : configFactory;
100 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory", value);
101 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory.initialized", true);
105 static internal Hashtable configurations
108 Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations");
110 lock (AppDomain.CurrentDomain){
111 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations.initialized");
112 if (initialized == null){
113 table = Hashtable.Synchronized (new Hashtable (StringComparer.OrdinalIgnoreCase));
114 configurations = table;
118 return table != null ? table : configurations;
122 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations", value);
123 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations.initialized", true);
127 static Dictionary <int, object> sectionCache
131 Dictionary <int, object> sectionCache = AppDomain.CurrentDomain.GetData ("sectionCache") as Dictionary <int, object>;
132 if (sectionCache == null) {
133 sectionCache = new Dictionary <int, object> ();
134 AppDomain.CurrentDomain.SetData ("sectionCache", sectionCache);
140 AppDomain.CurrentDomain.SetData ("sectionCache", value);
144 static internal Hashtable configPaths
147 Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configPaths");
149 lock (AppDomain.CurrentDomain){
150 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configPaths.initialized");
151 if (initialized == null){
152 table = Hashtable.Synchronized (new Hashtable (StringComparer.OrdinalIgnoreCase));
157 return table != null ? table : configPaths;
161 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configPaths", value);
162 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configPaths.initialized", true);
166 static Dictionary <string, DateTime> saveLocationsCache;
167 static Timer saveLocationsTimer;
169 static ArrayList extra_assemblies = null;
170 static internal ArrayList ExtraAssemblies {
172 if (extra_assemblies == null)
173 extra_assemblies = new ArrayList();
174 return extra_assemblies;
178 static bool hasConfigErrors = false;
179 static object hasConfigErrorsLock = new object ();
180 static internal bool HasConfigErrors {
182 lock (hasConfigErrorsLock) {
183 return hasConfigErrors;
188 static WebConfigurationManager ()
190 configFactory = ConfigurationManager.ConfigurationFactory;
191 _Configuration.SaveStart += ConfigurationSaveHandler;
192 _Configuration.SaveEnd += ConfigurationSaveHandler;
194 // Part of fix for bug #491531
195 Type type = Type.GetType ("System.Configuration.CustomizableFileSettingsProvider, System", false);
197 FieldInfo fi = type.GetField ("webConfigurationFileMapType", BindingFlags.Static | BindingFlags.NonPublic);
198 if (fi != null && fi.FieldType == Type.GetType ("System.Type"))
199 fi.SetValue (null, typeof (ApplicationSettingsConfigurationFileMap));
202 sectionCacheLock = new ReaderWriterLockSlim ();
205 static void ReenableWatcherOnConfigLocation (object state)
207 string path = state as string;
208 if (String.IsNullOrEmpty (path))
212 lock (saveLocationsCacheLock) {
213 if (!saveLocationsCache.TryGetValue (path, out lastWrite))
214 lastWrite = DateTime.MinValue;
217 DateTime now = DateTime.Now;
218 if (lastWrite == DateTime.MinValue || now.Subtract (lastWrite).TotalMilliseconds >= SAVE_LOCATIONS_CHECK_INTERVAL) {
219 saveLocationsTimer.Dispose ();
220 saveLocationsTimer = null;
221 HttpApplicationFactory.EnableWatcher (VirtualPathUtility.RemoveTrailingSlash (HttpRuntime.AppDomainAppPath), "?eb.?onfig");
223 saveLocationsTimer.Change (SAVE_LOCATIONS_CHECK_INTERVAL, SAVE_LOCATIONS_CHECK_INTERVAL);
226 static void ConfigurationSaveHandler (_Configuration sender, ConfigurationSaveEventArgs args)
230 sectionCacheLock.EnterWriteLock ();
232 sectionCache.Clear ();
235 sectionCacheLock.ExitWriteLock ();
238 lock (suppressAppReloadLock) {
239 string rootConfigPath = WebConfigurationHost.GetWebConfigFileName (HttpRuntime.AppDomainAppPath);
240 if (String.Compare (args.StreamPath, rootConfigPath, StringComparison.OrdinalIgnoreCase) == 0) {
241 SuppressAppReload (args.Start);
243 HttpApplicationFactory.DisableWatcher (VirtualPathUtility.RemoveTrailingSlash (HttpRuntime.AppDomainAppPath), "?eb.?onfig");
245 lock (saveLocationsCacheLock) {
246 if (saveLocationsCache == null)
247 saveLocationsCache = new Dictionary <string, DateTime> (StringComparer.Ordinal);
248 if (saveLocationsCache.ContainsKey (rootConfigPath))
249 saveLocationsCache [rootConfigPath] = DateTime.Now;
251 saveLocationsCache.Add (rootConfigPath, DateTime.Now);
253 if (saveLocationsTimer == null)
254 saveLocationsTimer = new Timer (ReenableWatcherOnConfigLocation,
256 SAVE_LOCATIONS_CHECK_INTERVAL,
257 SAVE_LOCATIONS_CHECK_INTERVAL);
264 public static _Configuration OpenMachineConfiguration ()
266 return ConfigurationManager.OpenMachineConfiguration ();
269 [MonoLimitation ("locationSubPath is not handled")]
270 public static _Configuration OpenMachineConfiguration (string locationSubPath)
272 return OpenMachineConfiguration ();
275 [MonoLimitation("Mono does not support remote configuration")]
276 public static _Configuration OpenMachineConfiguration (string locationSubPath,
280 return OpenMachineConfiguration (locationSubPath);
282 throw new NotSupportedException ("Mono doesn't support remote configuration");
285 [MonoLimitation("Mono does not support remote configuration")]
286 public static _Configuration OpenMachineConfiguration (string locationSubPath,
291 return OpenMachineConfiguration (locationSubPath);
292 throw new NotSupportedException ("Mono doesn't support remote configuration");
295 [MonoLimitation("Mono does not support remote configuration")]
296 public static _Configuration OpenMachineConfiguration (string locationSubPath,
302 return OpenMachineConfiguration (locationSubPath);
303 throw new NotSupportedException ("Mono doesn't support remote configuration");
306 public static _Configuration OpenWebConfiguration (string path)
308 return OpenWebConfiguration (path, null, null, null, null, null);
311 public static _Configuration OpenWebConfiguration (string path, string site)
313 return OpenWebConfiguration (path, site, null, null, null, null);
316 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
318 return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
321 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
323 return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
326 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
328 return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
331 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
333 return OpenWebConfiguration (path, site, locationSubPath, server, null, null, false);
336 static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password, bool fweb)
338 if (String.IsNullOrEmpty (path))
341 bool inAnotherApp = false;
342 if (!fweb && !String.IsNullOrEmpty (path))
343 path = FindWebConfig (path, out inAnotherApp);
345 string confKey = path + site + locationSubPath + server + userName + password;
346 _Configuration conf = null;
347 conf = (_Configuration) configurations [confKey];
350 conf = ConfigurationFactory.Create (typeof (WebConfigurationHost), null, path, site, locationSubPath, server, userName, password, inAnotherApp);
351 configurations [confKey] = conf;
352 } catch (Exception ex) {
353 lock (hasConfigErrorsLock) {
354 hasConfigErrors = true;
362 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
364 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
367 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
369 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
372 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
374 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
377 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
379 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
382 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
383 string locationSubPath)
385 return OpenMappedMachineConfiguration (fileMap);
388 internal static object SafeGetSection (string sectionName, Type configSectionType)
391 return GetSection (sectionName);
392 } catch (Exception) {
393 if (configSectionType != null)
394 return Activator.CreateInstance (configSectionType);
399 internal static object SafeGetSection (string sectionName, string path, Type configSectionType)
402 return GetSection (sectionName, path);
403 } catch (Exception) {
404 if (configSectionType != null)
405 return Activator.CreateInstance (configSectionType);
410 public static object GetSection (string sectionName)
412 HttpContext context = HttpContext.Current;
413 return GetSection (sectionName, GetCurrentPath (context), context);
416 public static object GetSection (string sectionName, string path)
418 return GetSection (sectionName, path, HttpContext.Current);
421 static bool LookUpLocation (string relativePath, ref _Configuration defaultConfiguration)
423 if (String.IsNullOrEmpty (relativePath))
426 _Configuration cnew = defaultConfiguration.FindLocationConfiguration (relativePath, defaultConfiguration);
427 if (cnew == defaultConfiguration)
430 defaultConfiguration = cnew;
434 internal static object GetSection (string sectionName, string path, HttpContext context)
436 if (String.IsNullOrEmpty (sectionName))
439 _Configuration c = OpenWebConfiguration (path, null, null, null, null, null, false);
440 string configPath = c.ConfigPath;
441 int baseCacheKey = 0;
443 bool pathPresent = !String.IsNullOrEmpty (path);
444 string locationPath = null;
448 locationPath = "location_" + path;
450 baseCacheKey = sectionName.GetHashCode ();
451 if (configPath != null)
452 baseCacheKey ^= configPath.GetHashCode ();
455 sectionCacheLock.EnterReadLock ();
460 cacheKey = baseCacheKey ^ locationPath.GetHashCode ();
461 if (sectionCache.TryGetValue (cacheKey, out o))
464 cacheKey = baseCacheKey ^ path.GetHashCode ();
465 if (sectionCache.TryGetValue (cacheKey, out o))
469 if (sectionCache.TryGetValue (baseCacheKey, out o))
473 sectionCacheLock.ExitReadLock ();
476 string cachePath = null;
480 if (VirtualPathUtility.IsRooted (path)) {
482 relPath = path.Length > 1 ? path.Substring (2) : String.Empty;
483 else if (path [0] == '/')
484 relPath = path.Substring (1);
490 HttpRequest req = context != null ? context.Request : null;
492 string vdir = VirtualPathUtility.GetDirectory (req.PathNoValidation);
494 vdir = vdir.TrimEnd (pathTrimChars);
495 if (String.Compare (c.ConfigPath, vdir, StringComparison.Ordinal) != 0 && LookUpLocation (vdir.Trim (pathTrimChars), ref c))
500 if (LookUpLocation (relPath, ref c))
501 cachePath = locationPath;
506 ConfigurationSection section = c.GetSection (sectionName);
511 object value = get_runtime_object.Invoke (section, new object [0]);
512 if (String.CompareOrdinal ("appSettings", sectionName) == 0) {
513 NameValueCollection collection;
514 collection = new KeyValueMergedCollection (HttpContext.Current, (NameValueCollection) value);
519 object value = SettingsMappingManager.MapSection (get_runtime_object.Invoke (section, new object [0]));
524 if (cachePath != null)
525 cacheKey = baseCacheKey ^ cachePath.GetHashCode ();
527 cacheKey = baseCacheKey;
529 AddSectionToCache (cacheKey, value);
533 static string MapPath (HttpRequest req, string virtualPath)
536 return req.MapPath (virtualPath);
538 string appRoot = HttpRuntime.AppDomainAppVirtualPath;
539 if (!String.IsNullOrEmpty (appRoot) && virtualPath.StartsWith (appRoot, StringComparison.Ordinal)) {
540 if (String.Compare (virtualPath, appRoot, StringComparison.Ordinal) == 0)
541 return HttpRuntime.AppDomainAppPath;
542 return UrlUtils.Combine (HttpRuntime.AppDomainAppPath, virtualPath.Substring (appRoot.Length));
548 static string GetParentDir (string rootPath, string curPath)
550 int len = curPath.Length - 1;
551 if (len > 0 && curPath [len] == '/')
552 curPath = curPath.Substring (0, len);
554 if (String.Compare (curPath, rootPath, StringComparison.Ordinal) == 0)
557 int idx = curPath.LastIndexOf ('/');
564 return curPath.Substring (0, idx);
567 internal static string FindWebConfig (string path)
571 return FindWebConfig (path, out dummy);
574 internal static string FindWebConfig (string path, out bool inAnotherApp)
576 inAnotherApp = false;
578 if (String.IsNullOrEmpty (path))
581 string rootPath = HttpRuntime.AppDomainAppVirtualPath;
583 curPath = configPaths [path] as ConfigPath;
584 if (curPath != null) {
585 inAnotherApp = curPath.InAnotherApp;
589 HttpContext ctx = HttpContext.Current;
590 HttpRequest req = ctx != null ? ctx.Request : null;
591 string physPath = req != null ? VirtualPathUtility.AppendTrailingSlash (MapPath (req, path)) : null;
593 if (physPath != null && !physPath.StartsWith (HttpRuntime.AppDomainAppPath, StringComparison.Ordinal))
597 if (inAnotherApp || path [path.Length - 1] == '/')
600 dir = VirtualPathUtility.GetDirectory (path, false);
605 curPath = configPaths [dir] as ConfigPath;
606 if (curPath != null) {
607 inAnotherApp = curPath.InAnotherApp;
614 curPath = new ConfigPath (path, inAnotherApp);
615 while (String.Compare (curPath.Path, rootPath, StringComparison.Ordinal) != 0) {
616 physPath = MapPath (req, curPath.Path);
617 if (physPath == null) {
618 curPath.Path = rootPath;
622 if (WebConfigurationHost.GetWebConfigFileName (physPath) != null)
625 curPath.Path = GetParentDir (rootPath, curPath.Path);
626 if (curPath.Path == null || curPath.Path == "~") {
627 curPath.Path = rootPath;
632 if (String.Compare (curPath.Path, path, StringComparison.Ordinal) != 0)
633 configPaths [path] = curPath;
635 configPaths [dir] = curPath;
640 static string GetCurrentPath (HttpContext ctx)
642 HttpRequest req = ctx != null ? ctx.Request : null;
643 return req != null ? req.PathNoValidation : HttpRuntime.AppDomainAppVirtualPath;
646 internal static bool SuppressAppReload (bool newValue)
650 lock (suppressAppReloadLock) {
651 ret = suppressAppReload;
652 suppressAppReload = newValue;
658 internal static void RemoveConfigurationFromCache (HttpContext ctx)
660 configurations.Remove (GetCurrentPath (ctx));
663 #if TARGET_J2EE || MONOWEB_DEP
664 readonly static MethodInfo get_runtime_object = typeof (ConfigurationSection).GetMethod ("GetRuntimeObject", BindingFlags.NonPublic | BindingFlags.Instance);
667 public static object GetWebApplicationSection (string sectionName)
669 HttpContext ctx = HttpContext.Current;
670 HttpRequest req = ctx != null ? ctx.Request : null;
671 string applicationPath = req != null ? req.ApplicationPath : null;
672 return GetSection (sectionName, String.IsNullOrEmpty (applicationPath) ? String.Empty : applicationPath);
675 public static NameValueCollection AppSettings {
676 get { return ConfigurationManager.AppSettings; }
679 public static ConnectionStringSettingsCollection ConnectionStrings {
680 get { return ConfigurationManager.ConnectionStrings; }
683 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
684 get { return configFactory; }
687 static void AddSectionToCache (int key, object section)
689 object cachedSection;
693 sectionCacheLock.EnterUpgradeableReadLock ();
696 if (sectionCache.TryGetValue (key, out cachedSection) && cachedSection != null)
699 bool innerLocked = false;
701 sectionCacheLock.EnterWriteLock ();
703 sectionCache.Add (key, section);
706 sectionCacheLock.ExitWriteLock ();
710 sectionCacheLock.ExitUpgradeableReadLock ();
714 #region stuff copied from WebConfigurationSettings
716 static internal IConfigurationSystem oldConfig {
718 return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
721 AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
725 static Web20DefaultConfig config {
727 return (Web20DefaultConfig) AppDomain.CurrentDomain.GetData ("Web20DefaultConfig.config");
730 AppDomain.CurrentDomain.SetData ("Web20DefaultConfig.config", value);
734 static IInternalConfigSystem configSystem {
736 return (IInternalConfigSystem) AppDomain.CurrentDomain.GetData ("IInternalConfigSystem.configSystem");
739 AppDomain.CurrentDomain.SetData ("IInternalConfigSystem.configSystem", value);
743 static internal IConfigurationSystem oldConfig;
744 static Web20DefaultConfig config;
745 //static IInternalConfigSystem configSystem;
747 const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
748 static readonly object lockobj = new object ();
750 internal static void Init ()
756 /* deal with the ConfigurationSettings stuff */
758 Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
759 Type t = typeof (ConfigurationSettings);
760 MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
763 if (changeConfig == null)
764 throw new ConfigurationException ("Cannot find method CCS");
766 object [] args = new object [] {settings};
767 oldConfig = (IConfigurationSystem)changeConfig.Invoke (null, args);
773 /* deal with the ConfigurationManager stuff */
775 HttpConfigurationSystem system = new HttpConfigurationSystem ();
776 Type t = typeof (ConfigurationManager);
777 MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
780 if (changeConfig == null)
781 throw new ConfigurationException ("Cannot find method CCS");
783 object [] args = new object [] {system};
784 changeConfig.Invoke (null, args);
785 //configSystem = system;
791 class Web20DefaultConfig : IConfigurationSystem
794 static Web20DefaultConfig instance {
796 Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
798 val = new Web20DefaultConfig();
799 AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
804 AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
808 static Web20DefaultConfig instance;
811 static Web20DefaultConfig ()
813 instance = new Web20DefaultConfig ();
816 public static Web20DefaultConfig GetInstance ()
821 public object GetConfig (string sectionName)
823 object o = WebConfigurationManager.GetWebApplicationSection (sectionName);
825 if (o == null || o is IgnoreSection) {
826 /* this can happen when the section
827 * handler doesn't subclass from
828 * ConfigurationSection. let's be
829 * nice and try to load it using the
830 * 1.x style routines in case there's
831 * a 1.x section handler registered
834 object o1 = WebConfigurationManager.oldConfig.GetConfig (sectionName);
844 // nothing. We need a context.