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;
94 results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
95 string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
96 cache.InsertPrivate (key, results, new CacheDependency (deps));
98 Monitor.Exit (ticket);
100 ReleaseCompilationTicket (key);
106 public static CompilerResults Compile (WebServiceCompiler compiler)
108 string key = cachePrefix + compiler.Parser.PhysicalPath;
109 Cache cache = HttpRuntime.Cache;
110 CompilerResults results = (CompilerResults) cache [key];
115 bool acquired = AcquireCompilationTicket (key, out ticket);
118 Monitor.Enter (ticket);
119 results = (CompilerResults) cache [key];
123 SimpleWebHandlerParser parser = compiler.Parser;
124 CompilerParameters options = compiler.CompilerOptions;
125 options.IncludeDebugInformation = parser.Debug;
126 results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
127 string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
128 cache.InsertPrivate (key, results, new CacheDependency (deps));
130 Monitor.Exit (ticket);
132 ReleaseCompilationTicket (key);
138 internal static CompilerParameters GetOptions (ICollection assemblies)
140 CompilerParameters options = new CompilerParameters ();
141 if (assemblies != null) {
142 StringCollection coll = options.ReferencedAssemblies;
143 foreach (string str in assemblies)
150 public static CompilerResults Compile (string language, string key, string file,
151 ArrayList assemblies)
153 Cache cache = HttpRuntime.Cache;
154 CompilerResults results = (CompilerResults) cache [cachePrefix + key];
158 if (!Directory.Exists (dynamicBase))
159 Directory.CreateDirectory (dynamicBase);
162 bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
165 Monitor.Enter (ticket);
166 results = (CompilerResults) cache [cachePrefix + key];
171 CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
172 Compiler c = config.Compilers[language];
173 Type t = Type.GetType (c.Type, true);
174 CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
176 CompilationConfiguration config;
177 config = CompilationConfiguration.GetInstance (HttpContext.Current);
178 CodeDomProvider provider = config.GetProvider (language);
180 if (provider == null)
181 throw new HttpException ("Configuration error. Language not supported: " +
183 ICodeCompiler compiler = provider.CreateCompiler ();
184 CompilerParameters options = GetOptions (assemblies);
185 TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
186 string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
187 options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
188 results = compiler.CompileAssemblyFromFile (options, file);
189 ArrayList realdeps = new ArrayList (assemblies.Count + 1);
191 for (int i = assemblies.Count - 1; i >= 0; i--) {
192 string current = (string) assemblies [i];
193 if (Path.IsPathRooted (current))
194 realdeps.Add (current);
197 string [] deps = (string []) realdeps.ToArray (typeof (string));
198 cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
200 Monitor.Exit (ticket);
202 ReleaseCompilationTicket (cachePrefix + key);
208 public static Type CompileAndGetType (string typename, string language, string key,
209 string file, ArrayList assemblies)
211 CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
212 if (result.NativeCompilerReturnValue != 0) {
213 StreamReader reader = new StreamReader (file);
214 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
217 Assembly assembly = result.CompiledAssembly;
218 if (assembly == null) {
219 StreamReader reader = new StreamReader (file);
220 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
223 Type type = assembly.GetType (typename, true);
224 InsertType (type, file);
228 static bool AcquireCompilationTicket (string key, out object ticket)
230 lock (compilationTickets.SyncRoot) {
231 ticket = compilationTickets [key];
232 if (ticket == null) {
233 ticket = new object ();
234 compilationTickets [key] = ticket;
241 static void ReleaseCompilationTicket (string key)
243 lock (compilationTickets.SyncRoot) {
244 compilationTickets.Remove (key);