Fixed loading of web.config for TARGET_J2EE.
[mono.git] / mcs / class / System.Web / System.Web.Configuration / WebConfigurationSettings.cs
index ec7cfaa296cc8e2aef69262c98ed0d8fb23272c6..9e9af3e2cc7d5d8e6b180ef92fae5ed2b06a496a 100644 (file)
@@ -36,23 +36,49 @@ using System.Reflection;
 using System.Runtime.Remoting;
 using System.Web.Util;
 using System.Xml;
+#if TARGET_J2EE
+using vmw.@internal.io;
+using vmw.common;
+using System.Web.J2EE;
+#endif
 
 namespace System.Web.Configuration
 {
        class WebConfigurationSettings
        {
+#if TARGET_J2EE
+               static private IConfigurationSystem oldConfig {
+                       get {
+                               return (IConfigurationSystem)AppDomain.CurrentDomain.GetData("WebConfigurationSettings.oldConfig");
+                       }
+                       set {
+                               AppDomain.CurrentDomain.SetData("WebConfigurationSettings.oldConfig", value);
+                       }
+               }
+
+               static private WebDefaultConfig config {
+                       get {
+                               return (WebDefaultConfig)AppDomain.CurrentDomain.GetData("WebConfigurationSettings.config");
+                       }
+                       set {
+                               AppDomain.CurrentDomain.SetData("WebConfigurationSettings.config", value);
+                       }
+               }
+#else
                static IConfigurationSystem oldConfig;
                static WebDefaultConfig config;
+#endif
                static string machineConfigPath;
                const BindingFlags privStatic = BindingFlags.NonPublic | BindingFlags.Static;
-                       
+               static readonly object lockobj = new object ();
+               
                private WebConfigurationSettings ()
                {
                }
 
                public static void Init ()
                {
-                       lock (typeof (WebConfigurationSettings)) {
+                       lock (lockobj) {
                                if (config != null)
                                        return;
 
@@ -88,7 +114,7 @@ namespace System.Web.Configuration
 
                public static string MachineConfigPath {
                        get {
-                               lock (typeof (WebConfigurationSettings)) {
+                               lock (lockobj) {
                                        if (machineConfigPath != null)
                                                return machineConfigPath;
 
@@ -115,7 +141,23 @@ namespace System.Web.Configuration
        //
        class WebDefaultConfig : IConfigurationSystem
        {
+#if TARGET_J2EE
+               static private WebDefaultConfig instance {
+                       get {
+                               WebDefaultConfig val = (WebDefaultConfig)AppDomain.CurrentDomain.GetData("WebDefaultConfig.instance");
+                               if (val == null) {
+                                       val = new WebDefaultConfig();
+                                       AppDomain.CurrentDomain.SetData("WebDefaultConfig.instance", val);
+                               }
+                               return val;
+                       }
+                       set {
+                               AppDomain.CurrentDomain.SetData("WebDefaultConfig.instance", value);
+                       }
+               }
+#else
                static WebDefaultConfig instance;
+#endif
                Hashtable fileToConfig;
                HttpContext firstContext;
                bool initCalled;
@@ -187,7 +229,6 @@ namespace System.Web.Configuration
                                tempDir = "";
                                realpath = HttpRuntime.AppDomainAppPath;
                        }
-
                        ConfigurationData parent = GetConfigFromFileName (tempDir, context);
                        if (wcfile == null) {
                                data = new ConfigurationData (parent, null, realpath);
@@ -200,7 +241,9 @@ namespace System.Web.Configuration
                                data.DirName = dir;
                                data.LoadFromFile (wcfile);
                                fileToConfig [dir] = data;
+#if !TARGET_JVM // no remoting support yet in Grasshopper
                                RemotingConfiguration.Configure (wcfile);
+#endif
                        }
 
                        return data;
@@ -229,18 +272,6 @@ namespace System.Web.Configuration
                                initCalled = true;
                        }
                }
-
-               static string GetAppConfigPath ()
-               {
-                       AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
-
-                       string configFile = currentInfo.ConfigurationFile;
-                       if (configFile == null || configFile.Length == 0)
-                               return null;
-
-                       return configFile;
-
-               }
        }
 
         class FileWatcherCache
@@ -248,7 +279,9 @@ namespace System.Web.Configuration
                 Hashtable cacheTable;
                string path;
                string filename;
+#if !TARGET_JVM // no file watcher support yet in Grasshopper
                FileSystemWatcher watcher;
+#endif
                ConfigurationData data;
 
                 public FileWatcherCache (ConfigurationData data)
@@ -260,14 +293,17 @@ namespace System.Web.Configuration
                        if (!Directory.Exists (path))
                                return;
 
+#if !TARGET_JVM
                        watcher = new FileSystemWatcher (this.path, this.filename);
                        FileSystemEventHandler handler = new FileSystemEventHandler (SetChanged);
                        watcher.Created += handler;
                        watcher.Changed += handler;
                        watcher.Deleted += handler;
                        watcher.EnableRaisingEvents = true;
+#endif
                 }
 
+#if !TARGET_JVM
                void SetChanged (object o, FileSystemEventArgs args)
                {
                        lock (data) {
@@ -280,6 +316,7 @@ namespace System.Web.Configuration
                                        data.LoadFromFile (args.FullPath);
                        }
                }
+#endif
                
                public object this [string key] {
                        get {
@@ -295,10 +332,12 @@ namespace System.Web.Configuration
 
                public void Close ()
                {
+#if !TARGET_JVM
                        if (watcher != null)
                                watcher.EnableRaisingEvents = false;
+#endif
                }
-        }
+       }
 
        enum AllowDefinition
        {
@@ -333,7 +372,6 @@ namespace System.Web.Configuration
                Hashtable locations;
                string fileName;
                string dirname;
-               string realdir;
                static object removedMark = new object ();
                static object groupMark = new object ();
                 static object emptyMark = new object ();
@@ -395,7 +433,6 @@ namespace System.Web.Configuration
                public ConfigurationData (ConfigurationData parent, string filename, string realdir)
                {
                        this.parent = (parent == this) ? null : parent;
-                       this.realdir = realdir;
                        if (filename == null) {
                                this.fileName = Path.Combine (realdir, "*.config");
                        } else {
@@ -407,13 +444,29 @@ namespace System.Web.Configuration
                public bool LoadFromFile (string fileName)
                {
                        this.fileName = fileName;
-                       if (fileName == null || !File.Exists (fileName))
-                               return false;
+                       Stream fs = null;
+                       if (fileName == null || !File.Exists (fileName)) {
+#if TARGET_J2EE
+                               if (fileName != null && fileName.EndsWith("machine.config"))
+                               {
+                                       if (fileName.StartsWith("/"))
+                                               fileName = fileName.Substring(1);
+                                       java.lang.ClassLoader cl = (java.lang.ClassLoader)AppDomain.CurrentDomain.GetData("GH_ContextClassLoader");
+                                       if (cl == null)
+                                               return false;
+                                       java.io.InputStream inputStream = cl.getResourceAsStream(fileName);
+                                       fs = (Stream)IOUtils.getStream(inputStream);
+                               }
+                               else
+#endif
+                                       return false;
+                       }
 
                        XmlTextReader reader = null;
 
                        try {
-                               FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
+                               if (fs == null)
+                                       fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
                                reader = new XmlTextReader (fs);
                                InitRead (reader);
                                ReadConfig (reader, false);