Merge pull request #297 from ermshiperete/4959
[mono.git] / mcs / class / corlib / System.Runtime.Remoting / RemotingConfiguration.cs
index 204bc6b4368f17bfa3fc31873a6d1aae6d9ebf78..46e71422e04baf501f3327fe6cfa3d393355a068 100644 (file)
@@ -31,6 +31,7 @@
 //
 
 using System;
+using System.Globalization;
 using System.IO;
 using System.Reflection;
 using System.Collections;
@@ -41,19 +42,14 @@ using Mono.Xml;
 
 namespace System.Runtime.Remoting
 {      
-       public class RemotingConfiguration
+       [System.Runtime.InteropServices.ComVisible (true)]
+       public static class RemotingConfiguration
        {
-               //
-               // Private constructor: nobody instantiates this.
-               //
-               private RemotingConfiguration ()
-               {
-               }
                
                static string applicationID = null;
                static string applicationName = null;
-               static string configFile = "";
-               static MiniParser parser = null; 
+//             static string configFile = "";
+//             static SmallXmlParser parser = null; 
                static string processGuid = null;
                static bool defaultConfigRead = false;
                static bool defaultDelayedConfigRead = false;
@@ -73,27 +69,25 @@ namespace System.Runtime.Remoting
                public static string ApplicationId 
                {
                        get 
-                       { 
-                               applicationID = AppDomain.CurrentDomain.SetupInformation.ApplicationName; 
+                       {
+                               applicationID = ApplicationName; 
                                return applicationID;
                        }
                }
                
                public static string ApplicationName 
                {
-                       get { 
-                               try {
-                                       applicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName; 
-                               }
-                               catch (Exception e) {
-                                       throw e;
-                               }
-                               // We return null if the application name has not been set.
-                               return null;
-                       }
+                       get { return applicationName; }
                        set { applicationName = value; }
                }
-               
+
+               [MonoTODO]
+               public static CustomErrorsModes CustomErrorsMode
+               {
+                       get { throw new NotImplementedException (); }
+                       set { throw new NotImplementedException (); }
+               }
+
                public static string ProcessId 
                {
                        get {
@@ -107,33 +101,39 @@ namespace System.Runtime.Remoting
 
                // public methods
                
-               public static void Configure (string filename) 
+               [MonoTODO ("ensureSecurity support has not been implemented")]
+               public static void Configure (string filename, bool ensureSecurity) 
                {
-                       lock (channelTemplates)
-                       {
-                               if (!defaultConfigRead)
-                               {
+                       lock (channelTemplates) {
+                               if (!defaultConfigRead) {
                                        ReadConfigFile (Environment.GetMachineConfigPath ());
                                        defaultConfigRead = true;
                                }
-                               
+
                                if (filename != null)
                                        ReadConfigFile (filename);
                        }
                }
 
+               [Obsolete ("Use Configure(String,Boolean)")]
+               public static void Configure (string filename) 
+               {
+                       Configure (filename, false);
+               }
+
                private static void ReadConfigFile (string filename)
                {
                        try
                        {
-                               MiniParser parser = new MiniParser ();
-                               RReader rreader = new RReader (filename);
-                               ConfigHandler handler = new ConfigHandler (false);
-                               parser.Parse (rreader, handler);
+                               SmallXmlParser parser = new SmallXmlParser ();
+                               using (TextReader rreader = new StreamReader (filename)) {
+                                       ConfigHandler handler = new ConfigHandler (false);
+                                       parser.Parse (rreader, handler);
+                               }
                        }
                        catch (Exception ex)
                        {
-                               throw new RemotingException ("Configuration file '" + filename + "' could not be loaded: " + ex.Message);
+                               throw new RemotingException ("Configuration file '" + filename + "' could not be loaded: " + ex.Message, ex);
                        }
                }
                
@@ -143,10 +143,11 @@ namespace System.Runtime.Remoting
                        {
                                if (defaultDelayedConfigRead || defaultConfigRead) return;
                                
-                               MiniParser parser = new MiniParser ();
-                               RReader rreader = new RReader (Environment.GetMachineConfigPath ());
-                               ConfigHandler handler = new ConfigHandler (true);
-                               parser.Parse (rreader, handler);
+                               SmallXmlParser parser = new SmallXmlParser ();
+                               using (TextReader rreader = new StreamReader (Environment.GetMachineConfigPath ())) {
+                                       ConfigHandler handler = new ConfigHandler (true);
+                                       parser.Parse (rreader, handler);
+                               }
                                defaultDelayedConfigRead = true;
                        }
                }
@@ -191,19 +192,19 @@ namespace System.Runtime.Remoting
                        }
                }
 
-               public static bool IsActivationAllowed (Type serverType) 
+               public static bool IsActivationAllowed (Type svrType) 
                {
                        lock (channelTemplates)
                        {
-                               return activatedServiceEntries.ContainsKey (serverType);
+                               return activatedServiceEntries.ContainsKey (svrType);
                        }
                }
 
-               public static ActivatedClientTypeEntry IsRemotelyActivatedClientType (Type serviceType) 
+               public static ActivatedClientTypeEntry IsRemotelyActivatedClientType (Type svrType) 
                {
                        lock (channelTemplates)
                        {
-                               return activatedClientEntries [serviceType] as ActivatedClientTypeEntry;
+                               return activatedClientEntries [svrType] as ActivatedClientTypeEntry;
                        }
                }
 
@@ -212,11 +213,11 @@ namespace System.Runtime.Remoting
                        return IsRemotelyActivatedClientType (Assembly.Load(assemblyName).GetType (typeName));
                }
 
-               public static WellKnownClientTypeEntry IsWellKnownClientType (Type serviceType) 
+               public static WellKnownClientTypeEntry IsWellKnownClientType (Type svrType) 
                {
                        lock (channelTemplates)
                        {
-                               return wellKnownClientEntries [serviceType] as WellKnownClientTypeEntry;
+                               return wellKnownClientEntries [svrType] as WellKnownClientTypeEntry;
                        }
                }
 
@@ -278,9 +279,9 @@ namespace System.Runtime.Remoting
                        }
                }
 
-               public static void RegisterWellKnownServiceType (Type type, string objectUrl, WellKnownObjectMode mode) 
+               public static void RegisterWellKnownServiceType (Type type, string objectUri, WellKnownObjectMode mode) 
                {
-                       RegisterWellKnownServiceType (new WellKnownServiceTypeEntry (type, objectUrl, mode));
+                       RegisterWellKnownServiceType (new WellKnownServiceTypeEntry (type, objectUri, mode));
                }
 
                public static void RegisterWellKnownServiceType (WellKnownServiceTypeEntry entry) 
@@ -374,10 +375,16 @@ namespace System.Runtime.Remoting
 
                internal static void SetCustomErrorsMode (string mode)
                {
-                       if (mode != "on" && mode != "off" && mode != "remoteOnly")
+                       if (mode == null)
+                               throw new RemotingException ("mode attribute is required");
+
+                       // the mode is case insensitive
+                       string m = mode.ToLower ();
+
+                       if (m != "on" && m != "off" && m != "remoteonly")
                                throw new RemotingException ("Invalid custom error mode: " + mode);
                                
-                       _errorMode = mode;
+                       _errorMode = m;
                }
        }
 
