2009-06-05 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / WebConfigurationHost.cs
index 9d85f4d00809a7c3c40ef5276a583a410257578d..24cd3b01150530ebcc5bbb35763415652ceff83f 100644 (file)
@@ -34,6 +34,7 @@ using System.IO;
 using System.Security;
 using System.Configuration;
 using System.Configuration.Internal;
+using System.Web.Hosting;
 using System.Web.Util;
 using System.Reflection;
 
@@ -66,7 +67,7 @@ namespace System.Web.Configuration
                
                public virtual object CreateDeprecatedConfigContext (string configPath)
                {
-                       throw new NotImplementedException ();
+                       return new HttpConfigurationContext(configPath);
                }
                
                public virtual string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider,
@@ -127,10 +128,12 @@ namespace System.Web.Configuration
                                        java.lang.ClassLoader cl = (java.lang.ClassLoader) AppDomain.CurrentDomain.GetData ("GH_ContextClassLoader");
                                        if (cl == null)
                                                return null;
-                                       java.net.URL url = cl.getResource ("META-INF/web.config");
-                                       if (url == null)
+                                       java.io.InputStream wcs = cl.getResourceAsStream ("META-INF/web.config");
+                                       if (wcs == null)
                                                return null;
 
+                                       wcs.close ();
+
                                        return "/META-INF/web.config";
                                }
 #else
@@ -213,6 +216,11 @@ namespace System.Web.Configuration
                
                public string MapPath (string virtualPath)
                {
+                       if (!String.IsNullOrEmpty (virtualPath)) {
+                               if (virtualPath.StartsWith (System.Web.Compilation.BuildManager.FAKE_VIRTUAL_PATH_PREFIX, StringComparison.Ordinal))
+                                       return HttpRuntime.AppDomainAppPath;
+                       }
+                       
                        if (map != null)
                                return MapPathFromMapper (virtualPath);
                        else if (HttpContext.Current != null && HttpContext.Current.Request != null)
@@ -272,7 +280,7 @@ namespace System.Web.Configuration
                        throw new HttpException ("Invalid virtual directory: " + virtualPath);
                }
 
-               string GetWebConfigFileName (string dir)
+               internal static string GetWebConfigFileName (string dir)
                {
 #if TARGET_J2EE
                        DirectoryInfo d = GetCaseSensitiveExistingDirectory (new DirectoryInfo (dir));
@@ -286,18 +294,35 @@ namespace System.Web.Configuration
                        if (file != null)
                                return file.FullName;
 #else
-                       string[] filenames = new string[] {"Web.Config", "Web.config", "web.config" };
+                       AppDomain domain = AppDomain.CurrentDomain;
+                       bool hosted = (domain.GetData (ApplicationHost.MonoHostedDataKey) as string) == "yes";
 
-                       foreach (string fn in filenames) {
-                               string file = Path.Combine (dir, fn);
-                               if (File.Exists (file))
-                                       return file;
+                       if (hosted) {
+                               foreach (string fn in ApplicationHost.WebConfigFileNames) {
+                                       string file = Path.Combine (dir, fn);
+                                       if (File.Exists (file))
+                                               return file;
+                               }
+                       } else {
+                               Assembly asm = Assembly.GetEntryAssembly () ?? Assembly.GetCallingAssembly ();
+                               string name = Path.GetFileName (asm.Location);
+                               string[] fileNames = new string[] {name + ".config", name + ".Config"};
+                               string appDir = domain.BaseDirectory;
+                               string file;
+
+                               foreach (string fn in fileNames) {
+                                       file = Path.Combine (appDir, fn);
+                                       if (File.Exists (file))
+                                               return file;
+                               }
                        }
-#endif
+#endif                 
                        return null;
                }
 #if TARGET_J2EE
                static DirectoryInfo GetCaseSensitiveExistingDirectory (DirectoryInfo dir) {
+                       if (dir == null)
+                               return null;
                        if (dir.Exists)
                                return dir;
 
@@ -335,8 +360,11 @@ namespace System.Web.Configuration
                                        return configPath == MachinePath || configPath == MachineWebPath;
                                case ConfigurationAllowDefinition.MachineToWebRoot:
                                case ConfigurationAllowDefinition.MachineToApplication:
-                                       return configPath == MachinePath || configPath == MachineWebPath || configPath == "/" ||
-                                               configPath == HttpRuntime.AppDomainAppVirtualPath;
+                                       return (String.Compare (configPath, MachinePath, StringComparison.Ordinal) == 0) ||
+                                               (String.Compare (configPath, MachineWebPath, StringComparison.Ordinal) == 0) ||
+                                               (String.Compare (configPath, "/", StringComparison.Ordinal) == 0) ||
+                                               (String.Compare (configPath, "~", StringComparison.Ordinal) == 0) ||
+                                               (String.Compare (configPath, HttpRuntime.AppDomainAppVirtualPath) == 0);
                                default:
                                        return true;
                        }
@@ -363,7 +391,7 @@ namespace System.Web.Configuration
                                        java.lang.ClassLoader cl = (java.lang.ClassLoader) AppDomain.CurrentDomain.GetData ("GH_ContextClassLoader");
                                        if (cl != null) {
                                                java.io.InputStream inputStream = cl.getResourceAsStream (streamName);
-                                               return (Stream) vmw.common.IOUtils.getStream (inputStream);
+                                               return new System.Web.J2EE.J2EEUtils.InputStreamWrapper (inputStream);
                                        }
                                }
 #endif