add to the directory parser all the assemblies found by theme file parsers
[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
87                                 if (!acquired)
88                                         Monitor.Wait (ticket);
89
90                                 results = (CompilerResults) cache [key];
91 #if NET_2_0
92                                 if (!compiler.IsRebuildingPartial)
93 #endif
94                                 if (results != null)
95                                         return results;
96
97                                 ICodeCompiler comp = compiler.Compiler;
98                                 results = comp.CompileAssemblyFromDom (compiler.CompilerParameters, compiler.Unit);
99                                 string [] deps = (string []) compiler.Parser.Dependencies.ToArray (typeof (string));
100                                 cache.InsertPrivate (key, results, new CacheDependency (deps));
101
102                                 Monitor.PulseAll (ticket);
103                         } finally {
104                                 Monitor.Exit (ticket);
105                                 if (acquired)
106                                         ReleaseCompilationTicket (key);
107                         }
108
109                         return results;
110                 }
111
112                 public static CompilerResults Compile (WebServiceCompiler compiler)
113                 {
114                         string key = cachePrefix + compiler.Parser.PhysicalPath;
115                         Cache cache = HttpRuntime.Cache;
116                         CompilerResults results = (CompilerResults) cache [key];
117                         if (results != null)
118                                 return results;
119
120                         object ticket;
121                         bool acquired = AcquireCompilationTicket (key, out ticket);
122
123                         try {
124                                 Monitor.Enter (ticket);
125
126                                 if (!acquired)
127                                         Monitor.Wait (ticket);
128
129                                 results = (CompilerResults) cache [key];
130                                 if (results != null)
131                                         return results;
132
133                                 SimpleWebHandlerParser parser = compiler.Parser;
134                                 CompilerParameters options = compiler.CompilerOptions;
135                                 options.IncludeDebugInformation = parser.Debug;
136                                 results = compiler.Compiler.CompileAssemblyFromFile (options, compiler.InputFile);
137                                 string [] deps = (string []) parser.Dependencies.ToArray (typeof (string));
138                                 cache.InsertPrivate (key, results, new CacheDependency (deps));
139
140                                 Monitor.PulseAll (ticket);
141                         } finally {
142                                 Monitor.Exit (ticket);
143                                 if (acquired)
144                                         ReleaseCompilationTicket (key);
145                         }
146
147                         return results;
148                 }
149
150                 internal static CompilerParameters GetOptions (ICollection assemblies)
151                 {
152                         CompilerParameters options = new CompilerParameters ();
153                         if (assemblies != null) {
154                                 StringCollection coll = options.ReferencedAssemblies;
155                                 foreach (string str in assemblies)
156                                         coll.Add (str);
157                         }
158
159                         return options;
160                 }
161
162                 public static CompilerResults Compile (string language, string key, string file,
163                                                         ArrayList assemblies)
164                 {
165                         Cache cache = HttpRuntime.Cache;
166                         CompilerResults results = (CompilerResults) cache [cachePrefix + key];
167                         if (results != null)
168                                 return results;
169
170                         if (!Directory.Exists (dynamicBase))
171                                 Directory.CreateDirectory (dynamicBase);
172
173                         object ticket;
174                         bool acquired = AcquireCompilationTicket (cachePrefix + key, out ticket);
175
176                         try {
177                                 Monitor.Enter (ticket);
178
179                                 if (!acquired)
180                                         Monitor.Wait (ticket);
181
182                                 results = (CompilerResults) cache [cachePrefix + key];
183                                 if (results != null)
184                                         return results;
185  
186 #if NET_2_0
187                                 CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
188                                 Compiler c = config.Compilers[language];
189                                 Type t = Type.GetType (c.Type, true);
190                                 CodeDomProvider provider = Activator.CreateInstance (t) as CodeDomProvider;
191 #else
192                                 CompilationConfiguration config;
193                                 config = CompilationConfiguration.GetInstance (HttpContext.Current);
194                                 CodeDomProvider provider = config.GetProvider (language);
195 #endif
196                                 if (provider == null)
197                                         throw new HttpException ("Configuration error. Language not supported: " +
198                                                                   language, 500);
199                                 ICodeCompiler compiler = provider.CreateCompiler ();
200                                 CompilerParameters options = GetOptions (assemblies);
201                                 TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory, true);
202                                 string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
203                                 options.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
204                                 results = compiler.CompileAssemblyFromFile (options, file);
205                                 ArrayList realdeps = new ArrayList (assemblies.Count + 1);
206                                 realdeps.Add (file);
207                                 for (int i = assemblies.Count - 1; i >= 0; i--) {
208                                         string current = (string) assemblies [i];
209                                         if (Path.IsPathRooted (current))
210                                                 realdeps.Add (current);
211                                 }
212
213                                 string [] deps = (string []) realdeps.ToArray (typeof (string));
214                                 cache.InsertPrivate (cachePrefix + key, results, new CacheDependency (deps));
215
216                                 Monitor.PulseAll (ticket);
217                         } finally {
218                                 Monitor.Exit (ticket);
219                                 if (acquired)
220                                         ReleaseCompilationTicket (cachePrefix + key);
221                         }
222
223                         return results;
224                 }
225
226                 public static Type CompileAndGetType (string typename, string language, string key,
227                                                 string file, ArrayList assemblies)
228                 {
229                         CompilerResults result = CachingCompiler.Compile (language, key, file, assemblies);
230                         if (result.NativeCompilerReturnValue != 0) {
231                                 StreamReader reader = new StreamReader (file);
232                                 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
233                         }
234
235                         Assembly assembly = result.CompiledAssembly;
236                         if (assembly == null) {
237                                 StreamReader reader = new StreamReader (file);
238                                 throw new CompilationException (file, result.Errors, reader.ReadToEnd ());
239                         }
240                 
241                         Type type = assembly.GetType (typename, true);
242                         InsertType (type, file);
243                         return type;
244                 }
245
246                 static bool AcquireCompilationTicket (string key, out object ticket)
247                 {
248                         lock (compilationTickets.SyncRoot) {
249                                 ticket = compilationTickets [key];
250                                 if (ticket == null) {
251                                         ticket = new object ();
252                                         compilationTickets [key] = ticket;
253                                         return true;
254                                 }
255                         }
256                         return false;
257                 }
258
259                 static void ReleaseCompilationTicket (string key)
260                 {
261                         lock (compilationTickets.SyncRoot) {
262                                 compilationTickets.Remove (key);
263                         }
264                 }
265         }
266 }
267