Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / System.Web / System.Web.Compilation / BuildManager.cs
1 //
2 // System.Web.Compilation.BuildManager
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //      Gonzalo Paniagua Javier (gonzalo@novell.com)
7 //
8 // (C) 2006 Novell, Inc (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 #if NET_2_0
33
34 using System;
35 using System.CodeDom;
36 using System.CodeDom.Compiler;
37 using System.Collections;
38 using System.Collections.Generic;
39 using System.IO;
40 using System.Reflection;
41 using System.Web;
42 using System.Web.Configuration;
43 using System.Web.Hosting;
44
45 namespace System.Web.Compilation {
46         public sealed class BuildManager {
47                 private static List<Assembly> AppCode_Assemblies = new List<Assembly>();
48                 private static List<Assembly> TopLevel_Assemblies = new List<Assembly>();
49                 private static bool haveResources;
50                 
51                 internal BuildManager ()
52                 {
53                 }
54
55                 internal static void ThrowNoProviderException (string extension)
56                 {
57                         string msg = "No registered provider for extension '{0}'.";
58                         throw new HttpException (String.Format (msg, extension));
59                 }
60
61                 public static object CreateInstanceFromVirtualPath (string virtualPath, Type requiredBaseType)
62                 {
63                         // virtualPath + Exists done in GetCompiledType()
64                         if (requiredBaseType == null)
65                                 throw new NullReferenceException (); // This is what MS does, but from somewhere else.
66
67                         // Get the Type.
68                         Type type = GetCompiledType (virtualPath);
69                         if (!requiredBaseType.IsAssignableFrom (type)) {
70                                 string msg = String.Format ("Type '{0}' does not inherit from '{1}'.",
71                                                                 type.FullName, requiredBaseType.FullName);
72                                 throw new HttpException (500, msg);
73                         }
74
75                         return Activator.CreateInstance (type, null);
76                 }
77
78                 [MonoTODO ("Not implemented, always returns null")]
79                 public static BuildDependencySet GetCachedBuildDependencySet (HttpContext context, string virtualPath)
80                 {
81                         return null; // null is ok here until we store the dependency set in the Cache.
82                 }
83
84                 internal static BuildProvider GetBuildProviderForPath (string virtualPath, bool throwOnMissing)
85                 {
86                         string extension = VirtualPathUtility.GetExtension (virtualPath);
87                         object o = WebConfigurationManager.GetSection ("system.web/compilation/buildProviders", virtualPath);
88                         BuildProviderCollection coll = o as BuildProviderCollection;
89                         if (coll == null || coll.Count == 0)
90                                 ThrowNoProviderException (extension);
91                         
92                         BuildProvider provider = coll.GetProviderForExtension (extension);
93                         if (provider == null && throwOnMissing)
94                                 ThrowNoProviderException (extension);
95                         return provider;
96                 }
97                 
98                 public static Assembly GetCompiledAssembly (string virtualPath)
99                 {
100                         BuildProvider provider;
101                         CompilerParameters parameters;
102
103                         AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
104                         CompilerType ctype = provider.CodeCompilerType;
105                         parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
106                         CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
107                         return results.CompiledAssembly;
108                 }
109
110                 static AssemblyBuilder GetAssemblyBuilder (string virtualPath, out BuildProvider provider)
111                 {
112                         if (virtualPath == null || virtualPath == "")
113                                 throw new ArgumentNullException ("virtualPath");
114
115                         if (virtualPath [0] != '/')
116                                 throw new ArgumentException ("The virtual path is not rooted", "virtualPath");
117
118                         if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
119                                 throw new HttpException (String.Format ("The file '{0}' does not exist.", virtualPath));
120                         provider = GetBuildProviderForPath (virtualPath, true);
121                         CompilerType compiler_type = provider.CodeCompilerType;
122                         Type ctype = compiler_type.CodeDomProviderType;
123                         CodeDomProvider dom_provider = (CodeDomProvider) Activator.CreateInstance (ctype, null);
124
125                         AssemblyBuilder abuilder = new AssemblyBuilder (virtualPath, dom_provider);
126                         provider.SetVirtualPath (virtualPath);
127                         provider.GenerateCode (abuilder);
128                         return abuilder;
129                 }
130
131                 public static string GetCompiledCustomString (string virtualPath)
132                 {
133                         BuildProvider provider = GetBuildProviderForPath (virtualPath, false);
134                         if (provider == null)
135                                 return null;
136                         // FIXME: where to get the CompilerResults from?
137                         return provider.GetCustomString (null);
138                 }
139
140                 static CompilerParameters PrepareParameters (TempFileCollection temp_files,
141                                                                 string virtualPath,
142                                                                 CompilerParameters base_params)
143                 {
144                         CompilerParameters res = new CompilerParameters ();
145                         res.TempFiles = temp_files;
146                         res.CompilerOptions = base_params.CompilerOptions;
147                         res.IncludeDebugInformation = base_params.IncludeDebugInformation;
148                         res.TreatWarningsAsErrors = base_params.TreatWarningsAsErrors;
149                         res.WarningLevel = base_params.WarningLevel;
150                         string dllfilename = Path.GetFileName (temp_files.AddExtension ("dll", true));
151                         res.OutputAssembly = Path.Combine (temp_files.TempDir, dllfilename);
152                         return res;
153                 }
154
155                 public static Type GetCompiledType (string virtualPath)
156                 {
157                         BuildProvider provider;
158                         CompilerParameters parameters;
159
160                         AssemblyBuilder abuilder = GetAssemblyBuilder (virtualPath, out provider);
161                         CompilerType ctype = provider.CodeCompilerType;
162                         parameters = PrepareParameters (abuilder.TempFiles, virtualPath, ctype.CompilerParameters);
163                         CompilerResults results = abuilder.BuildAssembly (virtualPath, parameters);
164                         return provider.GetGeneratedType (results);
165                 }
166
167                 // The 2 GetType() overloads work on the global.asax, App_GlobalResources, App_WebReferences or App_Browsers
168                 public static Type GetType (string typeName, bool throwOnError)
169                 {
170                         return GetType (typeName, throwOnError, false);
171                 }
172
173                 public static Type GetType (string typeName, bool throwOnError, bool ignoreCase)
174                 {
175                         Type ret = null;
176                         try {
177                                 foreach (Assembly asm in TopLevel_Assemblies) {
178                                         ret = asm.GetType (typeName, throwOnError, ignoreCase);
179                                         if (ret != null)
180                                                 break;
181                                 }
182                         } catch (Exception ex) {
183                                 throw new HttpException ("Failed to find the specified type.", ex);
184                         }
185                         return ret;
186                 }
187
188                 internal static ICollection GetVirtualPathDependencies (string virtualPath, BuildProvider bprovider)
189                 {
190                         BuildProvider provider = bprovider;
191                         if (provider == null)
192                                 provider = GetBuildProviderForPath (virtualPath, false);
193                         if (provider == null)
194                                 return null;
195                         return provider.VirtualPathDependencies;
196                 }
197                 
198                 public static ICollection GetVirtualPathDependencies (string virtualPath)
199                 {
200                         return GetVirtualPathDependencies (virtualPath, null);
201                 }
202
203                 // Assemblies built from the App_Code directory
204                 public static IList CodeAssemblies {
205                         get { return AppCode_Assemblies; }
206                 }
207
208                 internal static IList TopLevelAssemblies {
209                         get { return TopLevel_Assemblies; }
210                 }
211
212                 internal static bool HaveResources {
213                         get { return haveResources; }
214                         set { haveResources = value; }
215                 }
216         }
217 }
218
219 #endif
220