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";
51 public static void InsertTypeFileDep (Type type, string filename)
53 CacheDependency dep = new CacheDependency (filename);
54 HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
57 public static void InsertType (Type type, string filename)
59 string [] cacheKeys = new string [] { cachePrefix + filename };
60 CacheDependency dep = new CacheDependency (null, cacheKeys);
61 HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
64 public static Type GetTypeFromCache (string filename)
66 return (Type) HttpRuntime.Cache [cacheTypePrefix + filename];
69 public static CompilerResults Compile (BaseCompiler compiler)
71 Cache cache = HttpRuntime.Cache;
72 string key = cachePrefix + compiler.Parser.InputFile;
73 CompilerResults results = (CompilerResults) cache [key];
76 if (!compiler.IsRebuildingPartial)
82 bool acquired = AcquireCompilationTicket (key, out ticket);
85 Monitor.Enter (ticket);
86 results = (CompilerResults) cache [key];
88 if (!compiler.IsRebuildingPartial)
93 ICodeCompiler comp = compiler.Compiler;
95 GetExtraAssemblies (compiler.CompilerParameters);
97 results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
98 string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
99 cache.InsertPrivate (key, results, new CacheDependency (deps));
101 Monitor.Exit (ticket);
103 ReleaseCompilationTicket (key);
109 public static CompilerResults Compile (WebServiceCompiler compiler)
111 string key = cachePrefix + compiler.Parser.PhysicalPath;
112 Cache cache = HttpRuntime.Cache;
113 CompilerResults results = (CompilerResults) cache [key];
118 bool acquired = AcquireCompilationTicket (key, out ticket);
121 Monitor.Enter (ticket);
122 results = (CompilerResults) cache [key];
126 SimpleWebHandlerParser parser = compiler.Parser;
127 CompilerParameters options = compiler.CompilerOptions;
128 options.IncludeDebugInformation = parser.Debug;
130 GetExtraAssemblies (options);
132 results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
133 string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
134 cache.InsertPrivate (key, results, new CacheDependency (deps));
136 Monitor.Exit (ticket);
138 ReleaseCompilationTicket (key);
144 internal static CompilerParameters GetOptions (ICollection assemblies)
146 CompilerParameters options = new CompilerParameters ();
147 if (assemblies != null) {
148 StringCollection coll = options.ReferencedAssemblies;
149 foreach (string str in assemblies)
153 GetExtraAssemblies (options);
158 public static CompilerResults Compile (string language, string key, string file,
159 ArrayList assemblies)
161 Cache cache = HttpRuntime.Cache;
162 CompilerResults results = (CompilerResults) cache [cachePrefix + key];
166 if (!Directory.Exists (dynamicBase))
167 Directory.CreateDirectory (dynamicBase);
170 bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
173 Monitor.Enter (ticket);
174 results = (CompilerResults) cache [cachePrefix + key];
179 CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
180 Compiler c = config.Compilers[language];
181 Type t = Type.GetType (c.Type, true);
182 CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
184 CompilationConfiguration config;
185 config = CompilationConfiguration.GetInstance (HttpContext.Current);
186 CodeDomProvider provider = config.GetProvider (language);
188 if (provider == null)
189 throw new HttpException ("Configuration error. Language not supported: " +
191 ICodeCompiler compiler = provider.CreateCompiler ();
192 CompilerParameters options = GetOptions (assemblies);
193 TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
194 string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
195 options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
196 results = compiler.CompileAssemblyFromFile (options, file);
197 ArrayList realdeps = new ArrayList (assemblies.Count + 1);
199 for (int i = assemblies.Count - 1; i >= 0; i--) {
200 string current = (string) assemblies [i];
201 if (Path.IsPathRooted (current))
202 realdeps.Add (current);
205 string [] deps = (string []) realdeps.ToArray (typeof (string));
206 cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
208 Monitor.Exit (ticket);
210 ReleaseCompilationTicket (cachePrefix + key);
216 public static Type CompileAndGetType (string typename, string language, string key,
217 string file, ArrayList assemblies)
219 CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
220 if (result.NativeCompilerReturnValue != 0) {
221 StreamReader reader = new StreamReader (file);
222 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
225 Assembly assembly = result.CompiledAssembly;
226 if (assembly == null) {
227 StreamReader reader = new StreamReader (file);
228 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
231 Type type = assembly.GetType (typename, true);
232 InsertType (type, file);
237 static void GetExtraAssemblies (CompilerParameters options)
239 ArrayList al = WebConfigurationManager.ExtraAssemblies;
240 StringCollection refAsm = options.ReferencedAssemblies;
243 if (al != null && al.Count > 0) {
244 foreach (object o in al) {
245 asmName = o as string;
246 if (asmName != null && !refAsm.Contains (asmName))
247 refAsm.Add (asmName);
251 IList list = BuildManager.CodeAssemblies;
252 if (list != null && list.Count > 0) {
253 foreach (object o in list) {
254 asmName = o as string;
255 if (asmName != null && !refAsm.Contains (asmName))
256 refAsm.Add (asmName);
260 list = BuildManager.TopLevelAssemblies;
261 if (list != null && list.Count > 0) {
262 foreach (Assembly a in list) {
263 asmName = a.Location;
264 if (!refAsm.Contains (asmName))
265 refAsm.Add (asmName);
271 static bool AcquireCompilationTicket (string key, out object ticket)
273 lock (compilationTickets.SyncRoot) {
274 ticket = compilationTickets [key];
275 if (ticket == null) {
276 ticket = new object ();
277 compilationTickets [key] = ticket;
284 static void ReleaseCompilationTicket (string key)
286 lock (compilationTickets.SyncRoot) {
287 compilationTickets.Remove (key);