2 // System.Web.Compilation.CachingCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
11 using System.CodeDom.Compiler;
12 using System.Collections;
13 using System.Collections.Specialized;
17 namespace System.Web.Compilation
19 //TODO: caching should be done using System.Web.Caching, but that namespace still need some work.
20 internal class CompilationCacheItem
22 CompilerResults result;
23 ArrayList dependencies;
26 public CompilationCacheItem (CompilerResults result, ArrayList dependencies)
29 this.dependencies = dependencies;
30 this.reference = DateTime.Now;
33 public CompilationCacheItem (CompilerResults result, string file)
36 this.dependencies = new ArrayList (1);
37 dependencies.Add (file);
38 this.reference = DateTime.Now;
41 public bool CheckDependencies (string key)
43 if (dependencies == null)
44 return true; // Forever young
46 foreach (string s in dependencies) {
47 if (!File.Exists (s) || File.GetLastWriteTime (s) > reference)
54 public CompilerResults Result {
55 get { return result; }
59 internal class CompilationCache
61 static Hashtable cache;
62 static CompilationCache instance = new CompilationCache ();
64 private CompilationCache ()
68 static CompilationCache ()
70 cache = new Hashtable ();
73 public static CompilationCache GetInstance ()
78 public bool CheckDependencies (CompilationCacheItem item, string key)
80 bool result = item.CheckDependencies (key);
87 public CompilationCacheItem this [string key] {
88 get { return cache [key] as CompilationCacheItem; }
89 set { cache [key] = value; }
93 internal class CachingCompiler
95 static CompilationCache cache = CompilationCache.GetInstance ();
97 private CachingCompiler () {}
99 public static CompilationCacheItem GetCached (string key)
102 throw new ArgumentNullException ("key");
104 CompilationCacheItem item = cache [key];
105 if (item != null && cache.CheckDependencies (item, key))
111 static object compilationLock = new object ();
112 public static CompilerResults Compile (BaseCompiler compiler)
114 string key = compiler.Parser.InputFile;
115 CompilationCacheItem item = GetCached (key);
119 CompilerResults results = null;
120 lock (compilationLock) {
121 item = GetCached (key);
125 results = compiler.Compiler.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
126 cache [key] = new CompilationCacheItem (results, compiler.Parser.Dependencies);
132 public static CompilerResults Compile (string key, string file, WebServiceCompiler compiler)
134 CompilationCacheItem item = GetCached (key);
138 CompilerResults results = null;
139 lock (compilationLock) {
140 item = GetCached (key);
144 SimpleWebHandlerParser parser = compiler.Parser;
145 CompilerParameters options = GetOptions (parser.Assemblies);
146 options.IncludeDebugInformation = parser.Debug;
147 results = compiler.Compiler.CompileAssemblyFromFile (options, file);
148 cache [key] = new CompilationCacheItem (results, parser.Dependencies);
154 static CompilerParameters GetOptions (ICollection assemblies)
156 CompilerParameters options = new CompilerParameters ();
157 if (assemblies != null) {
158 StringCollection coll = options.ReferencedAssemblies;
159 foreach (string str in assemblies)
166 public static CompilerResults Compile (string key, string file, ArrayList assemblies)
168 CompilationCacheItem item = GetCached (key);
172 CompilerResults results = null;
173 lock (compilationLock) {
174 item = GetCached (file);
178 CompilerParameters options = GetOptions (assemblies);
179 //TODO: support for other languages
180 results = CSCompiler.Compiler.CompileAssemblyFromFile (options, file);
181 cache [key] = new CompilationCacheItem (results, file);