CachingCompiler.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. results = (CompilerResults) cache [key];
  78. #if NET_2_0
  79. if (!compiler.IsRebuildingPartial)
  80. #endif
  81. if (results != null)
  82. return results;
  83. ICodeCompiler comp = compiler.Compiler;
  84. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  85. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  86. cache.InsertPrivate (key, results, new CacheDependency (deps));
  87. } finally {
  88. Monitor.Exit (ticket);
  89. if (acquired)
  90. ReleaseCompilationTicket (key);
  91. }
  92. return results;
  93. }
  94. public static CompilerResults Compile (WebServiceCompiler compiler)
  95. {
  96. string key = cachePrefix + compiler.Parser.PhysicalPath;
  97. Cache cache = HttpRuntime.Cache;
  98. CompilerResults results = (CompilerResults) cache [key];
  99. if (results != null)
  100. return results;
  101. object ticket;
  102. bool acquired = AcquireCompilationTicket (key, out ticket);
  103. try {
  104. Monitor.Enter (ticket);
  105. results = (CompilerResults) cache [key];
  106. if (results != null)
  107. return results;
  108. SimpleWebHandlerParser parser = compiler.Parser;
  109. CompilerParameters options = compiler.CompilerOptions;
  110. options.IncludeDebugInformation = parser.Debug;
  111. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  112. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  113. cache.InsertPrivate (key, results, new CacheDependency (deps));
  114. } finally {
  115. Monitor.Exit (ticket);
  116. if (acquired)
  117. ReleaseCompilationTicket (key);
  118. }
  119. return results;
  120. }
  121. internal static CompilerParameters GetOptions (ICollection assemblies)
  122. {
  123. CompilerParameters options = new CompilerParameters ();
  124. if (assemblies != null) {
  125. StringCollection coll = options.ReferencedAssemblies;
  126. foreach (string str in assemblies)
  127. coll.Add (str);
  128. }
  129. return options;
  130. }
  131. public static CompilerResults Compile (string language, string key, string file,
  132. ArrayList assemblies)
  133. {
  134. Cache cache = HttpRuntime.Cache;
  135. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  136. if (results != null)
  137. return results;
  138. if (!Directory.Exists (dynamicBase))
  139. Directory.CreateDirectory (dynamicBase);
  140. object ticket;
  141. bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
  142. try {
  143. Monitor.Enter (ticket);
  144. results = (CompilerResults) cache [cachePrefix + key];
  145. if (results != null)
  146. return results;
  147. #if NET_2_0
  148. CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  149. Compiler c = config.Compilers[language];
  150. Type t = Type.GetType (c.Type, true);
  151. CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
  152. #else
  153. CompilationConfiguration config;
  154. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  155. CodeDomProvider provider = config.GetProvider (language);
  156. #endif
  157. if (provider == null)
  158. throw new HttpException ("Configuration error. Language not supported: " +
  159. language, 500);
  160. ICodeCompiler compiler = provider.CreateCompiler ();
  161. CompilerParameters options = GetOptions (assemblies);
  162. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
  163. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  164. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  165. results = compiler.CompileAssemblyFromFile (options, file);
  166. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  167. realdeps.Add (file);
  168. for (int i = assemblies.Count - 1; i >= 0; i--) {
  169. string current = (string) assemblies [i];
  170. if (Path.IsPathRooted (current))
  171. realdeps.Add (current);
  172. }
  173. string [] deps = (string []) realdeps.ToArray (typeof (string));
  174. cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
  175. } finally {
  176. Monitor.Exit (ticket);
  177. if (acquired)
  178. ReleaseCompilationTicket (cachePrefix + key);
  179. }
  180. return results;
  181. }
  182. public static Type CompileAndGetType (string typename, string language, string key,
  183. string file, ArrayList assemblies)
  184. {
  185. CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
  186. if (result.NativeCompilerReturnValue != 0) {
  187. StreamReader reader = new StreamReader (file);
  188. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  189. }
  190. Assembly assembly = result.CompiledAssembly;
  191. if (assembly == null) {
  192. StreamReader reader = new StreamReader (file);
  193. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  194. }
  195. Type type = assembly.GetType (typename, true);
  196. InsertType (type, file);
  197. return type;
  198. }
  199. static bool AcquireCompilationTicket (string key, out object ticket)
  200. {
  201. lock (compilationTickets.SyncRoot) {
  202. ticket = compilationTickets [key];
  203. if (ticket == null) {
  204. ticket = new object ();
  205. compilationTickets [key] = ticket;
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. static void ReleaseCompilationTicket (string key)
  212. {
  213. lock (compilationTickets.SyncRoot) {
  214. compilationTickets.Remove (key);
  215. }
  216. }
  217. }
  218. }