* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Web / System.Web.Compilation / WebServiceCompiler.cs
1 //
2 // System.Web.Compilation.WebServiceCompiler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (c) 2040 Novell, Inc. (http://www.novell.com)
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.CodeDom.Compiler;
33 using System.IO;
34 using System.Web.Configuration;
35 using System.Web.UI;
36 using System.Reflection;
37
38 namespace System.Web.Compilation
39 {
40         class WebServiceCompiler : BaseCompiler
41         {
42                 SimpleWebHandlerParser parser;
43                 ICodeCompiler compiler;
44                 CodeDomProvider provider;
45                 CompilerParameters compilerParameters;
46                 string inputFile;
47
48                 public WebServiceCompiler (SimpleWebHandlerParser wService)
49                         : base (null)
50                 {
51                         this.parser = wService;
52                 }
53
54                 public static Type CompileIntoType (SimpleWebHandlerParser wService)
55                 {
56                         WebServiceCompiler wsc = new WebServiceCompiler (wService);
57                         return wsc.GetCompiledType ();
58                 }
59
60                 public override Type GetCompiledType ()
61                 {
62                         Type type = CachingCompiler.GetTypeFromCache (parser.PhysicalPath);
63                         if (type != null)
64                                 return type;
65
66                         if (parser.Program.Trim () == "") {
67                                 type = parser.GetTypeFromBin (parser.ClassName);
68                                 CachingCompiler.InsertTypeFileDep (type, parser.PhysicalPath);
69                                 return type;
70                         }
71
72                         string lang = parser.Language;
73 #if NET_2_0
74                         CompilationSection config = (CompilationSection)WebConfigurationManager.GetSection ("system.web/compilation");
75                         Compiler c = config.Compilers[lang];
76                         Type t = Type.GetType (c.Type, true);
77                         provider = Activator.CreateInstance (t) as CodeDomProvider;
78 #else
79                         CompilationConfiguration config;
80                         config = CompilationConfiguration.GetInstance (parser.Context);
81                         provider = config.GetProvider (lang);
82 #endif
83                         if (provider == null)
84                                 throw new HttpException ("Configuration error. Language not supported: " +
85                                                           lang, 500);
86
87                         compiler = provider.CreateCompiler ();
88
89                         compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
90                         compilerParameters.IncludeDebugInformation = parser.Debug;
91 #if NET_2_0
92                         compilerParameters.CompilerOptions = c.CompilerOptions;
93                         compilerParameters.WarningLevel = c.WarningLevel;
94 #else
95                         compilerParameters.CompilerOptions = config.GetCompilerOptions (lang);
96                         compilerParameters.WarningLevel = config.GetWarningLevel (lang);
97 #endif
98
99                         bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
100
101                         TempFileCollection tempcoll;
102                         tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
103                         compilerParameters.TempFiles = tempcoll;
104
105                         inputFile = tempcoll.AddExtension (provider.FileExtension);
106                         Stream st = File.OpenWrite (inputFile);
107                         StreamWriter sw = new StreamWriter (st);
108                         sw.WriteLine (parser.Program);
109                         sw.Close ();
110
111                         string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
112
113                         compilerParameters.OutputAssembly = Path.Combine (DynamicDir (), dllfilename);
114
115                         CompilerResults results = CachingCompiler.Compile (this);
116                         CheckCompilerErrors (results);
117                         Assembly assembly = results.CompiledAssembly;
118                         if (assembly == null) {
119                                 if (!File.Exists (compilerParameters.OutputAssembly))
120                                         throw new CompilationException (inputFile, results.Errors,
121                                                 "No assembly returned after compilation!?");
122                                 assembly = Assembly.LoadFrom (compilerParameters.OutputAssembly);
123                         }
124
125                         results.TempFiles.Delete ();
126                         type = assembly.GetType (parser.ClassName, true);
127                         return type;
128                 }
129
130                 void CheckCompilerErrors (CompilerResults results)
131                 {
132                         if (results.NativeCompilerReturnValue == 0)
133                                 return;
134  
135                         throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
136                 }
137
138                 internal new SimpleWebHandlerParser Parser {
139                         get { return parser; }
140                 }
141
142                 internal override ICodeCompiler Compiler {
143                         get { return compiler; }
144                 }
145
146                 internal CompilerParameters CompilerOptions {
147                         get { return compilerParameters; }
148                 }
149
150                 internal string InputFile {
151                         get { return inputFile; }
152                 }
153         }
154 }
155