use ToLowerInvariant instead of ToLower
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / WebConfigurationManager.cs
1 //
2 // System.Web.Configuration.WebConfigurationManager.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //      Chris Toshok (toshok@ximian.com)
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
30 #if NET_2_0
31
32 using System;
33 using System.IO;
34 using System.Collections;
35 using System.Collections.Specialized;
36 using System.Reflection;
37 using System.Xml;
38 using System.Configuration;
39 using System.Configuration.Internal;
40 using _Configuration = System.Configuration.Configuration;
41
42 namespace System.Web.Configuration {
43
44         public static class WebConfigurationManager
45         {
46 #if !TARGET_J2EE
47                 static IInternalConfigConfigurationFactory configFactory;
48                 static Hashtable configurations = new Hashtable ();
49 #else
50                 static internal IInternalConfigConfigurationFactory configFactory
51                 {
52                         get{
53                                 IInternalConfigConfigurationFactory factory = (IInternalConfigConfigurationFactory)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory");
54                                 if (factory == null){
55                                         lock (AppDomain.CurrentDomain){
56                                                 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configFactory.initialized");
57                                                 if (initialized == null){
58                                                         PropertyInfo prop = typeof(ConfigurationManager).GetProperty("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
59                                                         if (prop != null){
60                                                                 factory = prop.GetValue(null, null) as IInternalConfigConfigurationFactory;
61                                                                 configFactory = factory;
62                                                         }
63                                                 }
64                                         }
65                                 }
66                                 return factory != null ? factory : configFactory;
67                         }
68                         set{
69                                 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory", value);
70                                 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configFactory.initialized", true);
71                         }
72                 }
73
74                 static internal Hashtable configurations
75                 {
76                         get{
77                                 Hashtable table = (Hashtable)AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations");
78                                 if (table == null){
79                                         lock (AppDomain.CurrentDomain){
80                                                 object initialized = AppDomain.CurrentDomain.GetData("WebConfigurationManager.configurations.initialized");
81                                                 if (initialized == null){
82                                                         table = new Hashtable();
83                                                         configurations = table;
84                                                 }
85                                         }
86                                 }
87                                 return table != null ? table : configurations;
88
89                         }
90                         set{
91                                 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations", value);
92                                 AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations.initialized", true);
93                         }
94                 }
95 #endif
96
97                 static internal ArrayList extra_assemblies = null;
98                 static internal ArrayList ExtraAssemblies {
99                         get {
100                                 if (extra_assemblies == null)
101                                         extra_assemblies = new ArrayList();
102                                 return extra_assemblies;
103                         }
104                 }
105                 
106                 static WebConfigurationManager ()
107                 {
108                         PropertyInfo prop = typeof(ConfigurationManager).GetProperty ("ConfigurationFactory", BindingFlags.Static | BindingFlags.NonPublic);
109                         if (prop != null)
110                                 configFactory = prop.GetValue (null, null) as IInternalConfigConfigurationFactory;
111                 }
112
113                 public static _Configuration OpenMachineConfiguration ()
114                 {
115                         return ConfigurationManager.OpenMachineConfiguration ();
116                 }
117                 
118                 [MonoTODO ("need to handle locationSubPath")]
119                 public static _Configuration OpenMachineConfiguration (string locationSubPath)
120                 {
121                         return OpenMachineConfiguration ();
122                 }
123
124                 [MonoTODO("Mono does not support remote configuration")]
125                 public static _Configuration OpenMachineConfiguration (string locationSubPath,
126                                                                        string server)
127                 {
128                         if (server == null)
129                                 return OpenMachineConfiguration (locationSubPath);
130
131                         throw new NotSupportedException ("Mono doesn't support remote configuration");
132                 }
133
134                 [MonoTODO("Mono does not support remote configuration")]
135                 public static _Configuration OpenMachineConfiguration (string locationSubPath,
136                                                                        string server,
137                                                                        IntPtr userToken)
138                 {
139                         if (server == null)
140                                 return OpenMachineConfiguration (locationSubPath);
141                         throw new NotSupportedException ("Mono doesn't support remote configuration");
142                 }
143
144                 [MonoTODO("Mono does not support remote configuration")]
145                 public static _Configuration OpenMachineConfiguration (string locationSubPath,
146                                                                        string server,
147                                                                        string userName,
148                                                                        string password)
149                 {
150                         if (server == null)
151                                 return OpenMachineConfiguration (locationSubPath);
152                         throw new NotSupportedException ("Mono doesn't support remote configuration");
153                 }
154
155                 public static _Configuration OpenWebConfiguration (string path)
156                 {
157                         return OpenWebConfiguration (path, null, null, null, null, null);
158                 }
159                 
160                 public static _Configuration OpenWebConfiguration (string path, string site)
161                 {
162                         return OpenWebConfiguration (path, site, null, null, null, null);
163                 }
164                 
165                 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath)
166                 {
167                         return OpenWebConfiguration (path, site, locationSubPath, null, null, null);
168                 }
169
170                 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server)
171                 {
172                         return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
173                 }
174
175                 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, IntPtr userToken)
176                 {
177                         return OpenWebConfiguration (path, site, locationSubPath, server, null, null);
178                 }
179                 
180                 public static _Configuration OpenWebConfiguration (string path, string site, string locationSubPath, string server, string userName, string password)
181                 {
182                         if (path == null || path.Length == 0)
183                                 path = "/";
184
185                         _Configuration conf;
186
187                         conf = (_Configuration) configurations [path];
188                         if (conf == null) {
189                                 lock (configurations) {
190                                         conf = (_Configuration) configurations [path];
191                                         if (conf == null) {
192                                                 conf = ConfigurationFactory.Create (typeof (WebConfigurationHost), null, path, site, locationSubPath, server, userName, password);
193                                                 configurations [path] = conf;
194                                         }
195                                 }
196                         }
197                         return conf;
198                 }
199
200                 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path)
201                 {
202                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path);
203                 }
204                 
205                 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site)
206                 {
207                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site);
208                 }
209                 
210                 public static _Configuration OpenMappedWebConfiguration (WebConfigurationFileMap fileMap, string path, string site, string locationSubPath)
211                 {
212                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap, path, site, locationSubPath);
213                 }
214                 
215                 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
216                 {
217                         return ConfigurationFactory.Create (typeof(WebConfigurationHost), fileMap);
218                 }
219
220                 public static _Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap,
221                                                                              string locationSubPath)
222                 {
223                         return OpenMappedMachineConfiguration (fileMap);
224                 }
225
226                 public static object GetSection (string sectionName)
227                 {
228                         string path = (HttpContext.Current != null
229                             && HttpContext.Current.Request != null) ?
230                                 HttpContext.Current.Request.Path : HttpRuntime.AppDomainAppVirtualPath;
231
232                         return GetSection (sectionName, path);
233                 }
234
235                 public static object GetSection (string sectionName, string path)
236                 {
237                         _Configuration c = OpenWebConfiguration (path);
238                         ConfigurationSection section = c.GetSection (sectionName);
239
240                         if (section == null)
241                                 return null;
242
243                         return get_runtime_object.Invoke (section, new object [0]);
244                 }
245
246                 readonly static MethodInfo get_runtime_object = typeof (ConfigurationSection).GetMethod ("GetRuntimeObject", BindingFlags.NonPublic | BindingFlags.Instance);
247
248                 public static object GetWebApplicationSection (string sectionName)
249                 {
250                         string path = (HttpContext.Current == null
251                                 || HttpContext.Current.Request == null
252                                 || HttpContext.Current.Request.ApplicationPath == null
253                                 || HttpContext.Current.Request.ApplicationPath == "") ?
254                                 String.Empty : HttpContext.Current.Request.ApplicationPath;
255
256                         return GetSection (sectionName, path);
257                 }
258
259                 public static NameValueCollection AppSettings {
260                         get { return ConfigurationManager.AppSettings; }
261                 }
262
263                 public static ConnectionStringSettingsCollection ConnectionStrings {
264                         get { return ConfigurationManager.ConnectionStrings; }
265                 }
266
267                 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
268                         get { return configFactory; }
269                 }
270                 
271 #region stuff copied from WebConfigurationSettings
272 #if TARGET_J2EE
273                 static internal IConfigurationSystem oldConfig {
274                         get {
275                                 return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationManager.oldConfig");
276                         }
277                         set {
278                                 AppDomain.CurrentDomain.SetData("WebConfigurationManager.oldConfig", value);
279                         }
280                 }
281
282                 static private Web20DefaultConfig config {
283                         get {
284                                 return (Web20DefaultConfig) AppDomain.CurrentDomain.GetData ("Web20DefaultConfig.config");
285                         }
286                         set {
287                                 AppDomain.CurrentDomain.SetData ("Web20DefaultConfig.config", value);
288                         }
289                 }
290
291                 static private IInternalConfigSystem configSystem {
292                         get {
293                                 return (IInternalConfigSystem) AppDomain.CurrentDomain.GetData ("IInternalConfigSystem.configSystem");
294                         }
295                         set {
296                                 AppDomain.CurrentDomain.SetData ("IInternalConfigSystem.configSystem", value);
297                         }
298                 }
299 #else
300                 static internal IConfigurationSystem oldConfig;
301                 static Web20DefaultConfig config;
302                 //static IInternalConfigSystem configSystem;
303 #endif
304                 const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
305                 static readonly object lockobj = new object ();
306
307                 internal static void Init ()
308                 {
309                         lock (lockobj) {
310                                 if (config != null)
311                                         return;
312
313                                 /* deal with the ConfigurationSettings stuff */
314                                 {
315                                         Web20DefaultConfig settings = Web20DefaultConfig.GetInstance ();
316                                         Type t = typeof (ConfigurationSettings);
317                                         MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
318                                                                                privStatic);
319
320                                         if (changeConfig == null)
321                                                 throw new ConfigurationException ("Cannot find method CCS");
322
323                                         object [] args = new object [] {settings};
324                                         oldConfig = (IConfigurationSystem)changeConfig.Invoke (null, args);
325                                         config = settings;
326
327                                         config.Init ();
328                                 }
329
330                                 /* deal with the ConfigurationManager stuff */
331                                 {
332                                         HttpConfigurationSystem system = new HttpConfigurationSystem ();
333                                         Type t = typeof (ConfigurationManager);
334                                         MethodInfo changeConfig = t.GetMethod ("ChangeConfigurationSystem",
335                                                                                privStatic);
336
337                                         if (changeConfig == null)
338                                                 throw new ConfigurationException ("Cannot find method CCS");
339
340                                         object [] args = new object [] {system};
341                                         changeConfig.Invoke (null, args);
342                                         //configSystem = system;
343                                 }
344                         }
345                 }
346         }
347
348         class Web20DefaultConfig : IConfigurationSystem
349         {
350 #if TARGET_J2EE
351                 static private Web20DefaultConfig instance {
352                         get {
353                                 Web20DefaultConfig val = (Web20DefaultConfig)AppDomain.CurrentDomain.GetData("Web20DefaultConfig.instance");
354                                 if (val == null) {
355                                         val = new Web20DefaultConfig();
356                                         AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", val);
357                                 }
358                                 return val;
359                         }
360                         set {
361                                 AppDomain.CurrentDomain.SetData("Web20DefaultConfig.instance", value);
362                         }
363                 }
364 #else
365                 static Web20DefaultConfig instance;
366 #endif
367
368                 static Web20DefaultConfig ()
369                 {
370                         instance = new Web20DefaultConfig ();
371                 }
372
373                 public static Web20DefaultConfig GetInstance ()
374                 {
375                         return instance;
376                 }
377
378                 public object GetConfig (string sectionName)
379                 {
380                         object o = WebConfigurationManager.GetWebApplicationSection (sectionName);
381
382                         if (o == null || o is IgnoreSection) {
383                                 /* this can happen when the section
384                                  * handler doesn't subclass from
385                                  * ConfigurationSection.  let's be
386                                  * nice and try to load it using the
387                                  * 1.x style routines in case there's
388                                  * a 1.x section handler registered
389                                  * for it.
390                                  */
391                                 object o1 = WebConfigurationManager.oldConfig.GetConfig (sectionName);
392                                 if (o1 != null)
393                                         return o1;
394                         }
395
396                         return o;
397                 }
398
399                 public void Init ()
400                 {
401                         // nothing. We need a context.
402                 }
403         }
404
405 #endregion
406 }
407
408 #endif