CachingCompiler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. using System;
  11. using System.CodeDom.Compiler;
  12. using System.Collections;
  13. using System.Collections.Specialized;
  14. using System.Web.UI;
  15. using System.Web.Caching;
  16. using System.Web.Configuration;
  17. namespace System.Web.Compilation
  18. {
  19. class CachingCompiler
  20. {
  21. static object compilationLock = new object ();
  22. const string cachePrefix = "@@Assembly";
  23. public static CompilerResults Compile (BaseCompiler compiler)
  24. {
  25. Cache cache = HttpRuntime.Cache;
  26. string key = cachePrefix + compiler.Parser.InputFile;
  27. CompilerResults results = (CompilerResults) cache [key];
  28. if (results != null)
  29. return results;
  30. lock (compilationLock) {
  31. results = (CompilerResults) cache [key];
  32. if (results != null)
  33. return results;
  34. ICodeCompiler comp = compiler.Compiler;
  35. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  36. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  37. cache.Insert (key, results, new CacheDependency (deps));
  38. }
  39. return results;
  40. }
  41. public static CompilerResults Compile (WebServiceCompiler compiler)
  42. {
  43. string key = cachePrefix + compiler.Parser.PhysicalPath;
  44. Cache cache = HttpRuntime.Cache;
  45. CompilerResults results = (CompilerResults) cache [key];
  46. if (results != null)
  47. return results;
  48. lock (compilationLock) {
  49. results = (CompilerResults) cache [key];
  50. if (results != null)
  51. return results;
  52. SimpleWebHandlerParser parser = compiler.Parser;
  53. CompilerParameters options = compiler.CompilerOptions;
  54. options.IncludeDebugInformation = parser.Debug;
  55. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  56. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  57. cache.Insert (key, results, new CacheDependency (deps));
  58. }
  59. return results;
  60. }
  61. internal static CompilerParameters GetOptions (ICollection assemblies)
  62. {
  63. CompilerParameters options = new CompilerParameters ();
  64. if (assemblies != null) {
  65. StringCollection coll = options.ReferencedAssemblies;
  66. foreach (string str in assemblies)
  67. coll.Add (str);
  68. }
  69. return options;
  70. }
  71. public static CompilerResults Compile (string language, string key, string file,
  72. ArrayList assemblies)
  73. {
  74. Cache cache = HttpRuntime.Cache;
  75. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  76. if (results != null)
  77. return results;
  78. lock (compilationLock) {
  79. results = (CompilerResults) cache [cachePrefix + key];
  80. if (results != null)
  81. return results;
  82. CompilationConfiguration config;
  83. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  84. CodeDomProvider provider = config.GetProvider (language);
  85. if (provider == null)
  86. throw new HttpException ("Configuration error. Language not supported: " +
  87. language, 500);
  88. ICodeCompiler compiler = provider.CreateCompiler ();
  89. CompilerParameters options = GetOptions (assemblies);
  90. results = compiler.CompileAssemblyFromFile (options, file);
  91. string [] deps = (string []) assemblies.ToArray (typeof (string));
  92. cache.Insert (cachePrefix + key, results, new CacheDependency (deps));
  93. }
  94. return results;
  95. }
  96. }
  97. }