Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / class / System.Configuration / System.Configuration / InternalConfigurationHost.cs
index 3b1235b2fd3766eb70175adc9be6a19bb74703c3..1219fcd99926776e632072f7c24381043c4b4d3a 100644 (file)
@@ -32,6 +32,7 @@ using System;
 using System.IO;
 using System.Security;
 using System.Configuration.Internal;
+using System.Runtime.CompilerServices;
 
 namespace System.Configuration
 {
@@ -44,7 +45,7 @@ namespace System.Configuration
                
                public virtual object CreateDeprecatedConfigContext (string configPath)
                {
-                       throw new NotImplementedException ();
+                       return null;
                }
                
                public virtual void DeleteStream (string streamName)
@@ -88,8 +89,8 @@ namespace System.Configuration
                public abstract string GetStreamName (string configPath);
                public abstract void Init (IInternalConfigRoot root, params object[] hostInitParams);
                public abstract void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams);
-               
-               [MonoTODO ("remote config")]
+
+               [MonoNotSupported ("mono does not support remote configuration")]
                public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
                {
                        throw new NotSupportedException ("mono does not support remote configuration");
@@ -162,11 +163,38 @@ namespace System.Configuration
                {
                        throw new NotImplementedException ();
                }
+#if !TARGET_JVM
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               extern private static string get_bundled_machine_config ();
+
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               extern private static string get_bundled_app_config ();
+#endif
                
                public virtual Stream OpenStreamForRead (string streamName)
                {
+                       if (String.CompareOrdinal (streamName, System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) == 0) {
+#if TARGET_JVM
+                               return (Stream) vmw.common.IOUtils.getStreamForGHConfigs (streamName);
+#else
+                               string bundle = get_bundled_machine_config ();
+                               if (bundle != null)
+                                       return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
+#endif
+                       }
+
+                       if (String.CompareOrdinal (streamName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == 0) {
+#if TARGET_JVM
+                               throw new NotImplementedException();
+#else
+                               string bundle = get_bundled_app_config ();
+                               if (bundle != null)
+                                       return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
+#endif
+                       }
+
                        if (!File.Exists (streamName))
-                               throw new ConfigurationException ("File '" + streamName + "' not found");
+                               return null;
                                
                        return new FileStream (streamName, FileMode.Open, FileAccess.Read);
                }
@@ -178,6 +206,9 @@ namespace System.Configuration
                
                public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
                {
+                       string dir = Path.GetDirectoryName (streamName);
+                       if (!Directory.Exists (dir))
+                               Directory.CreateDirectory (dir);
                        return new FileStream (streamName, FileMode.Create, FileAccess.Write);
                }
 
@@ -245,10 +276,12 @@ namespace System.Configuration
        class ExeConfigurationHost: InternalConfigurationHost
        {
                ExeConfigurationFileMap map;
+               ConfigurationUserLevel level;
                
                public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
                {
                        map = (ExeConfigurationFileMap) hostInitParams [0];
+                       level = (ConfigurationUserLevel) hostInitParams [1];
                }
                
                public override string GetStreamName (string configPath)
@@ -256,33 +289,61 @@ namespace System.Configuration
                        switch (configPath) {
                                case "exe": return map.ExeConfigFilename; 
                                case "local": return map.LocalUserConfigFilename;
-                               case "roaming": return map.LocalUserConfigFilename;
+                               case "roaming": return map.RoamingUserConfigFilename;
                                case "machine": return map.MachineConfigFilename;
-                               default: return map.ExeConfigFilename;
+                               default://return map.ExeConfigFilename;
+                               switch (level) {
+                                       case ConfigurationUserLevel.None:
+                                               return map.ExeConfigFilename; 
+                                       case ConfigurationUserLevel.PerUserRoaming:
+                                               return map.RoamingUserConfigFilename;
+                                       case ConfigurationUserLevel.PerUserRoamingAndLocal:
+                                               return map.LocalUserConfigFilename;
+                                       default:
+                                               return map.MachineConfigFilename;
+                               }
                        }
                }
                
                public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
                {
                        map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
+
+                       if (hostInitConfigurationParams.Length > 1 && 
+                           hostInitConfigurationParams [1] is ConfigurationUserLevel)
+                               level = (ConfigurationUserLevel) hostInitConfigurationParams [1];
+                       if (locationSubPath == null)
+                               switch (level) {
+                               case ConfigurationUserLevel.PerUserRoaming:
+                                       if (map.RoamingUserConfigFilename == null)
+                                               throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
+                                       locationSubPath = "roaming";
+                                       break;
+                               case ConfigurationUserLevel.PerUserRoamingAndLocal:
+                                       if (map.LocalUserConfigFilename == null)
+                                               throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
+                                       locationSubPath = "local";
+                                       break;
+                               }
+
                        configPath = null;
                        string next = null;
 
                        locationConfigPath = null;
 
-                       if ((locationSubPath == "exe" || locationSubPath == null) && map.ExeConfigFilename != null) {
+                       if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
                                configPath = "exe";
                                next = "local";
                                locationConfigPath = map.ExeConfigFilename;
                        }
                        
-                       if ((locationSubPath == "local" || configPath == null) && map.LocalUserConfigFilename != null) {
+                       if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
                                configPath = "local";
                                next = "roaming";
                                locationConfigPath = map.LocalUserConfigFilename;
                        }
                        
-                       if ((locationSubPath == "roaming" || configPath == null) && map.RoamingUserConfigFilename != null) {
+                       if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
                                configPath = "roaming";
                                next = "machine";
                                locationConfigPath = map.RoamingUserConfigFilename;
@@ -292,7 +353,6 @@ namespace System.Configuration
                                configPath = "machine";
                                next = null;
                        }
-
                        locationSubPath = next;
                }
        }
@@ -318,7 +378,12 @@ namespace System.Configuration
                        configPath = null;
                        locationConfigPath = null;
                }
-       }       
+
+               public override bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
+               {
+                       return true;
+               }
+       }
 }
 
 #endif