fixed tests
[mono.git] / mcs / class / System.Web / System.Web.Compilation / CachingCompiler.cs
1 //
2 // System.Web.Compilation.CachingCompiler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002 Ximian, Inc (http://www.ximian.com)
8 // (c) Copyright 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.Collections;
34 using System.Collections.Specialized;
35 using System.IO;
36 using System.Reflection;
37 using System.Threading;
38 using System.Web.UI;
39 using System.Web.Caching;
40 using System.Web.Configuration;
41
42 namespace System.Web.Compilation
43 {
44         class CachingCompiler
45         {
46                 static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
47                 static Hashtable compilationTickets = new Hashtable ();
48                 const string cachePrefix = "@@Assembly";
49                 const string cacheTypePrefix = "@@@Type";
50
51                 public static void InsertTypeFileDep (Type type, string filename)
52                 {
53                         CacheDependency dep = new CacheDependency (filename);
54                         HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
55                 }
56
57                 public static void InsertType (Type type, string filename)
58                 {
59                         string [] cacheKeys = new string [] { cachePrefix + filename };
60                         CacheDependency dep = new CacheDependency (null, cacheKeys);
61                         HttpRuntime.Cache.InsertPrivate (cacheTypePrefix + filename, type, dep);
62                 }
63
64                 public static Type GetTypeFromCache (string filename)
65                 {
66                         return (Type) HttpRuntime.Cache [cacheTypePrefix + filename];
67                 }
68
69                 public static CompilerResults Compile (BaseCompiler compiler)
70                 {
71                         Cache cache = HttpRuntime.Cache;
72                         string key = cachePrefix + compiler.Parser.InputFile;
73                         CompilerResults results = (CompilerResults) cache [key];
74
75 #if NET_2_0
76                         if (!compiler.IsRebuildingPartial)
77 #endif
78                         if (results != null)
79                                 return results;
80
81                         object ticket;
82                         bool acquired = AcquireCompilationTicket (key, out ticket);
83
84                         try {
85                                 Monitor.Enter (ticket);
86                                 results = (CompilerResults) cache [key];
87 #if NET_2_0
88                                 if (!compiler.IsRebuildingPartial)
89 #endif
90                                 if (results != null)
91                                         return results;
92
93                                 ICodeCompiler comp = compiler.Compiler;
94 #if NET_2_0
95                                 GetExtraAssemblies (compiler.CompilerParameters);
96 #endif
97                                 results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
98                                 string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
99                                 cache.InsertPrivate (key, results, new CacheDependency (deps));
100                         } finally {
101                                 Monitor.Exit (ticket);
102                                 if (acquired)
103                                         ReleaseCompilationTicket (key);
104                         }
105
106                         return results;
107                 }
108
109                 public static CompilerResults Compile (WebServiceCompiler compiler)
110                 {
111                         string key = cachePrefix + compiler.Parser.PhysicalPath;
112                         Cache cache = HttpRuntime.Cache;
113                         CompilerResults results = (CompilerResults) cache [key];
114                         if (results != null)
115                                 return results;
116
117                         object ticket;
118                         bool acquired = AcquireCompilationTicket (key, out ticket);
119
120                         try {
121                                 Monitor.Enter (ticket);
122                                 results = (CompilerResults) cache [key];
123                                 if (results != null)
124                                         return results;
125
126                                 SimpleWebHandlerParser parser = compiler.Parser;
127                                 CompilerParameters options = compiler.CompilerOptions;
128                                 options.IncludeDebugInformation = parser.Debug;
129 #if NET_2_0
130                                 GetExtraAssemblies (options);
131 #endif
132                                 results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
133                                 string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
134                                 cache.InsertPrivate (key, results, new CacheDependency (deps));
135                         } finally {
136                                 Monitor.Exit (ticket);
137                                 if (acquired)
138                                         ReleaseCompilationTicket (key);
139                         }
140
141                         return results;
142                 }
143
144                 internal static CompilerParameters GetOptions (ICollection assemblies)
145                 {
146                         CompilerParameters options = new CompilerParameters ();
147                         if (assemblies != null) {
148                                 StringCollection coll = options.ReferencedAssemblies;
149                                 foreach (string str in assemblies)
150                                         coll.Add (str);
151                         }
152 #if NET_2_0
153                         GetExtraAssemblies (options);
154 #endif
155                         return options;
156                 }
157
158                 public static CompilerResults Compile (string language, string key, string file,
159                                                         ArrayList assemblies)
160                 {
161                         Cache cache = HttpRuntime.Cache;
162                         CompilerResults results = (CompilerResults) cache [cachePrefix + key];
163                         if (results != null)
164                                 return results;
165
166                         if (!Directory.Exists (dynamicBase))
167                                 Directory.CreateDirectory (dynamicBase);
168
169                         object ticket;
170                         bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
171
172                         try {
173                                 Monitor.Enter (ticket);
174                                 results = (CompilerResults) cache [cachePrefix + key];
175                                 if (results != null)
176                                         return results;
177  
178 #if NET_2_0
179                                 CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
180                                 Compiler c = config.Compilers[language];
181                                 Type t = Type.GetType (c.Type, true);
182                                 CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
183 #else
184                                 CompilationConfiguration config;
185                                 config = CompilationConfiguration.GetInstance (HttpContext.Current);
186                                 CodeDomProvider provider = config.GetProvider (language);
187 #endif
188                                 if (provider == null)
189                                         throw new HttpException ("Configuration error. Language not supported: " +
190                                                                   language, 500);
191                                 ICodeCompiler compiler = provider.CreateCompiler ();
192                                 CompilerParameters options = GetOptions (assemblies);
193                                 TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
194                                 string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
195                                 options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
196                                 results = compiler.CompileAssemblyFromFile (options, file);
197                                 ArrayList realdeps = new ArrayList (assemblies.Count + 1);
198                                 realdeps.Add (file);
199                                 for (int i = assemblies.Count - 1; i >= 0; i--) {
200                                         string current = (string) assemblies [i];
201                                         if (Path.IsPathRooted (current))
202                                                 realdeps.Add (current);
203                                 }
204
205                                 string [] deps = (string []) realdeps.ToArray (typeof (string));
206                                 cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
207                         } finally {
208                                 Monitor.Exit (ticket);
209                                 if (acquired)
210                                         ReleaseCompilationTicket (cachePrefix + key);
211                         }
212
213                         return results;
214                 }
215
216                 public static Type CompileAndGetType (string typename, string language, string key,
217                                                 string file, ArrayList assemblies)
218                 {
219                         CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
220                         if (result.NativeCompilerReturnValue != 0) {
221                                 StreamReader reader = new StreamReader (file);
222                                 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
223                         }
224
225                         Assembly assembly = result.CompiledAssembly;
226                         if (assembly == null) {
227                                 StreamReader reader = new StreamReader (file);
228                                 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
229                         }
230                 
231                         Type type = assembly.GetType (typename, true);
232                         InsertType (type, file);
233                         return type;
234                 }
235
236 #if NET_2_0
237                 static void GetExtraAssemblies (CompilerParameters options)
238                 {
239                         ArrayList al = WebConfigurationManager.ExtraAssemblies;
240                         StringCollection refAsm = options.ReferencedAssemblies;
241                         string asmName;
242                         
243                         if (al != null && al.Count > 0) {
244                                 foreach (object o in al) {
245                                         asmName = o as string;
246                                         if (asmName != null && !refAsm.Contains (asmName))
247                                                 refAsm.Add (asmName);
248                                 }
249                         }
250
251                         IList list = BuildManager.CodeAssemblies;
252                         if (list != null && list.Count > 0) {
253                                 foreach (object o in list) {
254                                         asmName = o as string;
255                                         if (asmName != null && !refAsm.Contains (asmName))
256                                                 refAsm.Add (asmName);
257                                 }
258                         }
259                         
260                         list = BuildManager.TopLevelAssemblies;
261                         if (list != null && list.Count > 0) {
262                                 foreach (Assembly a in list) {
263                                         asmName = a.Location;
264                                         if (!refAsm.Contains (asmName))
265                                                 refAsm.Add (asmName);
266                                 }
267                         }
268                 }
269 #endif
270
271                 static bool AcquireCompilationTicket (string key, out object ticket)
272                 {
273                         lock (compilationTickets.SyncRoot) {
274                                 ticket = compilationTickets [key];
275                                 if (ticket == null) {
276                                         ticket = new object ();
277                                         compilationTickets [key] = ticket;
278                                         return true;
279                                 }
280                         }
281                         return false;
282                 }
283
284                 static void ReleaseCompilationTicket (string key)
285                 {
286                         lock (compilationTickets.SyncRoot) {
287                                 compilationTickets.Remove (key);
288                         }
289                 }
290         }
291 }
292