CachingCompiler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.Reflection;
  35. using System.Web.UI;
  36. using System.Web.Caching;
  37. using System.Web.Configuration;
  38. namespace System.Web.Compilation
  39. {
  40. class CachingCompiler
  41. {
  42. static object compilationLock = new object ();
  43. const string cachePrefix = "@@Assembly";
  44. public static Type GetTypeFromCache (string filename, string typename)
  45. {
  46. string key = CachingCompiler.cachePrefix + filename;
  47. CompilerResults results = (CompilerResults) HttpRuntime.Cache [key];
  48. if (results == null)
  49. return null;
  50. Assembly a = results.CompiledAssembly;
  51. if (a == null)
  52. return null;
  53. return a.GetType (typename, false);
  54. }
  55. public static CompilerResults Compile (BaseCompiler compiler)
  56. {
  57. Cache cache = HttpRuntime.Cache;
  58. string key = cachePrefix + compiler.Parser.InputFile;
  59. CompilerResults results = (CompilerResults) cache [key];
  60. if (results != null)
  61. return results;
  62. lock (compilationLock) {
  63. results = (CompilerResults) cache [key];
  64. if (results != null)
  65. return results;
  66. ICodeCompiler comp = compiler.Compiler;
  67. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  68. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  69. cache.Insert (key, results, new CacheDependency (deps));
  70. }
  71. return results;
  72. }
  73. public static CompilerResults Compile (WebServiceCompiler compiler)
  74. {
  75. string key = cachePrefix + compiler.Parser.PhysicalPath;
  76. Cache cache = HttpRuntime.Cache;
  77. CompilerResults results = (CompilerResults) cache [key];
  78. if (results != null)
  79. return results;
  80. lock (compilationLock) {
  81. results = (CompilerResults) cache [key];
  82. if (results != null)
  83. return results;
  84. SimpleWebHandlerParser parser = compiler.Parser;
  85. CompilerParameters options = compiler.CompilerOptions;
  86. options.IncludeDebugInformation = parser.Debug;
  87. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  88. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  89. cache.Insert (key, results, new CacheDependency (deps));
  90. }
  91. return results;
  92. }
  93. internal static CompilerParameters GetOptions (ICollection assemblies)
  94. {
  95. CompilerParameters options = new CompilerParameters ();
  96. if (assemblies != null) {
  97. StringCollection coll = options.ReferencedAssemblies;
  98. foreach (string str in assemblies)
  99. coll.Add (str);
  100. }
  101. return options;
  102. }
  103. public static CompilerResults Compile (string language, string key, string file,
  104. ArrayList assemblies)
  105. {
  106. Cache cache = HttpRuntime.Cache;
  107. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  108. if (results != null)
  109. return results;
  110. lock (compilationLock) {
  111. results = (CompilerResults) cache [cachePrefix + key];
  112. if (results != null)
  113. return results;
  114. CompilationConfiguration config;
  115. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  116. CodeDomProvider provider = config.GetProvider (language);
  117. if (provider == null)
  118. throw new HttpException ("Configuration error. Language not supported: " +
  119. language, 500);
  120. ICodeCompiler compiler = provider.CreateCompiler ();
  121. CompilerParameters options = GetOptions (assemblies);
  122. results = compiler.CompileAssemblyFromFile (options, file);
  123. string [] deps = (string []) assemblies.ToArray (typeof (string));
  124. cache.Insert (cachePrefix + key, results, new CacheDependency (deps));
  125. }
  126. return results;
  127. }
  128. }
  129. }