2 // System.Web.Compilation.CachingCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // (c) Copyright Novell, Inc. (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.CodeDom.Compiler;
33 using System.Collections;
34 using System.Collections.Specialized;
36 using System.Reflection;
37 using System.Threading;
39 using System.Web.Caching;
40 using System.Web.Configuration;
42 namespace System.Web.Compilation
46 static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
47 static Hashtable compilationTickets = new Hashtable ();
48 const string cachePrefix = "@@Assembly";
49 const string cacheTypePrefix = "@@@Type";
50 static Hashtable assemblyCache = new Hashtable ();
52 public static void InsertTypeFileDep (Type type, string filename)
54 CacheDependency dep = new CacheDependency (filename);
55 HttpRuntime.InternalCache.Insert (cacheTypePrefix + filename, type, dep);
58 public static void InsertType (Type type, string filename)
60 string [] cacheKeys = new string [] { cachePrefix + filename };
61 CacheDependency dep = new CacheDependency (null, cacheKeys);
62 HttpRuntime.InternalCache.Insert (cacheTypePrefix + filename, type, dep);
65 public static Type GetTypeFromCache (string filename)
67 return (Type) HttpRuntime.InternalCache [cacheTypePrefix + filename];
70 public static CompilerResults Compile (BaseCompiler compiler)
72 Cache cache = HttpRuntime.InternalCache;
73 string key = cachePrefix + compiler.Parser.InputFile;
74 CompilerResults results = (CompilerResults) cache [key];
77 if (!compiler.IsRebuildingPartial)
83 bool acquired = AcquireCompilationTicket (key, out ticket);
86 Monitor.Enter (ticket);
87 results = (CompilerResults) cache [key];
89 if (!compiler.IsRebuildingPartial)
94 ICodeCompiler comp = compiler.Compiler;
95 GetExtraAssemblies (compiler.CompilerParameters);
96 results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
97 string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
98 cache.Insert (key, results, new CacheDependency (deps));
100 Monitor.Exit (ticket);
102 ReleaseCompilationTicket (key);
108 public static CompilerResults Compile (WebServiceCompiler compiler)
110 string key = cachePrefix + compiler.Parser.PhysicalPath;
111 Cache cache = HttpRuntime.InternalCache;
112 CompilerResults results = (CompilerResults) cache [key];
117 bool acquired = AcquireCompilationTicket (key, out ticket);
120 Monitor.Enter (ticket);
121 results = (CompilerResults) cache [key];
125 SimpleWebHandlerParser parser = compiler.Parser;
126 CompilerParameters options = compiler.CompilerOptions;
127 options.IncludeDebugInformation = parser.Debug;
128 GetExtraAssemblies (options);
129 results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
130 string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
131 cache.Insert (key, results, new CacheDependency (deps));
133 Monitor.Exit (ticket);
135 ReleaseCompilationTicket (key);
141 internal static CompilerParameters GetOptions (ICollection assemblies)
143 CompilerParameters options = new CompilerParameters ();
144 if (assemblies != null) {
145 StringCollection coll = options.ReferencedAssemblies;
146 foreach (string str in assemblies)
149 GetExtraAssemblies (options);
153 public static CompilerResults Compile (string language, string key, string file,
154 ArrayList assemblies)
156 Cache cache = HttpRuntime.InternalCache;
157 CompilerResults results = (CompilerResults) cache [cachePrefix + key];
161 if (!Directory.Exists (dynamicBase))
162 Directory.CreateDirectory (dynamicBase);
165 bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
168 Monitor.Enter (ticket);
169 results = (CompilerResults) cache [cachePrefix + key];
174 CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
175 Compiler c = config.Compilers[language];
176 Type t = Type.GetType (c.Type, true);
177 CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
179 CompilationConfiguration config;
180 config = CompilationConfiguration.GetInstance (HttpContext.Current);
181 CodeDomProvider provider = config.GetProvider (language);
183 if (provider == null)
184 throw new HttpException ("Configuration error. Language not supported: " +
186 ICodeCompiler compiler = provider.CreateCompiler ();
187 CompilerParameters options = GetOptions (assemblies);
188 TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
189 string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
190 options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
191 results = compiler.CompileAssemblyFromFile (options, file);
192 ArrayList realdeps = new ArrayList (assemblies.Count + 1);
194 for (int i = assemblies.Count - 1; i >= 0; i--) {
195 string current = (string) assemblies [i];
196 if (Path.IsPathRooted (current))
197 realdeps.Add (current);
200 string [] deps = (string []) realdeps.ToArray (typeof (string));
201 cache.Insert (cachePrefix + key, results, new CacheDependency (deps));
203 Monitor.Exit (ticket);
205 ReleaseCompilationTicket (cachePrefix + key);
211 public static Type CompileAndGetType (string typename, string language, string key,
212 string file, ArrayList assemblies)
214 CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
215 if (result.NativeCompilerReturnValue != 0) {
216 StreamReader reader = new StreamReader (file);
217 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
220 Assembly assembly = result.CompiledAssembly;
221 if (assembly == null) {
222 StreamReader reader = new StreamReader (file);
223 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
226 Type type = assembly.GetType (typename, true);
227 InsertType (type, file);
231 static void GetExtraAssemblies (CompilerParameters options)
233 StringCollection refAsm = options.ReferencedAssemblies;
234 string asmName, asmLocation;
237 ArrayList al = WebConfigurationManager.ExtraAssemblies;
239 if (al != null && al.Count > 0) {
240 foreach (object o in al) {
241 asmName = o as string;
242 if (asmName != null && !refAsm.Contains (asmName))
243 refAsm.Add (asmName);
248 IList list = BuildManager.CodeAssemblies;
249 if (list != null && list.Count > 0) {
250 foreach (object o in list) {
254 asmName = asm.Location;
255 if (asmName != null && !refAsm.Contains (asmName))
256 refAsm.Add (asmName);
260 list = BuildManager.TopLevelAssemblies;
261 if (list != null && list.Count > 0) {
262 foreach (object o in list) {
266 asmName = asm.Location;
267 if (!refAsm.Contains (asmName))
268 refAsm.Add (asmName);
272 CompilationSection cfg = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
273 AssemblyCollection asmcoll = cfg != null ? cfg.Assemblies : null;
278 foreach (AssemblyInfo ai in asmcoll) {
279 asmLocation = GetAssemblyLocationFromName (ai.Assembly);
281 if (asmLocation == null || refAsm.Contains (asmLocation))
283 refAsm.Add (asmLocation);
286 CompilationConfiguration cfg = CompilationConfiguration.GetInstance (HttpContext.Current);
287 ArrayList asmcoll = cfg != null ? cfg.Assemblies : null;
292 foreach (string asm in asmcoll) {
293 asmLocation = GetAssemblyLocationFromName (asm);
295 if (asmLocation == null || refAsm.Contains (asmLocation))
297 refAsm.Add (asmLocation);
302 static string GetAssemblyLocationFromName (string name)
304 Assembly asm = assemblyCache [name] as Assembly;
309 asm = Assembly.Load (name);
316 assemblyCache [name] = asm;
320 static bool AcquireCompilationTicket (string key, out object ticket)
322 lock (compilationTickets.SyncRoot) {
323 ticket = compilationTickets [key];
324 if (ticket == null) {
325 ticket = new object ();
326 compilationTickets [key] = ticket;
333 static void ReleaseCompilationTicket (string key)
335 lock (compilationTickets.SyncRoot) {
336 compilationTickets.Remove (key);