2
0

CachingCompiler.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #if NET_2_0
  85. GetExtraAssemblies (compiler.CompilerParameters);
  86. #endif
  87. results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
  88. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  89. cache.InsertPrivate (key, results, new CacheDependency (deps));
  90. } finally {
  91. Monitor.Exit (ticket);
  92. if (acquired)
  93. ReleaseCompilationTicket (key);
  94. }
  95. return results;
  96. }
  97. public static CompilerResults Compile (WebServiceCompiler compiler)
  98. {
  99. string key = cachePrefix + compiler.Parser.PhysicalPath;
  100. Cache cache = HttpRuntime.Cache;
  101. CompilerResults results = (CompilerResults) cache [key];
  102. if (results != null)
  103. return results;
  104. object ticket;
  105. bool acquired = AcquireCompilationTicket (key, out ticket);
  106. try {
  107. Monitor.Enter (ticket);
  108. results = (CompilerResults) cache [key];
  109. if (results != null)
  110. return results;
  111. SimpleWebHandlerParser parser = compiler.Parser;
  112. CompilerParameters options = compiler.CompilerOptions;
  113. options.IncludeDebugInformation = parser.Debug;
  114. #if NET_2_0
  115. GetExtraAssemblies (options);
  116. #endif
  117. results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
  118. string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
  119. cache.InsertPrivate (key, results, new CacheDependency (deps));
  120. } finally {
  121. Monitor.Exit (ticket);
  122. if (acquired)
  123. ReleaseCompilationTicket (key);
  124. }
  125. return results;
  126. }
  127. internal static CompilerParameters GetOptions (ICollection assemblies)
  128. {
  129. CompilerParameters options = new CompilerParameters ();
  130. if (assemblies != null) {
  131. StringCollection coll = options.ReferencedAssemblies;
  132. foreach (string str in assemblies)
  133. coll.Add (str);
  134. }
  135. #if NET_2_0
  136. GetExtraAssemblies (options);
  137. #endif
  138. return options;
  139. }
  140. public static CompilerResults Compile (string language, string key, string file,
  141. ArrayList assemblies)
  142. {
  143. Cache cache = HttpRuntime.Cache;
  144. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  145. if (results != null)
  146. return results;
  147. if (!Directory.Exists (dynamicBase))
  148. Directory.CreateDirectory (dynamicBase);
  149. object ticket;
  150. bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
  151. try {
  152. Monitor.Enter (ticket);
  153. results = (CompilerResults) cache [cachePrefix + key];
  154. if (results != null)
  155. return results;
  156. #if NET_2_0
  157. CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
  158. Compiler c = config.Compilers[language];
  159. Type t = Type.GetType (c.Type, true);
  160. CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
  161. #else
  162. CompilationConfiguration config;
  163. config = CompilationConfiguration.GetInstance (HttpContext.Current);
  164. CodeDomProvider provider = config.GetProvider (language);
  165. #endif
  166. if (provider == null)
  167. throw new HttpException ("Configuration error. Language not supported: " +
  168. language, 500);
  169. ICodeCompiler compiler = provider.CreateCompiler ();
  170. CompilerParameters options = GetOptions (assemblies);
  171. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
  172. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  173. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  174. results = compiler.CompileAssemblyFromFile (options, file);
  175. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  176. realdeps.Add (file);
  177. for (int i = assemblies.Count - 1; i >= 0; i--) {
  178. string current = (string) assemblies [i];
  179. if (Path.IsPathRooted (current))
  180. realdeps.Add (current);
  181. }
  182. string [] deps = (string []) realdeps.ToArray (typeof (string));
  183. cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
  184. } finally {
  185. Monitor.Exit (ticket);
  186. if (acquired)
  187. ReleaseCompilationTicket (cachePrefix + key);
  188. }
  189. return results;
  190. }
  191. public static Type CompileAndGetType (string typename, string language, string key,
  192. string file, ArrayList assemblies)
  193. {
  194. CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
  195. if (result.NativeCompilerReturnValue != 0) {
  196. StreamReader reader = new StreamReader (file);
  197. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  198. }
  199. Assembly assembly = result.CompiledAssembly;
  200. if (assembly == null) {
  201. StreamReader reader = new StreamReader (file);
  202. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  203. }
  204. Type type = assembly.GetType (typename, true);
  205. InsertType (type, file);
  206. return type;
  207. }
  208. #if NET_2_0
  209. static void GetExtraAssemblies (CompilerParameters options)
  210. {
  211. ArrayList al = WebConfigurationManager.ExtraAssemblies;
  212. if (al != null && al.Count > 0) {
  213. foreach (object o in al)
  214. if (o is string)
  215. options.ReferencedAssemblies.Add ((string) o);
  216. }
  217. IList list = BuildManager.CodeAssemblies;
  218. if (list != null && list.Count > 0) {
  219. foreach (object o in list)
  220. if (o is string)
  221. options.ReferencedAssemblies.Add ((string) o);
  222. }
  223. list = BuildManager.TopLevelAssemblies;
  224. if (list != null && list.Count > 0) {
  225. foreach (Assembly a in list)
  226. options.ReferencedAssemblies.Add (a.Location);
  227. }
  228. }
  229. #endif
  230. static bool AcquireCompilationTicket (string key, out object ticket)
  231. {
  232. lock (compilationTickets.SyncRoot) {
  233. ticket = compilationTickets [key];
  234. if (ticket == null) {
  235. ticket = new object ();
  236. compilationTickets [key] = ticket;
  237. return true;
  238. }
  239. }
  240. return false;
  241. }
  242. static void ReleaseCompilationTicket (string key)
  243. {
  244. lock (compilationTickets.SyncRoot) {
  245. compilationTickets.Remove (key);
  246. }
  247. }
  248. }
  249. }