2 // System.Web.Configuration.WebConfigurationManager.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Reflection;
36 using System.Configuration;
37 using System.Configuration.Internal;
38 using _Configuration = System.Configuration.Configuration;
40 namespace System.Web.Configuration {
42 public abstract class WebConfigurationManager
44 static IInternalConfigConfigurationFactory configFactory;
45 static Hashtable configurations = new Hashtable ();
47 static WebConfigurationManager ()
49 PropertyInfo prop = typeof(ConfigurationManager).GetProperty ("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
51 configFactory = prop.GetValue (null, null) as IInternalConfigConfigurationFactory;
54 WebConfigurationManager ()
58 public static _Configuration OpenWebConfiguration (string path)
60 return OpenWebConfiguration (path, null, null, null, IntPtr.Zero, null);
63 public static _Configuration OpenWebConfiguration (string path, string site)
65 return OpenWebConfiguration (path, site, null, null, IntPtr.Zero, null);
68 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
70 return OpenWebConfiguration (path, site, locationSubPath, null, IntPtr.Zero, null);
73 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
75 return OpenWebConfiguration (path, site, locationSubPath, server, userToken, null);
78 [MonoTODO ("Do something with the extra parameters")]
79 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken, string password)
81 string basePath = GetBasePath (path);
84 lock (configurations) {
85 conf = (_Configuration) configurations [basePath];
87 conf = ConfigurationFactory.Create (typeof(WebConfigurationHost), null, path, site, locationSubPath, server, userToken, password);
88 configurations [basePath] = conf;
91 if (basePath.Length < path.Length) {
93 // If the path has a file name, look for a location specific configuration
95 int dif = path.Length - basePath.Length;
96 string file = path.Substring (path.Length - dif);
98 while (i < file.Length && file [i] == '/')
101 file = file.Substring (i);
103 if (file.Length != 0) {
104 foreach (ConfigurationLocation loc in conf.Locations) {
105 if (loc.Path == file)
106 return loc.OpenConfiguration ();
113 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
115 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
118 [MonoTODO ("Do something with the extra parameters")]
119 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
121 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
124 [MonoTODO ("Do something with the extra parameters")]
125 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
127 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
130 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
132 return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
135 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
136 get { return configFactory; }
139 static string GetBasePath (string path)
141 string pd = HttpContext.Current.Request.MapPath (path);
143 if (!Directory.Exists (pd)) {
144 int i = path.LastIndexOf ('/');
145 path = path.Substring (0, i);
148 while (path [path.Length - 1] == '/')
149 path = path.Substring (0, path.Length - 1);