CompilationConfiguration.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #if !NET_2_0
  30. using System;
  31. using System.CodeDom.Compiler;
  32. using System.Collections;
  33. using System.Configuration;
  34. using System.IO;
  35. using System.Web;
  36. using System.Xml;
  37. namespace System.Web.Configuration
  38. {
  39. sealed class CompilationConfiguration
  40. {
  41. bool debug = false;
  42. bool batch = false;
  43. int batch_timeout;
  44. string default_language = "c#";
  45. bool _explicit = true;
  46. int max_batch_size = 30;
  47. int max_batch_file_size = 3000;
  48. int num_recompiles_before_app_restart = 15;
  49. bool strict = false;
  50. string temp_directory;
  51. CompilerCollection compilers;
  52. ArrayList assemblies;
  53. bool assembliesInBin = false;
  54. /* Only the config. handler should create instances of this. Use GetInstance (context) */
  55. public CompilationConfiguration (object p)
  56. {
  57. CompilationConfiguration parent = p as CompilationConfiguration;
  58. if (parent != null)
  59. Init (parent);
  60. if (compilers == null)
  61. compilers = new CompilerCollection ();
  62. if (assemblies == null)
  63. assemblies = new ArrayList ();
  64. if (temp_directory == null || temp_directory == "")
  65. temp_directory = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  66. }
  67. static public CompilationConfiguration GetInstance (HttpContext context)
  68. {
  69. CompilationConfiguration config;
  70. if (context == null)
  71. context = HttpContext.Current;
  72. if (context != null) {
  73. config = context.GetConfig ("system.web/compilation") as CompilationConfiguration;
  74. if (config == null)
  75. throw new Exception ("Configuration error.");
  76. } else {
  77. // empty config (as used in unit tests)
  78. config = new CompilationConfiguration (null);
  79. }
  80. return config;
  81. }
  82. public CodeDomProvider GetProvider (string language)
  83. {
  84. Compiler compiler = Compilers [language];
  85. if (compiler == null)
  86. return null;
  87. if (compiler.Provider != null)
  88. return compiler.Provider;
  89. Type t = Type.GetType (compiler.Type, true);
  90. compiler.Provider = Activator.CreateInstance (t) as CodeDomProvider;
  91. return compiler.Provider;
  92. }
  93. public string GetCompilerOptions (string language)
  94. {
  95. Compiler compiler = Compilers [language];
  96. if (compiler == null)
  97. return null;
  98. return compiler.CompilerOptions;
  99. }
  100. public int GetWarningLevel (string language)
  101. {
  102. Compiler compiler = Compilers [language];
  103. if (compiler == null)
  104. return 0;
  105. return compiler.WarningLevel;
  106. }
  107. void Init (CompilationConfiguration parent)
  108. {
  109. debug = parent.debug;
  110. batch = parent.batch;
  111. batch_timeout = parent.batch_timeout;
  112. default_language = parent.default_language;
  113. _explicit = parent._explicit;
  114. max_batch_size = parent.max_batch_size;
  115. max_batch_file_size = parent.max_batch_file_size;
  116. num_recompiles_before_app_restart = parent.num_recompiles_before_app_restart;
  117. strict = parent.strict;
  118. temp_directory = parent.temp_directory;
  119. #if NET_2_0
  120. compilers = new CompilerCollection ();
  121. #else
  122. compilers = new CompilerCollection (parent.compilers);
  123. #endif
  124. ArrayList p = parent.assemblies;
  125. assembliesInBin = parent.assembliesInBin;
  126. ICollection coll = (p == null) ? (ICollection) new object [0] : p;
  127. assemblies = new ArrayList (coll);
  128. }
  129. public bool Debug {
  130. get { return debug; }
  131. set { debug = value; }
  132. }
  133. public bool Batch {
  134. get { return batch; }
  135. set { batch = value; }
  136. }
  137. public int BatchTimeout {
  138. get { return batch_timeout; }
  139. set { batch_timeout = value; }
  140. }
  141. public string DefaultLanguage {
  142. get { return default_language; }
  143. set { default_language = value; }
  144. }
  145. public bool Explicit {
  146. get { return _explicit; }
  147. set { _explicit = value; }
  148. }
  149. public int MaxBatchSize {
  150. get { return max_batch_size; }
  151. set { max_batch_size = value; }
  152. }
  153. public int MaxBatchFileSize {
  154. get { return max_batch_file_size; }
  155. set { max_batch_file_size = value; }
  156. }
  157. public int NumRecompilesBeforeAppRestart {
  158. get { return num_recompiles_before_app_restart; }
  159. set { num_recompiles_before_app_restart = value; }
  160. }
  161. public bool Strict {
  162. get { return strict; }
  163. set { strict = value; }
  164. }
  165. public string TempDirectory {
  166. get { return temp_directory; }
  167. set {
  168. if (value != null && !Directory.Exists (value))
  169. throw new ArgumentException ("Directory does not exist");
  170. Console.WriteLine ("Dos: '{0}'\n{1}", value, Environment.StackTrace);
  171. temp_directory = value;
  172. }
  173. }
  174. public CompilerCollection Compilers {
  175. get { return compilers; }
  176. }
  177. public ArrayList Assemblies {
  178. get { return assemblies; }
  179. }
  180. public bool AssembliesInBin {
  181. get { return assembliesInBin; }
  182. set { assembliesInBin = value; }
  183. }
  184. }
  185. }
  186. #endif