Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationManager.cs
index 9e969e765ee6f94bc6b7f9936182cbb94ec767bd..28d4fa428db9c1d659da469c364ea55c279d54df 100644 (file)
@@ -26,7 +26,7 @@
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
 //
-#if NET_2_0
+
 using System;
 using System.Collections;
 using System.Runtime.CompilerServices;
@@ -43,10 +43,11 @@ namespace System.Configuration {
 
        public static class ConfigurationManager
        {
+//             static bool systemWebInUse;
                static InternalConfigurationFactory configFactory = new InternalConfigurationFactory ();
                static IInternalConfigSystem configSystem = new ClientConfigurationSystem ();
                static object lockobj = new object ();
-
+               
                [MonoTODO ("Evidence and version still needs work")]
                static string GetAssemblyInfo (Assembly a)
                {
@@ -81,9 +82,6 @@ namespace System.Configuration {
 
                internal static Configuration OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, Assembly calling_assembly, string exePath)
                {
-                       if (calling_assembly == null && exePath == null)
-                               throw new ArgumentException ("exePath must be specified when not running inside a stand alone exe.");
-
                        ExeConfigurationFileMap map = new ExeConfigurationFileMap ();
 
                        /* Roaming and RoamingAndLocal should be different
@@ -95,46 +93,46 @@ namespace System.Configuration {
 
                        switch (userLevel) {
                        case ConfigurationUserLevel.None:
-                               if (exePath == null)
-                                       exePath = calling_assembly.Location;
-                               else if (!File.Exists (exePath))
-                                       exePath = "";
-
-                               if (exePath != "") {
-                                       if (!exePath.EndsWith (".config"))
-                                               map.ExeConfigFilename = exePath + ".config";
-                                       else
-                                               map.ExeConfigFilename = exePath;
+                               if (exePath == null || exePath.Length == 0) {
+                                       map.ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
+                               } else {
+                                       if (!Path.IsPathRooted (exePath))
+                                               exePath = Path.GetFullPath (exePath);
+                                       if (!File.Exists (exePath)) {
+                                               Exception cause = new ArgumentException ("The specified path does not exist.", "exePath");
+                                               throw new ConfigurationErrorsException ("Error Initializing the configuration system:", cause);
+                                       }
+                                       map.ExeConfigFilename = exePath + ".config";
                                }
                                break;
                        case ConfigurationUserLevel.PerUserRoaming:
                                map.RoamingUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), GetAssemblyInfo(calling_assembly));
                                map.RoamingUserConfigFilename = Path.Combine (map.RoamingUserConfigFilename, "user.config");
-                               goto case ConfigurationUserLevel.PerUserRoamingAndLocal;
+                               goto case ConfigurationUserLevel.None;
 
                        case ConfigurationUserLevel.PerUserRoamingAndLocal:
                                map.LocalUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), GetAssemblyInfo(calling_assembly));
                                map.LocalUserConfigFilename = Path.Combine (map.LocalUserConfigFilename, "user.config");
-                               break;
+                               goto case ConfigurationUserLevel.PerUserRoaming;
                        }
 
-                       return ConfigurationFactory.Create (typeof(ExeConfigurationHost), map);
+                       return ConfigurationFactory.Create (typeof(ExeConfigurationHost), map, userLevel);
                }
 
                public static Configuration OpenExeConfiguration (ConfigurationUserLevel userLevel)
                {
-                       return OpenExeConfigurationInternal (userLevel, Assembly.GetCallingAssembly (), null);
+                       return OpenExeConfigurationInternal (userLevel, Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly (), null);
                }
                
                public static Configuration OpenExeConfiguration (string exePath)
                {
-                       return OpenExeConfigurationInternal (ConfigurationUserLevel.None, Assembly.GetCallingAssembly (), exePath);
+                       return OpenExeConfigurationInternal (ConfigurationUserLevel.None, Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly (), exePath);
                }
 
-               [MonoTODO ("userLevel")]
+               [MonoLimitation("ConfigurationUserLevel parameter is not supported.")]
                public static Configuration OpenMappedExeConfiguration (ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
                {
-                       return ConfigurationFactory.Create (typeof(ExeConfigurationHost), fileMap);
+                       return ConfigurationFactory.Create (typeof(ExeConfigurationHost), fileMap, userLevel);
                }
 
                public static Configuration OpenMachineConfiguration ()
@@ -176,7 +174,6 @@ namespace System.Configuration {
                        }
                }
 
-               [MonoTODO]
                public static ConnectionStringSettingsCollection ConnectionStrings {
                        get {
                                ConnectionStringsSection connectionStrings = (ConnectionStringsSection) GetSection ("connectionStrings");
@@ -185,12 +182,22 @@ namespace System.Configuration {
                }
 
                /* invoked from System.Web */
-               static IInternalConfigSystem ChangeConfigurationSystem (IInternalConfigSystem newSystem)
+               internal static IInternalConfigSystem ChangeConfigurationSystem (IInternalConfigSystem newSystem)
                {
                        if (newSystem == null)
                                throw new ArgumentNullException ("newSystem");
 
                        lock (lockobj) {
+                               // KLUDGE!! We need that when an assembly loaded inside an ASP.NET
+                               // domain does OpenExeConfiguration ("") - we must return the path
+                               // to web.config in that instance.
+                               /*
+                               string t = newSystem.GetType ().ToString ();
+                               if (String.Compare (t, "System.Web.Configuration.HttpConfigurationSystem", StringComparison.OrdinalIgnoreCase) == 0)
+                                       systemWebInUse = true;
+                               else
+                                       systemWebInUse = false;
+                               */
                                IInternalConfigSystem old = configSystem;
                                configSystem = newSystem;
                                return old;
@@ -198,5 +205,3 @@ namespace System.Configuration {
                }
        }
 }
-
-#endif