2009-06-05 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / WebConfigurationHost.cs
index f4e3e9879f211c532d3d50ca6a641d7b503ae2ec..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,
@@ -90,58 +91,10 @@ namespace System.Web.Configuration
                {
                        return configPath + "/" + locatinSubPath;
                }
-
-               private static string privateBinPath;
-
-               private static string PrivateBinPath {
-                       get {
-                               if (privateBinPath != null)
-                                       return privateBinPath;
-                               
-                               AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
-                               privateBinPath = Path.Combine(setup.ApplicationBase, setup.PrivateBinPath);
-                               return privateBinPath;
-                       }
-               }
-               
-               private Type LoadType(string typeName)
-               {
-                       Type type = Type.GetType (typeName);
-                       if (type != null)
-                               return type;
-
-                       IList tla = System.Web.Compilation.BuildManager.TopLevelAssemblies;
-                       if (tla != null && tla.Count > 0) {
-                               foreach (Assembly asm in tla) {
-                                       if (asm == null)
-                                               continue;
-                                       type = asm.GetType (typeName, false);
-                                       if (type != null)
-                                               break;
-                               }
-                       }
-                       if (type != null)
-                               return type;
-                       
-                       if (!Directory.Exists (PrivateBinPath))
-                               return null;
-                       
-                       string[] binDlls = Directory.GetFiles(PrivateBinPath, "*.dll");
-                       foreach (string s in binDlls) {
-                               Assembly binA = Assembly.LoadFrom (s);
-                               type = binA.GetType (typeName);
-                               if (type == null)
-                                       continue;
-                               
-                               return type;
-                       }
-                       
-                       return null;
-               }
                
                public virtual Type GetConfigType (string typeName, bool throwOnError)
                {
-                       Type type = LoadType(typeName);
+                       Type type = HttpApplication.LoadType (typeName);
                        if (type == null && throwOnError)
                                throw new ConfigurationErrorsException ("Type not found: '" + typeName + "'");
                        return type;
@@ -170,7 +123,19 @@ namespace System.Web.Configuration
 
                                if (map == null)
 #if TARGET_J2EE
+                               {
+                                       // check META-INF/web.config exists
+                                       java.lang.ClassLoader cl = (java.lang.ClassLoader) AppDomain.CurrentDomain.GetData ("GH_ContextClassLoader");
+                                       if (cl == null)
+                                               return null;
+                                       java.io.InputStream wcs = cl.getResourceAsStream ("META-INF/web.config");
+                                       if (wcs == null)
+                                               return null;
+
+                                       wcs.close ();
+
                                        return "/META-INF/web.config";
+                               }
 #else
                                        mdir = Path.GetDirectoryName (System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile);
 #endif
@@ -180,12 +145,8 @@ namespace System.Web.Configuration
                                return GetWebConfigFileName (mdir);
                        }
                        
-                       try {
-                               string dir = MapPath (configPath);
-                               return GetWebConfigFileName (dir);
-                       } catch (Exception ex) {
-                               throw new HttpException (400, "Bad Request");
-                       }
+                       string dir = MapPath (configPath);
+                       return GetWebConfigFileName (dir);
                }
                
                public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
@@ -225,7 +186,11 @@ namespace System.Web.Configuration
                        } else {
                                int i;
                                if (locationSubPath == null)
+                               {
                                        configPath = fullPath;
+                                       if (configPath.Length > 1)
+                                               configPath = VirtualPathUtility.RemoveTrailingSlash (configPath);
+                               }
                                else
                                        configPath = locationSubPath;
 
@@ -251,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)
@@ -310,19 +280,68 @@ namespace System.Web.Configuration
                        throw new HttpException ("Invalid virtual directory: " + virtualPath);
                }
 
-               string GetWebConfigFileName (string dir)
+               internal static string GetWebConfigFileName (string dir)
                {
-                       string[] filenames = new string[] {"Web.Config", "Web.config", "web.config" };
+#if TARGET_J2EE
+                       DirectoryInfo d = GetCaseSensitiveExistingDirectory (new DirectoryInfo (dir));
+                       if (d == null)
+                               return null;
 
-                       foreach (string fn in filenames) {
-                               string file = Path.Combine (dir, fn);
-                               if (File.Exists (file))
-                                       return file;
-                       }
+                       FileInfo file = (FileInfo) FindByName ("web.config", d.GetFiles ("W*"));
+                       if (file == null)
+                               file = (FileInfo) FindByName ("web.config", d.GetFiles ("w*"));
 
+                       if (file != null)
+                               return file.FullName;
+#else
+                       AppDomain domain = AppDomain.CurrentDomain;
+                       bool hosted = (domain.GetData (ApplicationHost.MonoHostedDataKey) as string) == "yes";
+
+                       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                 
                        return null;
                }
+#if TARGET_J2EE
+               static DirectoryInfo GetCaseSensitiveExistingDirectory (DirectoryInfo dir) {
+                       if (dir == null)
+                               return null;
+                       if (dir.Exists)
+                               return dir;
+
+                       DirectoryInfo parent = GetCaseSensitiveExistingDirectory (dir.Parent);
+                       if (parent == null)
+                               return null;
+
+                       return (DirectoryInfo) FindByName (dir.Name, parent.GetDirectories ());
+               }
                
+               static FileSystemInfo FindByName (string name, FileSystemInfo [] infos)
+               {
+                       for (int i = 0; i < infos.Length; i++) {
+                               if (String.Compare (name, infos [i].Name, StringComparison.OrdinalIgnoreCase) == 0)
+                                       return infos [i];
+                       }
+                       return null;
+               }
+#endif
                public virtual bool IsAboveApplication (string configPath)
                {
                        throw new NotImplementedException ();
@@ -341,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;
                        }
@@ -369,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