2008-11-18 Marek Habersack <mhabersack@novell.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                 string inputFile;
44
45                 public WebServiceCompiler (SimpleWebHandlerParser wService)
46                         : base (null)
47                 {
48                         this.parser = wService;
49                 }
50
51                 public static Type CompileIntoType (SimpleWebHandlerParser wService)
52                 {
53                         WebServiceCompiler wsc = new WebServiceCompiler (wService);
54                         return wsc.GetCompiledType ();
55                 }
56
57                 public override Type GetCompiledType ()
58                 {
59                         Type type = CachingCompiler.GetTypeFromCache (parser.PhysicalPath);
60                         if (type != null)
61                                 return type;
62
63                         if (parser.Program.Trim () == "") {
64                                 type = Type.GetType (parser.ClassName, false);
65                                 if (type == null)
66                                         type = parser.GetTypeFromBin (parser.ClassName);
67                                 CachingCompiler.InsertTypeFileDep (type, parser.PhysicalPath);
68                                 return type;
69                         }
70
71                         string lang = parser.Language;
72                         string compilerOptions;
73                         string tempdir;
74                         int warningLevel;
75
76                         CodeDomProvider provider;
77                         Provider = provider = CreateProvider (parser.Context, lang, out compilerOptions, out warningLevel, out tempdir);
78                         if (Provider == null)
79                                 throw new HttpException ("Configuration error. Language not supported: " +
80                                                           lang, 500);
81
82 #if !NET_2_0
83                         Compiler = provider.CreateCompiler ();
84 #endif
85
86                         CompilerParameters compilerParameters;
87                         CompilerParameters = compilerParameters = CachingCompiler.GetOptions (parser.Assemblies);
88                         compilerParameters.IncludeDebugInformation = parser.Debug;
89                         compilerParameters.CompilerOptions = compilerOptions;
90                         compilerParameters.WarningLevel = warningLevel;
91
92                         bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
93
94                         TempFileCollection tempcoll;
95                         tempcoll = new TempFileCollection (tempdir, keepFiles);
96                         compilerParameters.TempFiles = tempcoll;
97
98                         inputFile = tempcoll.AddExtension (provider.FileExtension);
99                         Stream st = File.OpenWrite (inputFile);
100                         StreamWriter sw = new StreamWriter (st);
101                         sw.WriteLine (parser.Program);
102                         sw.Close ();
103
104                         string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
105
106                         compilerParameters.OutputAssembly = Path.Combine (DynamicDir (), dllfilename);
107
108                         CompilerResults results = CachingCompiler.Compile (this);
109                         CheckCompilerErrors (results);
110                         Assembly assembly = results.CompiledAssembly;
111                         if (assembly == null) {
112                                 if (!File.Exists (compilerParameters.OutputAssembly))
113                                         throw new CompilationException (inputFile, results.Errors,
114                                                 "No assembly returned after compilation!?");
115                                 assembly = Assembly.LoadFrom (compilerParameters.OutputAssembly);
116                         }
117
118                         results.TempFiles.Delete ();
119                         type = assembly.GetType (parser.ClassName, true);
120                         CachingCompiler.InsertTypeFileDep (type, parser.PhysicalPath);
121                         return type;
122                 }
123
124                 void CheckCompilerErrors (CompilerResults results)
125                 {
126                         if (results.NativeCompilerReturnValue == 0)
127                                 return;
128  
129                         throw new CompilationException (parser.PhysicalPath, results.Errors, parser.Program);
130                 }
131
132                 internal new SimpleWebHandlerParser Parser {
133                         get { return parser; }
134                 }
135
136                 internal string InputFile {
137                         get { return inputFile; }
138                 }
139         }
140 }
141