CachingCompiler.cs 5.8 KB

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