2004-07-02 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[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, parser.ClassName);
63                         if (type != null)
64                                 return type;
65
66                         if (parser.Program.Trim () == "")
67                                 return parser.GetTypeFromBin (parser.ClassName);
68
69                         string lang = parser.Language;
70                         CompilationConfiguration config;
71                         config = CompilationConfiguration.GetInstance (parser.Context);
72                         provider = config.GetProvider (lang);
73                         if (provider == null)
74                                 throw new HttpException ("Configuration error. Language not supported: " +
75                                                           lang, 500);
76
77                         compiler = provider.CreateCompiler ();
78
79                         compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
80                         compilerParameters.IncludeDebugInformation = parser.Debug;
81                         compilerParameters.CompilerOptions = config.GetCompilerOptions (lang);
82                         compilerParameters.WarningLevel = config.GetWarningLevel (lang);
83
84                         bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
85
86                         TempFileCollection tempcoll;
87                         tempcoll = new TempFileCollection (config.TempDirectory, keepFiles);
88                         compilerParameters.TempFiles = tempcoll;
89
90                         inputFile = tempcoll.AddExtension (provider.FileExtension);
91                         Stream st = File.OpenWrite (inputFile);
92                         StreamWriter sw = new StreamWriter (st);
93                         sw.WriteLine (parser.Program);
94                         sw.Close ();
95
96                         string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
97                         if (!Directory.Exists (dynamicBase))
98                                 Directory.CreateDirectory (dynamicBase);
99
100                         compilerParameters.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
101
102                         CompilerResults results = CachingCompiler.Compile (this);
103                         CheckCompilerErrors (results);
104                         if (results.CompiledAssembly == null)
105                                 throw new CompilationException (inputFile, results.Errors,
106                                         "No assembly returned after compilation!?");
107
108                         results.TempFiles.Delete ();
109                         return results.CompiledAssembly.GetType (parser.ClassName, true);
110                 }
111
112                 void CheckCompilerErrors (CompilerResults results)
113                 {
114                         if (results.NativeCompilerReturnValue == 0)
115                                 return;
116  
117                         throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
118                 }
119
120                 internal new SimpleWebHandlerParser Parser {
121                         get { return parser; }
122                 }
123
124                 internal override ICodeCompiler Compiler {
125                         get { return compiler; }
126                 }
127
128                 internal CompilerParameters CompilerOptions {
129                         get { return compilerParameters; }
130                 }
131
132                 internal string InputFile {
133                         get { return inputFile; }
134                 }
135         }
136 }
137