2
0

CachingCompiler.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // System.Web.Compilation.CachingCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. // (c) Copyright Novell, Inc. (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.CodeDom.Compiler;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.IO;
  35. using System.Reflection;
  36. using System.Threading;
  37. using System.Web.UI;
  38. using System.Web.Caching;
  39. using System.Web.Configuration;
  40. namespace System.Web.Compilation
  41. {
  42. class CachingCompiler
  43. {
  44. static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  45. static Hashtable compilationTickets = new Hashtable ();
  46. const string cachePrefix = "@@Assembly";
  47. const string cacheTypePrefix = "@@@Type";
  48. public static void InsertTypeFileDep (Type type, string filename)
  49. {
  50. CacheDependency dep = new CacheDependency (filename);
  51. HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
  52. }
  53. public static void InsertType (Type type, string filename)
  54. {
  55. string [] cacheKeys = new string [] { cachePrefix + filename };
  56. CacheDependency dep = new CacheDependency (null, cacheKeys);
  57. HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
  58. }
  59. public static Type GetTypeFromCache (string filename)
  60. {
  61. return (Type) HttpRuntime.Cache [cacheTypePrefix + filename];
  62. }
  63. public static CompilerResults Compile (BaseCompiler compiler)
  64. {
  65. Cache cache = HttpRuntime.Cache;
  66. string key = cachePrefix + compiler.Parser.InputFile;
  67. CompilerResults results = (CompilerResults) cache [key];
  68. #if NET_2_0
  69. if (!compiler.IsRebuildingPartial)
  70. #endif
  71. if (results != null)
  72. return results;
  73. object ticket;
  74. bool acquired = AcquireCompilationTicket (key, out ticket);
  75. try {
  76. Monitor.Enter (ticket);
  77. if (!acquired)
  78. Monitor.Wait (ticket);
  79. results = (CompilerResults) cache [key];
  80. #if NET_2_0
  81. if (!compiler.IsRebuildingPartial)
  82. #endif
  83. if (results != null)
  84. return results;
  85. ICodeCompiler comp = compiler.Compiler;
  86. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  87. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  88. cache.InsertPrivate (key, results, new CacheDependency (deps));
  89. Monitor.PulseAll (ticket);
  90. } finally {
  91. Monitor.Exit (ticket);
  92. if (acquired)
  93. ReleaseCompilationTicket (key);
  94. }
  95. return results;
  96. }
  97. public static CompilerResults Compile (WebServiceCompiler compiler)
  98. {
  99. string key = cachePrefix + compiler.Parser.PhysicalPath;
  100. Cache cache = HttpRuntime.Cache;
  101. CompilerResults results = (CompilerResults) cache [key];
  102. if (results != null)
  103. return results;
  104. object ticket;
  105. bool acquired = AcquireCompilationTicket (key, out ticket);
  106. try {
  107. Monitor.Enter (ticket);
  108. if (!acquired)
  109. Monitor.Wait (ticket);
  110. results = (CompilerResults) cache [key];
  111. if (results != null)
  112. return results;
  113. SimpleWebHandlerParser parser = compiler.Parser;
  114. CompilerParameters options = compiler.CompilerOptions;
  115. options.IncludeDebugInformation = parser.Debug;
  116. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  117. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  118. cache.InsertPrivate (key, results, new CacheDependency (deps));
  119. Monitor.PulseAll (ticket);
  120. } finally {
  121. Monitor.Exit (ticket);
  122. if (acquired)
  123. ReleaseCompilationTicket (key);
  124. }
  125. return results;
  126. }
  127. internal static CompilerParameters GetOptions (ICollection assemblies)
  128. {
  129. CompilerParameters options = new CompilerParameters ();
  130. if (assemblies != null) {
  131. StringCollection coll = options.ReferencedAssemblies;
  132. foreach (string str in assemblies)
  133. coll.Add (str);
  134. }
  135. return options;
  136. }
  137. public static CompilerResults Compile (string language, string key, string file,
  138. ArrayList assemblies)
  139. {
  140. Cache cache = HttpRuntime.Cache;
  141. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  142. if (results != null)
  143. return results;
  144. if (!Directory.Exists (dynamicBase))
  145. Directory.CreateDirectory (dynamicBase);
  146. object ticket;
  147. bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
  148. try {
  149. Monitor.Enter (ticket);
  150. if (!acquired)
  151. Monitor.Wait (ticket);
  152. results = (CompilerResults) cache [cachePrefix + key];
  153. if (results != null)
  154. return results;
  155. #if NET_2_0
  156. CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  157. Compiler c = config.Compilers[language];
  158. Type t = Type.GetType (c.Type, true);
  159. CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
  160. #else
  161. CompilationConfiguration config;
  162. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  163. CodeDomProvider provider = config.GetProvider (language);
  164. #endif
  165. if (provider == null)
  166. throw new HttpException ("Configuration error. Language not supported: " +
  167. language, 500);
  168. ICodeCompiler compiler = provider.CreateCompiler ();
  169. CompilerParameters options = GetOptions (assemblies);
  170. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
  171. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  172. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  173. results = compiler.CompileAssemblyFromFile (options, file);
  174. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  175. realdeps.Add (file);
  176. for (int i = assemblies.Count - 1; i >= 0; i--) {
  177. string current = (string) assemblies [i];
  178. if (Path.IsPathRooted (current))
  179. realdeps.Add (current);
  180. }
  181. string [] deps = (string []) realdeps.ToArray (typeof (string));
  182. cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
  183. Monitor.PulseAll (ticket);
  184. } finally {
  185. Monitor.Exit (ticket);
  186. if (acquired)
  187. ReleaseCompilationTicket (cachePrefix + key);
  188. }
  189. return results;
  190. }
  191. public static Type CompileAndGetType (string typename, string language, string key,
  192. string file, ArrayList assemblies)
  193. {
  194. CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
  195. if (result.NativeCompilerReturnValue != 0) {
  196. StreamReader reader = new StreamReader (file);
  197. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  198. }
  199. Assembly assembly = result.CompiledAssembly;
  200. if (assembly == null) {
  201. StreamReader reader = new StreamReader (file);
  202. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  203. }
  204. Type type = assembly.GetType (typename, true);
  205. InsertType (type, file);
  206. return type;
  207. }
  208. static bool AcquireCompilationTicket (string key, out object ticket)
  209. {
  210. lock (compilationTickets.SyncRoot) {
  211. ticket = compilationTickets [key];
  212. if (ticket == null) {
  213. ticket = new object ();
  214. compilationTickets [key] = ticket;
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. static void ReleaseCompilationTicket (string key)
  221. {
  222. lock (compilationTickets.SyncRoot) {
  223. compilationTickets.Remove (key);
  224. }
  225. }
  226. }
  227. }