CachingCompiler.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Threading;
  37. using System.Web.UI;
  38. using System.Web.Caching;
  39. using System.Web.Configuration;
  40. namespace System.Web.Compilation
  41. {
  42. class CachingCompiler
  43. {
  44. static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  45. static Hashtable compilationTickets = new Hashtable ();
  46. const string cachePrefix = "@@Assembly";
  47. const string cacheTypePrefix = "@@@Type";
  48. public static void InsertTypeFileDep (Type type, string filename)
  49. {
  50. CacheDependency dep = new CacheDependency (filename);
  51. HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
  52. }
  53. public static void InsertType (Type type, string filename)
  54. {
  55. string [] cacheKeys = new string [] { cachePrefix + filename };
  56. CacheDependency dep = new CacheDependency (null, cacheKeys);
  57. HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
  58. }
  59. public static Type GetTypeFromCache (string filename)
  60. {
  61. return (Type) HttpRuntime.Cache [cacheTypePrefix + filename];
  62. }
  63. public static CompilerResults Compile (BaseCompiler compiler)
  64. {
  65. Cache cache = HttpRuntime.Cache;
  66. string key = cachePrefix + compiler.Parser.InputFile;
  67. CompilerResults results = (CompilerResults) cache [key];
  68. #if NET_2_0
  69. if (!compiler.IsRebuildingPartial)
  70. #endif
  71. if (results != null)
  72. return results;
  73. object ticket;
  74. bool acquired = AcquireCompilationTicket (key, out ticket);
  75. try {
  76. Monitor.Enter (ticket);
  77. results = (CompilerResults) cache [key];
  78. #if NET_2_0
  79. if (!compiler.IsRebuildingPartial)
  80. #endif
  81. if (results != null)
  82. return results;
  83. ICodeCompiler comp = compiler.Compiler;
  84. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  85. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  86. cache.InsertPrivate (key, results, new CacheDependency (deps));
  87. } finally {
  88. Monitor.Exit (ticket);
  89. if (acquired)
  90. ReleaseCompilationTicket (key);
  91. }
  92. return results;
  93. }
  94. public static CompilerResults Compile (WebServiceCompiler compiler)
  95. {
  96. string key = cachePrefix + compiler.Parser.PhysicalPath;
  97. Cache cache = HttpRuntime.Cache;
  98. CompilerResults results = (CompilerResults) cache [key];
  99. if (results != null)
  100. return results;
  101. object ticket;
  102. bool acquired = AcquireCompilationTicket (key, out ticket);
  103. try {
  104. Monitor.Enter (ticket);
  105. results = (CompilerResults) cache [key];
  106. if (results != null)
  107. return results;
  108. SimpleWebHandlerParser parser = compiler.Parser;
  109. CompilerParameters options = compiler.CompilerOptions;
  110. options.IncludeDebugInformation = parser.Debug;
  111. #if NET_2_0
  112. GetExtraAssemblies (options);
  113. #endif
  114. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  115. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  116. cache.InsertPrivate (key, results, new CacheDependency (deps));
  117. } finally {
  118. Monitor.Exit (ticket);
  119. if (acquired)
  120. ReleaseCompilationTicket (key);
  121. }
  122. return results;
  123. }
  124. internal static CompilerParameters GetOptions (ICollection assemblies)
  125. {
  126. CompilerParameters options = new CompilerParameters ();
  127. if (assemblies != null) {
  128. StringCollection coll = options.ReferencedAssemblies;
  129. foreach (string str in assemblies)
  130. coll.Add (str);
  131. }
  132. #if NET_2_0
  133. GetExtraAssemblies (options);
  134. #endif
  135. return options;
  136. }
  137. public static CompilerResults Compile (string language, string key, string file,
  138. ArrayList assemblies)
  139. {
  140. Cache cache = HttpRuntime.Cache;
  141. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  142. if (results != null)
  143. return results;
  144. if (!Directory.Exists (dynamicBase))
  145. Directory.CreateDirectory (dynamicBase);
  146. object ticket;
  147. bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
  148. try {
  149. Monitor.Enter (ticket);
  150. results = (CompilerResults) cache [cachePrefix + key];
  151. if (results != null)
  152. return results;
  153. #if NET_2_0
  154. CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  155. Compiler c = config.Compilers[language];
  156. Type t = Type.GetType (c.Type, true);
  157. CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
  158. #else
  159. CompilationConfiguration config;
  160. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  161. CodeDomProvider provider = config.GetProvider (language);
  162. #endif
  163. if (provider == null)
  164. throw new HttpException ("Configuration error. Language not supported: " +
  165. language, 500);
  166. ICodeCompiler compiler = provider.CreateCompiler ();
  167. CompilerParameters options = GetOptions (assemblies);
  168. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
  169. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  170. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  171. results = compiler.CompileAssemblyFromFile (options, file);
  172. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  173. realdeps.Add (file);
  174. for (int i = assemblies.Count - 1; i >= 0; i--) {
  175. string current = (string) assemblies [i];
  176. if (Path.IsPathRooted (current))
  177. realdeps.Add (current);
  178. }
  179. string [] deps = (string []) realdeps.ToArray (typeof (string));
  180. cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
  181. } finally {
  182. Monitor.Exit (ticket);
  183. if (acquired)
  184. ReleaseCompilationTicket (cachePrefix + key);
  185. }
  186. return results;
  187. }
  188. public static Type CompileAndGetType (string typename, string language, string key,
  189. string file, ArrayList assemblies)
  190. {
  191. CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
  192. if (result.NativeCompilerReturnValue != 0) {
  193. StreamReader reader = new StreamReader (file);
  194. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  195. }
  196. Assembly assembly = result.CompiledAssembly;
  197. if (assembly == null) {
  198. StreamReader reader = new StreamReader (file);
  199. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  200. }
  201. Type type = assembly.GetType (typename, true);
  202. InsertType (type, file);
  203. return type;
  204. }
  205. #if NET_2_0
  206. static void GetExtraAssemblies (CompilerParameters options)
  207. {
  208. ArrayList al = WebConfigurationManager.ExtraAssemblies;
  209. if (al != null && al.Count > 0) {
  210. foreach (object o in al)
  211. if (o is string)
  212. options.ReferencedAssemblies.Add ((string) o);
  213. }
  214. IList list = BuildManager.CodeAssemblies;
  215. if (list != null && list.Count > 0) {
  216. foreach (object o in list)
  217. if (o is string)
  218. options.ReferencedAssemblies.Add ((string) o);
  219. }
  220. }
  221. #endif
  222. static bool AcquireCompilationTicket (string key, out object ticket)
  223. {
  224. lock (compilationTickets.SyncRoot) {
  225. ticket = compilationTickets [key];
  226. if (ticket == null) {
  227. ticket = new object ();
  228. compilationTickets [key] = ticket;
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. static void ReleaseCompilationTicket (string key)
  235. {
  236. lock (compilationTickets.SyncRoot) {
  237. compilationTickets.Remove (key);
  238. }
  239. }
  240. }
  241. }