GlobalAsaxCompiler.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // System.Web.Compilation.GlobalAsaxCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Web;
  14. using System.Web.Util;
  15. namespace System.Web.Compilation
  16. {
  17. class GlobalAsaxCompiler : BaseCompiler
  18. {
  19. string filename;
  20. string sourceFile;
  21. HttpContext context;
  22. private GlobalAsaxCompiler (string filename)
  23. {
  24. this.filename = filename;
  25. }
  26. public override Type GetCompiledType ()
  27. {
  28. sourceFile = GenerateSourceFile ();
  29. CachingCompiler compiler = new CachingCompiler (this);
  30. CompilationResult result = new CompilationResult (sourceFile);
  31. result.Options = options;
  32. if (compiler.Compile (result) == false)
  33. throw new CompilationException (result);
  34. Assembly assembly = Assembly.LoadFrom (result.OutputFile);
  35. Type [] types = assembly.GetTypes ();
  36. foreach (Type t in types) {
  37. if (t.IsSubclassOf (typeof (HttpApplication))) {
  38. if (result.Data != null)
  39. throw new CompilationException ("More that 1 app!!!", result);
  40. result.Data = t;
  41. }
  42. }
  43. return result.Data as Type;
  44. }
  45. public override string Key {
  46. get {
  47. return filename;
  48. }
  49. }
  50. public override string SourceFile {
  51. get {
  52. return sourceFile;
  53. }
  54. }
  55. public static Type CompileApplicationType (string filename, HttpContext context)
  56. {
  57. CompilationCacheItem item = CachingCompiler.GetCached (filename);
  58. if (item != null && item.Result != null) {
  59. if (item.Result != null)
  60. return item.Result.Data as Type;
  61. throw new CompilationException (item.Result);
  62. }
  63. GlobalAsaxCompiler gac = new GlobalAsaxCompiler (filename);
  64. gac.context = context;
  65. return gac.GetCompiledType ();
  66. }
  67. string GenerateSourceFile ()
  68. {
  69. AspGenerator generator = new AspGenerator (filename);
  70. generator.Context = context;
  71. generator.BaseType = typeof (HttpApplication).ToString ();
  72. generator.ProcessElements ();
  73. string generated = generator.GetCode ().ReadToEnd ();
  74. options = generator.Options;
  75. //FIXME: should get Tmp dir for this application
  76. string csName = Path.GetTempFileName () + ".cs";
  77. WebTrace.WriteLine ("Writing {0}", csName);
  78. StreamWriter output = new StreamWriter (File.OpenWrite (csName));
  79. output.Write (generated);
  80. output.Close ();
  81. return csName;
  82. }
  83. }
  84. }