2008-07-01 Miguel de Icaza <miguel@novell.com>
[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 InternalConfigurationFactory configFactory = new InternalConfigurationFactory ();
47                 static IInternalConfigSystem configSystem = new ClientConfigurationSystem ();
48                 static object lockobj = new object ();
49
50                 [MonoTODO ("Evidence and version still needs work")]
51                 static string GetAssemblyInfo (Assembly a)
52                 {
53                         object[] attrs;
54                         StringBuilder sb;
55
56                         string app_name;
57                         string evidence_str;
58                         string version;
59
60                         attrs = a.GetCustomAttributes (typeof (AssemblyProductAttribute), false);
61                         if (attrs != null && attrs.Length > 0)
62                                 app_name = ((AssemblyProductAttribute)attrs[0]).Product;
63                         else
64                                 app_name = AppDomain.CurrentDomain.FriendlyName;
65
66                         sb = new StringBuilder();
67
68                         sb.Append ("evidencehere");
69
70                         evidence_str = sb.ToString();
71
72                         attrs = a.GetCustomAttributes (typeof (AssemblyVersionAttribute), false);
73                         if (attrs != null && attrs.Length > 0)
74                                 version = ((AssemblyVersionAttribute)attrs[0]).Version;
75                         else
76                                 version = "1.0.0.0" /* XXX */;
77
78
79                         return Path.Combine (String.Format ("{0}_{1}", app_name, evidence_str), version);
80                 }
81
82                 internal static Configuration OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, Assembly calling_assembly, string exePath)
83                 {
84                         if (calling_assembly == null && exePath == null)
85                                 throw new ArgumentException ("exePath must be specified when not running inside a stand alone exe.");
86
87                         ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
88
89                         /* Roaming and RoamingAndLocal should be different
90
91                         On windows,
92                           PerUserRoaming = \Documents and Settings\<username>\Application Data\...
93                           PerUserRoamingAndLocal = \Documents and Settings\<username>\Local Settings\Application Data\...
94                         */
95
96                         switch (userLevel) {
97                         case ConfigurationUserLevel.None:
98                                 if (exePath == null || exePath == ""){
99                                         Assembly a = Assembly.GetEntryAssembly ();
100                                         if (a == null)
101                                                 a = calling_assembly;
102                                         
103                                         exePath = a.Location;
104                                 } else if (!File.Exists (exePath))
105                                         exePath = "";
106
107                                 if (exePath != "") {
108                                         if (!exePath.EndsWith (".config"))
109                                                 map.ExeConfigFilename = exePath + ".config";
110                                         else
111                                                 map.ExeConfigFilename = exePath;
112                                 }
113                                 break;
114                         case ConfigurationUserLevel.PerUserRoaming:
115                                 map.RoamingUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), GetAssemblyInfo(calling_assembly));
116                                 map.RoamingUserConfigFilename = Path.Combine (map.RoamingUserConfigFilename, "user.config");
117                                 goto case ConfigurationUserLevel.PerUserRoamingAndLocal;
118
119                         case ConfigurationUserLevel.PerUserRoamingAndLocal:
120                                 map.LocalUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), GetAssemblyInfo(calling_assembly));
121                                 map.LocalUserConfigFilename = Path.Combine (map.LocalUserConfigFilename, "user.config");
122                                 break;
123                         }
124
125                         return ConfigurationFactory.Create (typeof(ExeConfigurationHost), map, userLevel);
126                 }
127
128 #if TARGET_JVM
129                 [MonoLimitation ("Supported only when the userLevel parameter is set to ConfigurationUserLevel.None. Other values are not supported because Environment.GetFolderPath method is not implemented.")]
130 #endif
131                 public static Configuration OpenExeConfiguration (ConfigurationUserLevel userLevel)
132                 {
133                         return OpenExeConfigurationInternal (userLevel, Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly (), null);
134                 }
135                 
136                 public static Configuration OpenExeConfiguration (string exePath)
137                 {
138                         return OpenExeConfigurationInternal (ConfigurationUserLevel.None, Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly (), exePath);
139                 }
140
141                 [MonoLimitation("ConfigurationUserLevel parameter is not supported.")]
142                 public static Configuration OpenMappedExeConfiguration (ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
143                 {
144                         return ConfigurationFactory.Create (typeof(ExeConfigurationHost), fileMap, userLevel);
145                 }
146
147                 public static Configuration OpenMachineConfiguration ()
148                 {
149                         ConfigurationFileMap map = new ConfigurationFileMap ();
150                         return ConfigurationFactory.Create (typeof(MachineConfigurationHost), map);
151                 }
152                 
153                 public static Configuration OpenMappedMachineConfiguration (ConfigurationFileMap fileMap)
154                 {
155                         return ConfigurationFactory.Create (typeof(MachineConfigurationHost), fileMap);
156                 }
157                 
158                 internal static IInternalConfigConfigurationFactory ConfigurationFactory {
159                         get { return configFactory; }
160                 }
161
162                 internal static IInternalConfigSystem ConfigurationSystem {
163                         get { return configSystem; }
164                 }
165
166                 public static object GetSection (string sectionName)
167                 {
168                         object o = ConfigurationSystem.GetSection (sectionName);
169                         if (o is ConfigurationSection)
170                                 return ((ConfigurationSection) o).GetRuntimeObject ();
171                         else
172                                 return o;
173                 }
174
175                 public static void RefreshSection (string sectionName)
176                 {
177                         ConfigurationSystem.RefreshConfig (sectionName);
178                 }
179
180                 public static NameValueCollection AppSettings {
181                         get {
182                                 return (NameValueCollection) GetSection ("appSettings");
183                         }
184                 }
185
186                 [MonoTODO]
187                 public static ConnectionStringSettingsCollection ConnectionStrings {
188                         get {
189                                 ConnectionStringsSection connectionStrings = (ConnectionStringsSection) GetSection ("connectionStrings");
190                                 return connectionStrings.ConnectionStrings;
191                         }
192                 }
193
194                 /* invoked from System.Web */
195                 internal static IInternalConfigSystem ChangeConfigurationSystem (IInternalConfigSystem newSystem)
196                 {
197                         if (newSystem == null)
198                                 throw new ArgumentNullException ("newSystem");
199
200                         lock (lockobj) {
201                                 IInternalConfigSystem old = configSystem;
202                                 configSystem = newSystem;
203                                 return old;
204                         }
205                 }
206         }
207 }
208
209 #endif