CachingCompiler.cs 6.3 KB

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