revert
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationManager.cs
1 //
2 // System.Configuration.ConfigurationManager.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Lluis Sanchez Gual (lluis@novell.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) 2004 Novell, Inc (http://www.novell.com)
28 //
29 #if NET_2_0
30 using System;
31 using System.Collections;
32 using System.Runtime.CompilerServices;
33 using System.Collections.Specialized;
34 using System.Reflection;
35 using System.Xml;
36 using System.IO;
37 using System.Text;
38 using System.Configuration.Internal;
39
40 namespace System.Configuration {
41
42         /*roaming user config path: C:\Documents and Settings\toshok\Application Data\domain-System.Configurati_Url_py3nlovv3wxe21qgacxc3n2b1mph2log\1.0.0.0\user.config */
43
44         public static class ConfigurationManager
45         {
46                 static bool systemWebInUse;
47                 static InternalConfigurationFactory configFactory = new InternalConfigurationFactory ();
48                 static IInternalConfigSystem configSystem = new ClientConfigurationSystem ();
49                 static object lockobj = new object ();
50                 
51                 [MonoTODO ("Evidence and version still needs work")]
52                 static string GetAssemblyInfo (Assembly a)
53                 {
54                         object[] attrs;
55                         StringBuilder sb;
56
57                         string app_name;
58                         string evidence_str;
59                         string version;
60
61                         attrs = a.GetCustomAttributes (typeof (AssemblyProductAttribute), false);
62                         if (attrs != null && attrs.Length > 0)
63                                 app_name = ((AssemblyProductAttribute)attrs[0]).Product;
64                         else
65                                 app_name = AppDomain.CurrentDomain.FriendlyName;
66
67                         sb = new StringBuilder();
68
69                         sb.Append ("evidencehere");
70
71                         evidence_str = sb.ToString();
72
73                         attrs = a.GetCustomAttributes (typeof (AssemblyVersionAttribute), false);
74                         if (attrs != null && attrs.Length > 0)
75                                 version = ((AssemblyVersionAttribute)attrs[0]).Version;
76                         else
77                                 version = "1.0.0.0" /* XXX */;
78
79
80                         return Path.Combine (String.Format ("{0}_{1}", app_name, evidence_str), version);
81                 }
82
83                 internal static Configuration OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, Assembly calling_assembly, string exePath)
84                 {
85                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
86
87                         /* Roaming and RoamingAndLocal should be different
88
89                         On windows,
90                           PerUserRoaming = \Documents and Settings\<username>\Application Data\...
91                           PerUserRoamingAndLocal = \Documents and Settings\<username>\Local Settings\Application Data\...
92                         */
93
94                         switch (userLevel) {
95                         case ConfigurationUserLevel.None:
96                                 if (exePath == null || exePath.Length == 0) {
97                                         if (!systemWebInUse && calling_assembly != null)
98                                                 exePath = calling_assembly.Location;
99                                         else
100                                                 exePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
101                                         map.ExeConfigFilename = exePath.EndsWith (".config") ? exePath : exePath + ".config";
102                                 } else {
103                                         if (!Path.IsPathRooted (exePath))
104                                                 exePath = Path.GetFullPath (exePath);
105                                         if (!File.Exists (exePath)) {
106                                                 Exception cause = new ArgumentException ("The specified path does not exist.", "exePath");
107                                                 throw new ConfigurationErrorsException ("Error Initializing the configuration system:", cause);
108                                         }
109                                         map.ExeConfigFilename = exePath + ".config";
110                                 }
111                                 break;
112                         case ConfigurationUserLevel.PerUserRoaming:
113                                 map.RoamingUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), GetAssemblyInfo(calling_assembly));
114                                 map.RoamingUserConfigFilename = Path.Combine (map.RoamingUserConfigFilename, "user.config");
115                                 goto case ConfigurationUserLevel.PerUserRoamingAndLocal;
116
117                         case ConfigurationUserLevel.PerUserRoamingAndLocal:
118                                 map.LocalUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), GetAssemblyInfo(calling_assembly));
119                                 map.LocalUserConfigFilename = Path.Combine (map.LocalUserConfigFilename, "user.config");
120                                 break;
121                         }
122
123                         return ConfigurationFactory.Create (typeof(ExeConfigurationHost), map, userLevel);
124                 }
125
126 #if TARGET_JVM
127                 [MonoLimitation ("Supported only when the userLevel parameter is set to ConfigurationUserLevel.None. Other values are not supported because Environment.GetFolderPath method is not implemented.")]
128 #endif
129                 public static Configuration OpenExeConfiguration (ConfigurationUserLevel userLevel)
130                 {
131                         return OpenExeConfigurationInternal (userLevel, Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly (), null);
132                 }
133                 
134                 public static Configuration OpenExeConfiguration (string exePath)
135                 {
136                         return OpenExeConfigurationInternal (ConfigurationUserLevel.None, Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly (), exePath);
137                 }
138
139                 [MonoLimitation("ConfigurationUserLevel parameter is not supported.")]
140                 public static Configuration OpenMappedExeConfiguration (ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
141                 {
142                         return ConfigurationFactory.Create (typeof(ExeConfigurationHost), fileMap, userLevel);
143                 }
144
145                 public static Configuration OpenMachineConfiguration ()
146                 {
147                         ConfigurationFileMap map = new ConfigurationFileMap ();
148                         return ConfigurationFactory.Create (typeof(MachineConfigurationHost), map);
149                 }
150                 
151                 public static Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
152                 {
153                         return ConfigurationFactory.Create (typeof(MachineConfigurationHost), fileMap);
154                 }
155                 
156                 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
157                         get { return configFactory; }
158                 }
159
160                 internal static IInternalConfigSystem ConfigurationSystem {
161                         get { return configSystem; }
162                 }
163
164                 public static object GetSection (string sectionName)
165                 {
166                         object o = ConfigurationSystem.GetSection (sectionName);
167                         if (o is ConfigurationSection)
168                                 return ((ConfigurationSection) o).GetRuntimeObject ();
169                         else
170                                 return o;
171                 }
172
173                 public static void RefreshSection (string sectionName)
174                 {
175                         ConfigurationSystem.RefreshConfig (sectionName);
176                 }
177
178                 public static NameValueCollection AppSettings {
179                         get {
180                                 return (NameValueCollection) GetSection ("appSettings");
181                         }
182                 }
183
184                 [MonoTODO]
185                 public static ConnectionStringSettingsCollection ConnectionStrings {
186                         get {
187                                 ConnectionStringsSection connectionStrings = (ConnectionStringsSection) GetSection ("connectionStrings");
188                                 return connectionStrings.ConnectionStrings;
189                         }
190                 }
191
192                 /* invoked from System.Web */
193                 internal static IInternalConfigSystem ChangeConfigurationSystem (IInternalConfigSystem newSystem)
194                 {
195                         if (newSystem == null)
196                                 throw new ArgumentNullException ("newSystem");
197
198                         lock (lockobj) {
199                                 // KLUDGE!! We need that when an assembly loaded inside an ASP.NET
200                                 // domain does OpenExeConfiguration ("") - we must return the path
201                                 // to web.config in that instance.
202                                 string t = newSystem.GetType ().ToString ();
203                                 if (String.Compare (t, "System.Web.Configuration.HttpConfigurationSystem", StringComparison.OrdinalIgnoreCase) == 0)
204                                         systemWebInUse = true;
205                                 else
206                                         systemWebInUse = false;
207
208                                 IInternalConfigSystem old = configSystem;
209                                 configSystem = newSystem;
210                                 return old;
211                         }
212                 }
213         }
214 }
215
216 #endif