2005-06-26 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration / WebConfigurationManager.cs
1 //
2 // System.Web.Configuration.WebConfigurationManager.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
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:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
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.
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
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
40 namespace System.Web.Configuration {
41
42         public abstract class WebConfigurationManager
43         {
44                 static IInternalConfigConfigurationFactory configFactory;
45                 static Hashtable configurations = new Hashtable ();
46                 
47                 static WebConfigurationManager ()
48                 {
49                         PropertyInfo prop = typeof(ConfigurationManager).GetProperty ("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
50                         if (prop != null)
51                                 configFactory = prop.GetValue (null, null) as IInternalConfigConfigurationFactory;
52                 }
53                 
54                 WebConfigurationManager ()
55                 {
56                 }
57                 
58                 public static _Configuration OpenWebConfiguration (string path)
59                 {
60                         return OpenWebConfiguration (path, null, null, null, IntPtr.Zero, null);
61                 }
62                 
63                 public static _Configuration OpenWebConfiguration (string path, string site)
64                 {
65                         return OpenWebConfiguration (path, site, null, null, IntPtr.Zero, null);
66                 }
67                 
68                 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
69                 {
70                         return OpenWebConfiguration (path, site, locationSubPath, null, IntPtr.Zero, null);
71                 }
72                 
73                 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
74                 {
75                         return OpenWebConfiguration (path, site, locationSubPath, server, userToken, null);
76                 }
77                 
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)
80                 {
81                         string basePath = GetBasePath (path);
82                         _Configuration conf;
83                         
84                         lock (configurations) {
85                                 conf = (_Configuration) configurations [basePath];
86                                 if (conf == null) {
87                                         conf = ConfigurationFactory.Create (typeof(WebConfigurationHost), null, path, site, locationSubPath, server, userToken, password);
88                                         configurations [basePath] = conf;
89                                 }
90                         }
91                         if (basePath.Length < path.Length) {
92                         
93                                 // If the path has a file name, look for a location specific configuration
94                                 
95                                 int dif = path.Length - basePath.Length;
96                                 string file = path.Substring (path.Length - dif);
97                                 int i=0;
98                                 while (i < file.Length && file [i] == '/')
99                                         i++;
100                                 if (i != 0)
101                                         file = file.Substring (i);
102
103                                 if (file.Length != 0) {
104                                         foreach (ConfigurationLocation loc in conf.Locations) {
105                                                 if (loc.Path == file)
106                                                         return loc.OpenConfiguration ();
107                                         }
108                                 }
109                         }
110                         return conf;
111                 }
112                 
113                 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
114                 {
115                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
116                 }
117                 
118                 [MonoTODO ("Do something with the extra parameters")]
119                 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
120                 {
121                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
122                 }
123                 
124                 [MonoTODO ("Do something with the extra parameters")]
125                 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
126                 {
127                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
128                 }
129                 
130                 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
131                 {
132                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
133                 }
134                 
135                 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
136                         get { return configFactory; }
137                 }
138                 
139                 static string GetBasePath (string path)
140                 {
141                         string pd = HttpContext.Current.Request.MapPath (path);
142
143                         if (!Directory.Exists (pd)) {
144                                 int i = path.LastIndexOf ('/');
145                                 path = path.Substring (0, i);
146                         } 
147                         
148                         while (path [path.Length - 1] == '/')
149                                 path = path.Substring (0, path.Length - 1);
150                         return path;
151                 }
152         }
153 }
154
155 #endif