@@ -385,33 +392,7 @@ namespace System.Runtime.Remoting
         * Internal classes used by RemotingConfiguration.Configure () *
         ***************************************************************/
         
-       internal class RReader : MiniParser.IReader {
-               private string xml; // custom remoting config file
-               private int pos;
-
-               public RReader (string filename)
-               {
-                       try {
-                               StreamReader sr = new StreamReader (filename);
-                               xml = sr.ReadToEnd ();
-                               sr.Close ();
-                       }
-                       catch {
-                               xml = null;
-                       }
-               }
-
-               public int Read () {
-                       try {
-                               return (int) xml[pos++];
-                       }
-                       catch {
-                               return -1;
-                       }
-               }
-       }
-       
-       internal class ConfigHandler : MiniParser.IHandler 
+       internal class ConfigHandler : SmallXmlParser.IContentHandler 
        {
                ArrayList typeEntries = new ArrayList ();
                ArrayList channelInstances = new ArrayList ();
@@ -440,15 +421,20 @@ namespace System.Runtime.Remoting
                
                bool CheckPath (string path)
                {
-                       if (path.StartsWith ("/"))
+                       CompareInfo ci = CultureInfo.InvariantCulture.CompareInfo;
+                       if (ci.IsPrefix (path, "/", CompareOptions.Ordinal))
                                return path == currentXmlPath;
                        else
-                               return currentXmlPath.EndsWith (path);
+                               return ci.IsSuffix (currentXmlPath, path, CompareOptions.Ordinal);
                }
                
-               public void OnStartParsing (MiniParser parser) {}
+               public void OnStartParsing (SmallXmlParser parser) {}
                
-               public void OnStartElement (string name, MiniParser.IAttrList attrs)
+               public void OnProcessingInstruction (string name, string text) {}
+
+               public void OnIgnorableWhitespace (string s) {}
+
+               public void OnStartElement (string name, SmallXmlParser.IAttrList attrs)
                {
                        try
                        {
@@ -459,11 +445,11 @@ namespace System.Runtime.Remoting
                        }
                        catch (Exception ex)
                        {
-                               throw new RemotingException ("Error in element " + name + ": " + ex.Message);
+                               throw new RemotingException ("Error in element " + name + ": " + ex.Message, ex);
                        }
                }
                
-               public void ParseElement (string name, MiniParser.IAttrList attrs)
+               public void ParseElement (string name, SmallXmlParser.IAttrList attrs)
                {
                        if (currentProviderData != null)
                        {
@@ -601,14 +587,14 @@ namespace System.Runtime.Remoting
                        if (currentProviderData != null)
                        {
                                currentProviderData.Pop ();
-                               if (currentProviderData.Count > 0) return;
-                               currentProviderData = null;
+                               if (currentProviderData.Count == 0) 
+                                       currentProviderData = null;
                        }
                        
                        currentXmlPath = currentXmlPath.Substring (0, currentXmlPath.Length - name.Length - 1);
                }
                
-               void ReadCustomProviderData (string name, MiniParser.IAttrList attrs)
+               void ReadCustomProviderData (string name, SmallXmlParser.IAttrList attrs)
                {
                        SinkProviderData parent = (SinkProviderData) currentProviderData.Peek ();
                        
@@ -620,7 +606,7 @@ namespace System.Runtime.Remoting
                        currentProviderData.Push (data);
                }
 
-               void ReadLifetine (MiniParser.IAttrList attrs)
+               void ReadLifetine (SmallXmlParser.IAttrList attrs)
                {
                        for (int i=0; i < attrs.Names.Length; ++i) {
                                switch (attrs.Names[i]) {
@@ -672,7 +658,7 @@ namespace System.Runtime.Remoting
                        throw new RemotingException ("Invalid time unit: " + unit);
                }
                
-               void ReadChannel (MiniParser.IAttrList attrs, bool isTemplate)
+               void ReadChannel (SmallXmlParser.IAttrList attrs, bool isTemplate)
                {
                        ChannelData channel = new ChannelData ();
                        
@@ -705,7 +691,7 @@ namespace System.Runtime.Remoting
                        currentChannel = channel;
                }
                
-               ProviderData ReadProvider (string name, MiniParser.IAttrList attrs, bool isTemplate)
+               ProviderData ReadProvider (string name, SmallXmlParser.IAttrList attrs, bool isTemplate)
                {
                        ProviderData prov = (name == "provider") ? new ProviderData () : new FormatterData ();
                        SinkProviderData data = new SinkProviderData ("root");
@@ -721,9 +707,9 @@ namespace System.Runtime.Remoting
                                
                                if (at == "id" && isTemplate)
                                        prov.Id = val;
-                               if (at == "type")
+                               else if (at == "type")
                                        prov.Type = val;
-                               if (at == "ref" && !isTemplate)
+                               else if (at == "ref" && !isTemplate)
                                        prov.Ref = val;
                                else
                                        prov.CustomProperties.Add (at, val);
@@ -733,7 +719,7 @@ namespace System.Runtime.Remoting
                        return prov;
                }
                
-               void ReadClientActivated (MiniParser.IAttrList attrs)
+               void ReadClientActivated (SmallXmlParser.IAttrList attrs)
                {
                        string type = GetNotNull (attrs, "type");
                        string assm = ExtractAssembly (ref type);
@@ -744,7 +730,7 @@ namespace System.Runtime.Remoting
                        typeEntries.Add (new ActivatedClientTypeEntry (type, assm, currentClientUrl));
                }
                
-               void ReadServiceActivated (MiniParser.IAttrList attrs)
+               void ReadServiceActivated (SmallXmlParser.IAttrList attrs)
                {
                        string type = GetNotNull (attrs, "type");
                        string assm = ExtractAssembly (ref type);
@@ -752,7 +738,7 @@ namespace System.Runtime.Remoting
                        typeEntries.Add (new ActivatedServiceTypeEntry (type, assm));
                }
                
-               void ReadClientWellKnown (MiniParser.IAttrList attrs)
+               void ReadClientWellKnown (SmallXmlParser.IAttrList attrs)
                {
                        string url = GetNotNull (attrs, "url");
                        string type = GetNotNull (attrs, "type");
@@ -761,7 +747,7 @@ namespace System.Runtime.Remoting
                        typeEntries.Add (new WellKnownClientTypeEntry (type, assm, url));
                }
                
-               void ReadServiceWellKnown (MiniParser.IAttrList attrs)
+               void ReadServiceWellKnown (SmallXmlParser.IAttrList attrs)
                {
                        string objectUri = GetNotNull (attrs, "objectUri");
                        string smode = GetNotNull (attrs, "mode");
@@ -776,7 +762,7 @@ namespace System.Runtime.Remoting
                        typeEntries.Add (new WellKnownServiceTypeEntry (type, assm, objectUri, mode));
                }
                
-               void ReadInteropXml (MiniParser.IAttrList attrs, bool isElement)
+               void ReadInteropXml (SmallXmlParser.IAttrList attrs, bool isElement)
                {
                        Type t = Type.GetType (GetNotNull (attrs, "clr"));
                        string[] xmlName = GetNotNull (attrs, "xml").Split (',');
@@ -787,7 +773,7 @@ namespace System.Runtime.Remoting
                        else SoapServices.RegisterInteropXmlType (localName, ns, t);
                }
                
-               void ReadPreload (MiniParser.IAttrList attrs)
+               void ReadPreload (SmallXmlParser.IAttrList attrs)
                {
                        string type = attrs.GetValue ("type");
                        string assm = attrs.GetValue ("assembly");
@@ -803,7 +789,7 @@ namespace System.Runtime.Remoting
                                throw new RemotingException ("Either type or assembly attributes must be specified");
                }
                                        
-               string GetNotNull (MiniParser.IAttrList attrs, string name)
+               string GetNotNull (SmallXmlParser.IAttrList attrs, string name)
                {
                        string value = attrs.GetValue (name);
                        if (value == null || value == "") 
@@ -823,7 +809,7 @@ namespace System.Runtime.Remoting
                
                public void OnChars (string ch) {}
                
-               public void OnEndParsing (MiniParser parser)
+               public void OnEndParsing (SmallXmlParser parser)
                {
                        RemotingConfiguration.RegisterChannels (channelInstances, onlyDelayedChannels);
                        if (appName != null) RemotingConfiguration.ApplicationName = appName;