CompilationConfiguration.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // System.Web.Configuration.CompilationConfiguration
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) Copyright 2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.CodeDom.Compiler;
  31. using System.Collections;
  32. using System.Configuration;
  33. using System.IO;
  34. using System.Web;
  35. using System.Xml;
  36. namespace System.Web.Configuration
  37. {
  38. sealed class CompilationConfiguration
  39. {
  40. bool debug = false;
  41. bool batch = false;
  42. int batch_timeout;
  43. string default_language = "c#";
  44. bool _explicit = true;
  45. int max_batch_size = 30;
  46. int max_batch_file_size = 3000;
  47. int num_recompiles_before_app_restart = 15;
  48. bool strict = false;
  49. string temp_directory;
  50. CompilerCollection compilers;
  51. ArrayList assemblies;
  52. bool assembliesInBin = false;
  53. /* Only the config. handler should create instances of this. Use GetInstance (context) */
  54. public CompilationConfiguration (object p)
  55. {
  56. CompilationConfiguration parent = p as CompilationConfiguration;
  57. if (parent != null)
  58. Init (parent);
  59. if (compilers == null)
  60. compilers = new CompilerCollection ();
  61. if (assemblies == null)
  62. assemblies = new ArrayList ();
  63. if (temp_directory == null || temp_directory == "")
  64. temp_directory = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  65. }
  66. static public CompilationConfiguration GetInstance (HttpContext context)
  67. {
  68. CompilationConfiguration config;
  69. if (context == null)
  70. context = HttpContext.Current;
  71. if (context != null) {
  72. config = context.GetConfig ("system.web/compilation") as CompilationConfiguration;
  73. if (config == null)
  74. throw new Exception ("Configuration error.");
  75. } else {
  76. // empty config (as used in unit tests)
  77. config = new CompilationConfiguration (null);
  78. }
  79. return config;
  80. }
  81. public CodeDomProvider GetProvider (string language)
  82. {
  83. Compiler compiler = Compilers [language];
  84. if (compiler == null)
  85. return null;
  86. if (compiler.Provider != null)
  87. return compiler.Provider;
  88. Type t = Type.GetType (compiler.Type, true);
  89. compiler.Provider = Activator.CreateInstance (t) as CodeDomProvider;
  90. return compiler.Provider;
  91. }
  92. public string GetCompilerOptions (string language)
  93. {
  94. Compiler compiler = Compilers [language];
  95. if (compiler == null)
  96. return null;
  97. return compiler.CompilerOptions;
  98. }
  99. public int GetWarningLevel (string language)
  100. {
  101. Compiler compiler = Compilers [language];
  102. if (compiler == null)
  103. return 0;
  104. return compiler.WarningLevel;
  105. }
  106. void Init (CompilationConfiguration parent)
  107. {
  108. debug = parent.debug;
  109. batch = parent.batch;
  110. batch_timeout = parent.batch_timeout;
  111. default_language = parent.default_language;
  112. _explicit = parent._explicit;
  113. max_batch_size = parent.max_batch_size;
  114. max_batch_file_size = parent.max_batch_file_size;
  115. num_recompiles_before_app_restart = parent.num_recompiles_before_app_restart;
  116. strict = parent.strict;
  117. temp_directory = parent.temp_directory;
  118. compilers = new CompilerCollection (parent.compilers);
  119. ArrayList p = parent.assemblies;
  120. assembliesInBin = parent.assembliesInBin;
  121. ICollection coll = (p == null) ? (ICollection) new object [0] : p;
  122. assemblies = new ArrayList (coll);
  123. }
  124. public bool Debug {
  125. get { return debug; }
  126. set { debug = value; }
  127. }
  128. public bool Batch {
  129. get { return batch; }
  130. set { batch = value; }
  131. }
  132. public int BatchTimeout {
  133. get { return batch_timeout; }
  134. set { batch_timeout = value; }
  135. }
  136. public string DefaultLanguage {
  137. get { return default_language; }
  138. set { default_language = value; }
  139. }
  140. public bool Explicit {
  141. get { return _explicit; }
  142. set { _explicit = value; }
  143. }
  144. public int MaxBatchSize {
  145. get { return max_batch_size; }
  146. set { max_batch_size = value; }
  147. }
  148. public int MaxBatchFileSize {
  149. get { return max_batch_file_size; }
  150. set { max_batch_file_size = value; }
  151. }
  152. public int NumRecompilesBeforeAppRestart {
  153. get { return num_recompiles_before_app_restart; }
  154. set { num_recompiles_before_app_restart = value; }
  155. }
  156. public bool Strict {
  157. get { return strict; }
  158. set { strict = value; }
  159. }
  160. public string TempDirectory {
  161. get { return temp_directory; }
  162. set {
  163. if (value != null && !Directory.Exists (value))
  164. throw new ArgumentException ("Directory does not exist");
  165. Console.WriteLine ("Dos: '{0}'\n{1}", value, Environment.StackTrace);
  166. temp_directory = value;
  167. }
  168. }
  169. public CompilerCollection Compilers {
  170. get { return compilers; }
  171. }
  172. public ArrayList Assemblies {
  173. get { return assemblies; }
  174. }
  175. public bool AssembliesInBin {
  176. get { return assembliesInBin; }
  177. set { assembliesInBin = value; }
  178. }
  179. }
  180. }