CachingCompiler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 (results != null)
  68. return results;
  69. lock (compilationLock) {
  70. results = (CompilerResults) cache [key];
  71. if (results != null)
  72. return results;
  73. ICodeCompiler comp = compiler.Compiler;
  74. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  75. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  76. cache.InsertPrivate (key, results, new CacheDependency (deps));
  77. }
  78. return results;
  79. }
  80. public static CompilerResults Compile (WebServiceCompiler compiler)
  81. {
  82. string key = cachePrefix + compiler.Parser.PhysicalPath;
  83. Cache cache = HttpRuntime.Cache;
  84. CompilerResults results = (CompilerResults) cache [key];
  85. if (results != null)
  86. return results;
  87. lock (compilationLock) {
  88. results = (CompilerResults) cache [key];
  89. if (results != null)
  90. return results;
  91. SimpleWebHandlerParser parser = compiler.Parser;
  92. CompilerParameters options = compiler.CompilerOptions;
  93. options.IncludeDebugInformation = parser.Debug;
  94. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  95. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  96. cache.InsertPrivate (key, results, new CacheDependency (deps));
  97. }
  98. return results;
  99. }
  100. internal static CompilerParameters GetOptions (ICollection assemblies)
  101. {
  102. CompilerParameters options = new CompilerParameters ();
  103. if (assemblies != null) {
  104. StringCollection coll = options.ReferencedAssemblies;
  105. foreach (string str in assemblies)
  106. coll.Add (str);
  107. }
  108. return options;
  109. }
  110. public static CompilerResults Compile (string language, string key, string file,
  111. ArrayList assemblies)
  112. {
  113. Cache cache = HttpRuntime.Cache;
  114. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  115. if (results != null)
  116. return results;
  117. if (!Directory.Exists (dynamicBase))
  118. Directory.CreateDirectory (dynamicBase);
  119. lock (compilationLock) {
  120. results = (CompilerResults) cache [cachePrefix + key];
  121. if (results != null)
  122. return results;
  123. #if CONFIGURATION_2_0
  124. CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  125. Compiler c = config.Compilers[language];
  126. CodeDomProvider provider = c.Provider;
  127. #else
  128. CompilationConfiguration config;
  129. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  130. CodeDomProvider provider = config.GetProvider (language);
  131. #endif
  132. if (provider == null)
  133. throw new HttpException ("Configuration error. Language not supported: " +
  134. language, 500);
  135. ICodeCompiler compiler = provider.CreateCompiler ();
  136. CompilerParameters options = GetOptions (assemblies);
  137. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
  138. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  139. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  140. results = compiler.CompileAssemblyFromFile (options, file);
  141. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  142. realdeps.Add (file);
  143. for (int i = assemblies.Count - 1; i >= 0; i--) {
  144. string current = (string) assemblies [i];
  145. if (Path.IsPathRooted (current))
  146. realdeps.Add (current);
  147. }
  148. string [] deps = (string []) realdeps.ToArray (typeof (string));
  149. cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
  150. }
  151. return results;
  152. }
  153. public static Type CompileAndGetType (string typename, string language, string key,
  154. string file, ArrayList assemblies)
  155. {
  156. CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
  157. if (result.NativeCompilerReturnValue != 0) {
  158. StreamReader reader = new StreamReader (file);
  159. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  160. }
  161. Assembly assembly = result.CompiledAssembly;
  162. if (assembly == null) {
  163. StreamReader reader = new StreamReader (file);
  164. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  165. }
  166. Type type = assembly.GetType (typename, true);
  167. InsertType (type, file);
  168. return type;
  169. }
  170. }
  171. }