CachingCompiler.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.Web.UI;
  37. using System.Web.Caching;
  38. using System.Web.Configuration;
  39. namespace System.Web.Compilation
  40. {
  41. class CachingCompiler
  42. {
  43. static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  44. static object compilationLock = new object ();
  45. const string cachePrefix = "@@Assembly";
  46. const string cacheTypePrefix = "@@@Type";
  47. public static void InsertTypeFileDep (Type type, string filename)
  48. {
  49. CacheDependency dep = new CacheDependency (filename);
  50. HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
  51. }
  52. public static void InsertType (Type type, string filename)
  53. {
  54. string [] cacheKeys = new string [] { cachePrefix + filename };
  55. CacheDependency dep = new CacheDependency (null, cacheKeys);
  56. HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
  57. }
  58. public static Type GetTypeFromCache (string filename)
  59. {
  60. return (Type) HttpRuntime.Cache [cacheTypePrefix + filename];
  61. }
  62. public static CompilerResults Compile (BaseCompiler compiler)
  63. {
  64. Cache cache = HttpRuntime.Cache;
  65. string key = cachePrefix + compiler.Parser.InputFile;
  66. CompilerResults results = (CompilerResults) cache [key];
  67. #if NET_2_0
  68. if (!compiler.IsRebuildingPartial)
  69. #endif
  70. if (results != null)
  71. return results;
  72. lock (compilationLock) {
  73. results = (CompilerResults) cache [key];
  74. #if NET_2_0
  75. if (!compiler.IsRebuildingPartial)
  76. #endif
  77. if (results != null)
  78. return results;
  79. ICodeCompiler comp = compiler.Compiler;
  80. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  81. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  82. cache.InsertPrivate (key, results, new CacheDependency (deps));
  83. }
  84. return results;
  85. }
  86. public static CompilerResults Compile (WebServiceCompiler compiler)
  87. {
  88. string key = cachePrefix + compiler.Parser.PhysicalPath;
  89. Cache cache = HttpRuntime.Cache;
  90. CompilerResults results = (CompilerResults) cache [key];
  91. if (results != null)
  92. return results;
  93. lock (compilationLock) {
  94. results = (CompilerResults) cache [key];
  95. if (results != null)
  96. return results;
  97. SimpleWebHandlerParser parser = compiler.Parser;
  98. CompilerParameters options = compiler.CompilerOptions;
  99. options.IncludeDebugInformation = parser.Debug;
  100. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  101. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  102. cache.InsertPrivate (key, results, new CacheDependency (deps));
  103. }
  104. return results;
  105. }
  106. internal static CompilerParameters GetOptions (ICollection assemblies)
  107. {
  108. CompilerParameters options = new CompilerParameters ();
  109. if (assemblies != null) {
  110. StringCollection coll = options.ReferencedAssemblies;
  111. foreach (string str in assemblies)
  112. coll.Add (str);
  113. }
  114. return options;
  115. }
  116. public static CompilerResults Compile (string language, string key, string file,
  117. ArrayList assemblies)
  118. {
  119. Cache cache = HttpRuntime.Cache;
  120. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  121. if (results != null)
  122. return results;
  123. if (!Directory.Exists (dynamicBase))
  124. Directory.CreateDirectory (dynamicBase);
  125. lock (compilationLock) {
  126. results = (CompilerResults) cache [cachePrefix + key];
  127. if (results != null)
  128. return results;
  129. #if CONFIGURATION_2_0
  130. CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  131. Compiler c = config.Compilers[language];
  132. CodeDomProvider provider = c.Provider;
  133. #else
  134. CompilationConfiguration config;
  135. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  136. CodeDomProvider provider = config.GetProvider (language);
  137. #endif
  138. if (provider == null)
  139. throw new HttpException ("Configuration error. Language not supported: " +
  140. language, 500);
  141. ICodeCompiler compiler = provider.CreateCompiler ();
  142. CompilerParameters options = GetOptions (assemblies);
  143. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
  144. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  145. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  146. results = compiler.CompileAssemblyFromFile (options, file);
  147. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  148. realdeps.Add (file);
  149. for (int i = assemblies.Count - 1; i >= 0; i--) {
  150. string current = (string) assemblies [i];
  151. if (Path.IsPathRooted (current))
  152. realdeps.Add (current);
  153. }
  154. string [] deps = (string []) realdeps.ToArray (typeof (string));
  155. cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
  156. }
  157. return results;
  158. }
  159. public static Type CompileAndGetType (string typename, string language, string key,
  160. string file, ArrayList assemblies)
  161. {
  162. CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
  163. if (result.NativeCompilerReturnValue != 0) {
  164. StreamReader reader = new StreamReader (file);
  165. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  166. }
  167. Assembly assembly = result.CompiledAssembly;
  168. if (assembly == null) {
  169. StreamReader reader = new StreamReader (file);
  170. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  171. }
  172. Type type = assembly.GetType (typename, true);
  173. InsertType (type, file);
  174. return type;
  175. }
  176. }
  177. }