CachingCompiler.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // CachingCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Ankit Jain ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  9. // (c) Copyright Novell, Inc. (http://www.novell.com)
  10. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. // Code taken from System.Web.Compilation.CachingCompiler
  33. //
  34. using System;
  35. using System.CodeDom.Compiler;
  36. using System.Collections;
  37. using System.Collections.Specialized;
  38. using System.IO;
  39. using System.Reflection;
  40. using System.Threading;
  41. using System.Web;
  42. using System.Web.UI;
  43. using System.Web.Caching;
  44. using System.Web.Configuration;
  45. namespace System.ServiceModel.Channels
  46. {
  47. class CachingCompiler
  48. {
  49. static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  50. static Hashtable compilationTickets = new Hashtable ();
  51. public const string cachePrefix = "@@Assembly";
  52. public const string cacheTypePrefix = "@@@Type";
  53. public static void InsertTypeFileDep (Type type, string filename)
  54. {
  55. CacheDependency dep = new CacheDependency (filename);
  56. HttpRuntime.Cache.Insert (cacheTypePrefix + filename, type, dep);
  57. }
  58. public static void InsertType (Type type, string filename, string key,
  59. CacheItemRemovedCallback removed_callback)
  60. {
  61. //string [] cacheKeys = new string [] { cachePrefix + filename };
  62. //CacheDependency dep = new CacheDependency (null, cacheKeys);
  63. CacheDependency dep = new CacheDependency (filename);
  64. HttpRuntime.Cache.Insert (cacheTypePrefix + key, type, dep,
  65. Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
  66. CacheItemPriority.Normal, removed_callback);
  67. }
  68. public static Type GetTypeFromCache (string filename)
  69. {
  70. return (Type) HttpRuntime.Cache [cacheTypePrefix + filename];
  71. }
  72. internal static CompilerParameters GetOptions (ICollection assemblies)
  73. {
  74. CompilerParameters options = new CompilerParameters ();
  75. if (assemblies != null) {
  76. StringCollection coll = options.ReferencedAssemblies;
  77. foreach (string str in assemblies)
  78. coll.Add (str);
  79. }
  80. return options;
  81. }
  82. public static CompilerResults Compile (string language, string key, string source,
  83. string filename, ArrayList assemblies)
  84. {
  85. Cache cache = HttpRuntime.Cache;
  86. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  87. if (results != null)
  88. return results;
  89. if (!Directory.Exists (dynamicBase))
  90. Directory.CreateDirectory (dynamicBase);
  91. object ticket;
  92. bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
  93. try {
  94. Monitor.Enter (ticket);
  95. results = (CompilerResults) cache [cachePrefix + key];
  96. if (results != null)
  97. return results;
  98. CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  99. Compiler c = config.Compilers[language];
  100. Type t = Type.GetType (c.Type, true);
  101. CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
  102. if (provider == null)
  103. throw new HttpException ("Configuration error. Language not supported: " +
  104. language, 500);
  105. CompilerParameters options = GetOptions (assemblies);
  106. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
  107. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  108. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  109. results = provider.CompileAssemblyFromSource (options, source);
  110. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  111. realdeps.Add (filename);
  112. for (int i = assemblies.Count - 1; i >= 0; i--) {
  113. string current = (string) assemblies [i];
  114. if (Path.IsPathRooted (current))
  115. realdeps.Add (current);
  116. }
  117. string [] deps = (string []) realdeps.ToArray (typeof (string));
  118. //cache results
  119. cache.Insert (cachePrefix + key, results, new CacheDependency (deps));
  120. } finally {
  121. Monitor.Exit (ticket);
  122. if (acquired)
  123. ReleaseCompilationTicket (cachePrefix + key);
  124. }
  125. return results;
  126. }
  127. public static Type CompileAndGetType (ServiceHostParser parser,
  128. string key, CacheItemRemovedCallback removed_callback)
  129. {
  130. CompilerResults result = CachingCompiler.Compile (parser.Language, key, parser.Program, parser.Filename, parser.Assemblies);
  131. if (result.NativeCompilerReturnValue != 0)
  132. throw new CompilationException (parser.Filename, result.Errors, parser.Program);
  133. Assembly assembly = result.CompiledAssembly;
  134. if (assembly == null)
  135. throw new CompilationException (parser.Filename, result.Errors, parser.Program);
  136. Type type = assembly.GetType (parser.TypeName, true);
  137. //cache type
  138. InsertType (type, parser.Filename, key, removed_callback);
  139. return type;
  140. }
  141. static bool AcquireCompilationTicket (string key, out object ticket)
  142. {
  143. lock (compilationTickets.SyncRoot) {
  144. ticket = compilationTickets [key];
  145. if (ticket == null) {
  146. ticket = new object ();
  147. compilationTickets [key] = ticket;
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. static void ReleaseCompilationTicket (string key)
  154. {
  155. lock (compilationTickets.SyncRoot) {
  156. compilationTickets.Remove (key);
  157. }
  158. }
  159. }
  160. }