PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Web/System.Web.Compilation/CachingCompiler.cs

https://bitbucket.org/danipen/mono
C# | 342 lines | 263 code | 50 blank | 29 comment | 67 complexity | 7dfca81505c88a989842cf5c0b6d2007 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // System.Web.Compilation.CachingCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier (gonzalo@ximian.com)
  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.Generic;
  34. using System.Collections.Specialized;
  35. using System.IO;
  36. using System.Reflection;
  37. using System.Threading;
  38. using System.Web.UI;
  39. using System.Web.Caching;
  40. using System.Web.Configuration;
  41. namespace System.Web.Compilation
  42. {
  43. class CachingCompiler
  44. {
  45. static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  46. static Hashtable compilationTickets = new Hashtable ();
  47. const string cachePrefix = "@@Assembly";
  48. const string cacheTypePrefix = "@@@Type";
  49. static Hashtable assemblyCache = new Hashtable ();
  50. public static void InsertTypeFileDep (Type type, string filename)
  51. {
  52. CacheDependency dep = new CacheDependency (filename);
  53. HttpRuntime.InternalCache.Insert (cacheTypePrefix + filename, type, dep);
  54. }
  55. public static void InsertType (Type type, string filename)
  56. {
  57. string [] cacheKeys = new string [] { cachePrefix + filename };
  58. CacheDependency dep = new CacheDependency (null, cacheKeys);
  59. HttpRuntime.InternalCache.Insert (cacheTypePrefix + filename, type, dep);
  60. }
  61. public static Type GetTypeFromCache (string filename)
  62. {
  63. return (Type) HttpRuntime.InternalCache [cacheTypePrefix + filename];
  64. }
  65. public static CompilerResults Compile (BaseCompiler compiler)
  66. {
  67. Cache cache = HttpRuntime.InternalCache;
  68. string key = cachePrefix + compiler.Parser.InputFile;
  69. CompilerResults results = (CompilerResults) cache [key];
  70. if (!compiler.IsRebuildingPartial)
  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 (!compiler.IsRebuildingPartial)
  79. if (results != null)
  80. return results;
  81. CodeDomProvider comp = compiler.Provider;
  82. CompilerParameters options = compiler.CompilerParameters;
  83. GetExtraAssemblies (options);
  84. results = comp.CompileAssemblyFromDom (options, compiler.CompileUnit);
  85. List <string> dependencies = compiler.Parser.Dependencies;
  86. if (dependencies != null && dependencies.Count > 0) {
  87. string [] deps = dependencies.ToArray ();
  88. HttpContext ctx = HttpContext.Current;
  89. HttpRequest req = ctx != null ? ctx.Request : null;
  90. if (req == null)
  91. throw new HttpException ("No current context, cannot compile.");
  92. for (int i = 0; i < deps.Length; i++)
  93. deps [i] = req.MapPath (deps [i]);
  94. cache.Insert (key, results, new CacheDependency (deps));
  95. }
  96. } finally {
  97. Monitor.Exit (ticket);
  98. if (acquired)
  99. ReleaseCompilationTicket (key);
  100. }
  101. return results;
  102. }
  103. public static CompilerResults Compile (WebServiceCompiler compiler)
  104. {
  105. string key = cachePrefix + compiler.Parser.PhysicalPath;
  106. Cache cache = HttpRuntime.InternalCache;
  107. CompilerResults results = (CompilerResults) cache [key];
  108. if (results != null)
  109. return results;
  110. object ticket;
  111. bool acquired = AcquireCompilationTicket (key, out ticket);
  112. try {
  113. Monitor.Enter (ticket);
  114. results = (CompilerResults) cache [key];
  115. if (results != null)
  116. return results;
  117. CodeDomProvider comp = compiler.Provider;
  118. CompilerParameters options = compiler.CompilerParameters;
  119. GetExtraAssemblies (options);
  120. results = comp.CompileAssemblyFromFile (options, compiler.InputFile);
  121. string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
  122. cache.Insert (key, results, new CacheDependency (deps));
  123. } finally {
  124. Monitor.Exit (ticket);
  125. if (acquired)
  126. ReleaseCompilationTicket (key);
  127. }
  128. return results;
  129. }
  130. internal static CompilerParameters GetOptions (ICollection assemblies)
  131. {
  132. CompilerParameters options = new CompilerParameters ();
  133. if (assemblies != null) {
  134. StringCollection coll = options.ReferencedAssemblies;
  135. foreach (string str in assemblies)
  136. coll.Add (str);
  137. }
  138. GetExtraAssemblies (options);
  139. return options;
  140. }
  141. public static CompilerResults Compile (string language, string key, string file, ArrayList assemblies)
  142. {
  143. return Compile (language, key, file, assemblies, false);
  144. }
  145. public static CompilerResults Compile (string language, string key, string file, ArrayList assemblies, bool debug)
  146. {
  147. Cache cache = HttpRuntime.InternalCache;
  148. CompilerResults results = (CompilerResults) cache [cachePrefix + key];
  149. if (results != null)
  150. return results;
  151. if (!Directory.Exists (dynamicBase))
  152. Directory.CreateDirectory (dynamicBase);
  153. object ticket;
  154. bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
  155. try {
  156. Monitor.Enter (ticket);
  157. results = (CompilerResults) cache [cachePrefix + key];
  158. if (results != null)
  159. return results;
  160. CodeDomProvider provider = null;
  161. int warningLevel;
  162. string compilerOptions;
  163. string tempdir;
  164. provider = BaseCompiler.CreateProvider (language, out compilerOptions, out warningLevel, out tempdir);
  165. if (provider == null)
  166. throw new HttpException ("Configuration error. Language not supported: " +
  167. language, 500);
  168. CodeDomProvider compiler = provider;
  169. CompilerParameters options = GetOptions (assemblies);
  170. options.IncludeDebugInformation = debug;
  171. options.WarningLevel = warningLevel;
  172. options.CompilerOptions = compilerOptions;
  173. TempFileCollection tempcoll = new TempFileCollection (tempdir, true);
  174. string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
  175. options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  176. results = compiler.CompileAssemblyFromFile (options, file);
  177. ArrayList realdeps = new ArrayList (assemblies.Count + 1);
  178. realdeps.Add (file);
  179. for (int i = assemblies.Count - 1; i >= 0; i--) {
  180. string current = (string) assemblies [i];
  181. if (Path.IsPathRooted (current))
  182. realdeps.Add (current);
  183. }
  184. string [] deps = (string []) realdeps.ToArray (typeof (string));
  185. cache.Insert (cachePrefix + key, results, new CacheDependency (deps));
  186. } finally {
  187. Monitor.Exit (ticket);
  188. if (acquired)
  189. ReleaseCompilationTicket (cachePrefix + key);
  190. }
  191. return results;
  192. }
  193. public static Type CompileAndGetType (string typename, string language, string key,
  194. string file, ArrayList assemblies)
  195. {
  196. CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
  197. if (result.NativeCompilerReturnValue != 0) {
  198. using (StreamReader reader = new StreamReader (file)) {
  199. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  200. }
  201. }
  202. Assembly assembly = result.CompiledAssembly;
  203. if (assembly == null) {
  204. using (StreamReader reader = new StreamReader (file)) {
  205. throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
  206. }
  207. }
  208. Type type = assembly.GetType (typename, true);
  209. InsertType (type, file);
  210. return type;
  211. }
  212. static void GetExtraAssemblies (CompilerParameters options)
  213. {
  214. StringCollection refAsm = options.ReferencedAssemblies;
  215. string asmLocation;
  216. string asmName;
  217. ArrayList al = WebConfigurationManager.ExtraAssemblies;
  218. if (al != null && al.Count > 0) {
  219. foreach (object o in al) {
  220. asmName = o as string;
  221. if (asmName != null && !refAsm.Contains (asmName))
  222. refAsm.Add (asmName);
  223. }
  224. }
  225. Assembly asm;
  226. IList list = BuildManager.CodeAssemblies;
  227. if (list != null && list.Count > 0) {
  228. foreach (object o in list) {
  229. asm = o as Assembly;
  230. if (asm == null)
  231. continue;
  232. asmName = asm.Location;
  233. if (asmName != null && !refAsm.Contains (asmName))
  234. refAsm.Add (asmName);
  235. }
  236. }
  237. list = BuildManager.TopLevelAssemblies;
  238. if (list != null && list.Count > 0) {
  239. foreach (object o in list) {
  240. asm = o as Assembly;
  241. if (o == null)
  242. continue;
  243. asmName = asm.Location;
  244. if (!refAsm.Contains (asmName))
  245. refAsm.Add (asmName);
  246. }
  247. }
  248. CompilationSection cfg = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
  249. AssemblyCollection asmcoll = cfg != null ? cfg.Assemblies : null;
  250. if (asmcoll == null)
  251. return;
  252. foreach (AssemblyInfo ai in asmcoll) {
  253. asmLocation = GetAssemblyLocationFromName (ai.Assembly);
  254. if (asmLocation == null || refAsm.Contains (asmLocation))
  255. continue;
  256. refAsm.Add (asmLocation);
  257. }
  258. }
  259. static string GetAssemblyLocationFromName (string name)
  260. {
  261. Assembly asm = assemblyCache [name] as Assembly;
  262. if (asm != null)
  263. return asm.Location;
  264. try {
  265. asm = Assembly.Load (name);
  266. } catch {
  267. }
  268. if (asm == null)
  269. return null;
  270. assemblyCache [name] = asm;
  271. return asm.Location;
  272. }
  273. static bool AcquireCompilationTicket (string key, out object ticket)
  274. {
  275. lock (compilationTickets.SyncRoot) {
  276. ticket = compilationTickets [key];
  277. if (ticket == null) {
  278. ticket = new object ();
  279. compilationTickets [key] = ticket;
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. static void ReleaseCompilationTicket (string key)
  286. {
  287. lock (compilationTickets.SyncRoot) {
  288. compilationTickets.Remove (key);
  289. }
  290. }
  291. }
  292. }