importing messaging-2008 branch to trunk.
[mono.git] / mcs / class / System.Web / System.Web.Compilation / AspComponentFoundry.cs
1 //
2 // System.Web.Compilation.AspComponentFoundry
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections;
32 using System.IO;
33 using System.Reflection;
34
35 #if NET_2_0
36 using System.Collections.Generic;
37 using System.Web;
38 using System.Web.Configuration;
39 using System.Web.UI;
40 #endif
41
42 namespace System.Web.Compilation
43 {
44         internal class AspComponentFoundry
45         {
46                 Hashtable foundries;
47 #if NET_2_0
48                 Dictionary <string, AspComponent> components;
49 #else
50                 Hashtable components;
51 #endif
52
53 #if NET_2_0
54                 Dictionary <string, AspComponent> Components {
55                         get {
56                                 if (components == null)
57                                         components = new Dictionary <string, AspComponent> (StringComparer.OrdinalIgnoreCase);
58                                 return components;
59                         }
60                 }
61 #else
62                 Hashtable Components {
63                         get {
64                                 if (components == null)
65                                         components = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant, CaseInsensitiveComparer.DefaultInvariant);
66                                 return components;
67                         }
68                 }
69 #endif
70                 
71                 public AspComponentFoundry ()
72                 {
73 #if NET_2_0
74                         foundries = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
75 #else
76                         foundries = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
77                                                    CaseInsensitiveComparer.DefaultInvariant);
78 #endif
79
80                         Assembly sw = typeof (AspComponentFoundry).Assembly;
81                         RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
82                         RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
83
84 #if NET_2_0
85                         RegisterConfigControls ();
86 #endif
87                 }
88
89                 public AspComponent GetComponent (string tagName)
90                 {
91                         if (tagName == null || tagName.Length == 0)
92                                 return null;
93                         
94                         if (components != null) {
95 #if NET_2_0
96                                 AspComponent ret;
97                                 if (components.TryGetValue (tagName, out ret))
98                                         return ret;
99 #else
100                                 if (components.Contains (tagName))
101                                         return components [tagName] as AspComponent;
102 #endif
103                         }
104
105                         string foundryName, tag;
106                         int colon = tagName.IndexOf (':');
107                         if (colon > -1) {
108                                 if (colon == 0)
109                                         throw new Exception ("Empty TagPrefix is not valid.");
110                                 if (colon + 1 == tagName.Length)
111                                         return null;
112                                 foundryName = tagName.Substring (0, colon);
113                                 tag = tagName.Substring (colon + 1);
114                         } else {
115                                 foundryName = String.Empty;
116                                 tag = tagName;
117                         }
118                         
119                         object o = foundries [foundryName];                     
120                         if (o == null)
121                                 return null;
122
123                         Foundry foundry = o as Foundry;
124                         if (foundry != null)
125                                 return CreateComponent (foundry, tagName, foundryName, tag);
126                         
127                         ArrayList af = o as ArrayList;
128                         if (af == null)
129                                 return null;
130
131                         AspComponent component = null;
132                         Exception e = null;
133                         foreach (Foundry f in af) {
134                                 try {
135                                         component = CreateComponent (f, tagName, foundryName, tag);
136                                         if (component != null)
137                                                 return component;
138                                 } catch (Exception ex) {
139                                         e = ex;
140                                 }
141                         }
142
143                         if (e != null)
144                                 throw e;
145                         
146                         return null;
147                 }
148
149                 AspComponent CreateComponent (Foundry foundry, string tagName, string prefix, string tag)
150                 {
151                         string source, ns;
152                         Type type;
153
154                         type = foundry.GetType (tag, out source, out ns);
155                         if (type == null)
156                                 return null;
157                         
158                         AspComponent ret = new AspComponent (type, ns, prefix, source, foundry.FromConfig);
159 #if NET_2_0
160                         Dictionary <string, AspComponent> components = Components;
161 #else
162                         Hashtable components = Components;
163 #endif
164                         components.Add (tagName, ret);
165                         return ret;
166                 }
167                 
168                 public void RegisterFoundry (string foundryName, Assembly assembly, string nameSpace)
169                 {
170                         RegisterFoundry (foundryName, assembly, nameSpace, false);
171                 }
172                 
173                 public void RegisterFoundry (string foundryName,
174                                              Assembly assembly,
175                                              string nameSpace,
176                                              bool fromConfig)
177                 {
178                         AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
179                         foundry.FromConfig = fromConfig;
180                         InternalRegister (foundryName, foundry, fromConfig);
181                 }
182
183                 public void RegisterFoundry (string foundryName, string tagName, Type type)
184                 {
185                         RegisterFoundry (foundryName, tagName, type, false);
186                 }
187                 
188                 public void RegisterFoundry (string foundryName,
189                                              string tagName,
190                                              Type type,
191                                              bool fromConfig)
192                 {
193                         TagNameFoundry foundry = new TagNameFoundry (tagName, type);
194                         foundry.FromConfig = fromConfig;
195                         InternalRegister (foundryName, foundry, fromConfig);
196                 }
197
198 #if NET_2_0
199                 public void RegisterFoundry (string foundryName, string tagName, string source)
200                 {
201                         RegisterFoundry (foundryName, tagName, source, false);
202                 }
203                 
204                 public void RegisterFoundry (string foundryName,
205                                              string tagName,
206                                              string source,
207                                              bool fromConfig)
208                 {
209                         TagNameFoundry foundry = new TagNameFoundry (tagName, source);
210                         foundry.FromConfig = fromConfig;
211                         InternalRegister (foundryName, foundry, fromConfig);
212                 }
213
214                 public void RegisterAssemblyFoundry (string foundryName,
215                                                      string assemblyName,
216                                                      string nameSpace,
217                                                      bool fromConfig)
218                 {
219                         AssemblyFoundry foundry = new AssemblyFoundry (assemblyName, nameSpace);
220                         foundry.FromConfig = fromConfig;
221                         InternalRegister (foundryName, foundry, fromConfig);
222                 }               
223
224                 void RegisterConfigControls ()
225                 {
226                         PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
227                         if (pages == null)
228                                 return;
229
230                         TagPrefixCollection controls = pages.Controls;
231                         if (controls == null || controls.Count == 0)
232                                 return;
233                         
234                         IList appCode = BuildManager.CodeAssemblies;
235                         bool haveCodeAssemblies = appCode != null && appCode.Count > 0;
236                         Assembly asm;
237                         foreach (TagPrefixInfo tpi in controls) {
238                                 if (!String.IsNullOrEmpty (tpi.TagName))
239                                         RegisterFoundry (tpi.TagPrefix, tpi.TagName, tpi.Source, true);
240                                 else if (String.IsNullOrEmpty (tpi.Assembly)) {
241                                         if (haveCodeAssemblies) {
242                                                 foreach (object o in appCode) {
243                                                         asm = o as Assembly;
244                                                         if (asm == null)
245                                                                 continue;
246                                                         RegisterFoundry (tpi.TagPrefix, asm, tpi.Namespace, true);
247                                                 }
248                                         }
249                                 } else if (!String.IsNullOrEmpty (tpi.Namespace))
250                                         RegisterAssemblyFoundry (tpi.TagPrefix,
251                                                                  tpi.Assembly,
252                                                                  tpi.Namespace,
253                                                                  true);
254                         }
255                 }
256 #endif
257                 
258                 void InternalRegister (string foundryName, Foundry foundry, bool fromConfig)
259                 {
260                         object f = foundries [foundryName];
261                         Foundry newFoundry = null;
262                         
263                         if (f is CompoundFoundry) {
264                                 ((CompoundFoundry) f).Add (foundry);
265                                 return;
266                         } else if (f == null || f is ArrayList || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
267                                 newFoundry = foundry;
268                         } else if (f != null) {
269                                 CompoundFoundry compound = new CompoundFoundry (foundryName);
270                                 compound.Add ((Foundry) f);
271                                 compound.Add (foundry);
272                                 newFoundry = foundry;
273                                 newFoundry.FromConfig = fromConfig;
274                         }
275
276                         if (newFoundry == null)
277                                 return;
278
279                         if (f == null) {
280                                 foundries [foundryName] = newFoundry;
281                                 return;
282                         }
283
284                         ArrayList af = f as ArrayList;
285                         if (af == null) {
286                                 af = new ArrayList (2);
287                                 af.Add (f);
288                                 foundries [foundryName] = af;
289                         }
290
291                         if (newFoundry is AssemblyFoundry) {
292                                 object o;
293                                 for (int i = 0; i < af.Count; i++) {
294                                         o = af [i];
295                                         if (o is AssemblyFoundry) {
296                                                 af.Insert (i, newFoundry);
297                                                 return;
298                                         }
299                                 }
300                                 af.Add (newFoundry);
301                         } else
302                                 af.Insert (0, newFoundry);
303                 }
304
305                 public bool LookupFoundry (string foundryName)
306                 {
307                         return foundries.Contains (foundryName);
308                 }
309
310                 abstract class Foundry
311                 {
312                         bool _fromConfig;
313
314                         public bool FromConfig {
315                                 get { return _fromConfig; }
316                                 set { _fromConfig = value; }
317                         }
318                         
319                         public abstract Type GetType (string componentName, out string source, out string ns);
320                 }
321                 
322
323                 class TagNameFoundry : Foundry
324                 {
325                         string tagName;
326                         Type type;
327
328 #if NET_2_0
329                         string source;
330
331                         public bool FromWebConfig {
332                                 get { return source != null; }
333                         }
334                         
335                         public TagNameFoundry (string tagName, string source)
336                         {
337                                 this.tagName = tagName;
338                                 this.source = source;
339                         }
340 #endif
341                         
342                         public TagNameFoundry (string tagName, Type type)
343                         {
344                                 this.tagName = tagName;
345                                 this.type = type;
346                         }
347
348                         public override Type GetType (string componentName, out string source, out string ns)
349                         {
350                                 source = null;
351                                 ns = null;
352                                 if (0 != String.Compare (componentName, tagName, true))
353                                         return null;
354
355 #if NET_2_0
356                                 source = this.source;
357 #endif
358                                 return LoadType ();
359                         }
360
361                         Type LoadType ()
362                         {
363 #if NET_2_0
364                                 if (type != null)
365                                         return type;
366
367                                 HttpContext context = HttpContext.Current;
368                                 string vpath;
369                                 string realpath;
370                                 
371                                 if (VirtualPathUtility.IsAppRelative (source)) {
372                                         vpath = source;
373                                         realpath = context.Request.MapPath (source);
374                                 } else {
375                                         vpath = VirtualPathUtility.ToAppRelative (source);
376                                         realpath = source;
377                                 }
378                                 
379                                 if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
380                                         return type;
381                                 
382                                 ArrayList other_deps = new ArrayList ();
383                                 type = BuildManager.GetCompiledType (vpath);
384                                 if (type != null) {
385                                         AspGenerator.AddTypeToCache (other_deps, realpath, type);
386                                         BuildManager.AddToReferencedAssemblies (type.Assembly);
387                                 }
388                                 return type;
389 #else
390                                 return type;
391 #endif
392                         }
393                         
394                         public string TagName {
395                                 get { return tagName; }
396                         }
397                 }
398
399                 class AssemblyFoundry : Foundry
400                 {
401                         string nameSpace;
402                         Assembly assembly;
403 #if NET_2_0
404                         string assemblyName;
405                         Dictionary <string, Assembly> assemblyCache;
406 #endif
407                         
408                         public AssemblyFoundry (Assembly assembly, string nameSpace)
409                         {
410                                 this.assembly = assembly;
411                                 this.nameSpace = nameSpace;
412 #if NET_2_0
413                                 if (assembly != null)
414                                         this.assemblyName = assembly.FullName;
415                                 else
416                                         this.assemblyName = null;
417 #endif
418                         }
419
420 #if NET_2_0
421                         public AssemblyFoundry (string assemblyName, string nameSpace)
422                         {
423                                 this.assembly = null;
424                                 this.nameSpace = nameSpace;
425                                 this.assemblyName = assemblyName;
426                         }
427 #endif
428                         
429                         public override Type GetType (string componentName, out string source, out string ns)
430                         {
431                                 source = null;
432                                 ns = nameSpace;
433                                 
434 #if NET_2_0
435                                 if (assembly == null && assemblyName != null)
436                                         assembly = GetAssemblyByName (assemblyName, true);
437 #endif
438                                 string typeName = String.Concat (nameSpace, ".", componentName);
439                                 if (assembly != null)
440                                         return assembly.GetType (typeName, true, true);
441
442 #if NET_2_0
443                                 IList tla = BuildManager.TopLevelAssemblies;
444                                 if (tla != null && tla.Count > 0) {
445                                         Type ret = null;
446                                         foreach (Assembly asm in tla) {
447                                                 if (asm == null)
448                                                         continue;
449                                                 ret = asm.GetType (typeName, false, true);
450                                                 if (ret != null)
451                                                         return ret;
452                                         }
453                                 }
454 #endif
455                                 return null;
456                         }
457
458 #if NET_2_0
459                         Assembly GetAssemblyByName (string name, bool throwOnMissing)
460                         {
461                                 if (assemblyCache == null)
462                                         assemblyCache = new Dictionary <string, Assembly> ();
463                                 
464                                 if (assemblyCache.ContainsKey (name))
465                                         return assemblyCache [name];
466                                 
467                                 Assembly assembly = null;
468                                 Exception error = null;
469                                 if (name.IndexOf (',') != -1) {
470                                         try {
471                                                 assembly = Assembly.Load (name);
472                                         } catch (Exception e) { error = e; }
473                                 }
474
475                                 if (assembly == null) {
476                                         try {
477                                                 assembly = Assembly.LoadWithPartialName (name);
478                                         } catch (Exception e) { error = e; }
479                                 }
480                         
481                                 if (assembly == null)
482                                         if (throwOnMissing)
483                                                 throw new HttpException ("Assembly " + name + " not found", error);
484                                         else
485                                                 return null;
486
487                                 assemblyCache.Add (name, assembly);
488                                 return assembly;
489                         }
490 #endif
491                 }
492
493                 class CompoundFoundry : Foundry
494                 {
495                         AssemblyFoundry assemblyFoundry;
496                         Hashtable tagnames;
497                         string tagPrefix;
498
499                         public CompoundFoundry (string tagPrefix)
500                         {
501                                 this.tagPrefix = tagPrefix;
502 #if NET_2_0
503                                 tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
504 #else
505                                 tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
506                                                           CaseInsensitiveComparer.DefaultInvariant);
507 #endif
508                         }
509
510                         public void Add (Foundry foundry)
511                         {
512                                 if (foundry is AssemblyFoundry) {
513                                         assemblyFoundry = (AssemblyFoundry) foundry;
514                                         return;
515                                 }
516                                 
517                                 TagNameFoundry tn = (TagNameFoundry) foundry;
518                                 string tagName = tn.TagName;
519                                 if (tagnames.Contains (tagName)) {
520 #if NET_2_0
521                                         if (tn.FromWebConfig)
522                                                 return;
523 #endif
524                                         string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
525                                         throw new ApplicationException (msg);
526                                 }
527                                 tagnames.Add (tagName, foundry);
528                         }
529
530                         public override Type GetType (string componentName, out string source, out string ns)
531                         {
532                                 source = null;
533                                 ns = null;
534                                 Type type = null;
535                                 Foundry foundry = tagnames [componentName] as Foundry;
536                                 if (foundry != null)
537                                         return foundry.GetType (componentName, out source, out ns);
538
539                                 if (assemblyFoundry != null) {
540                                         try {
541                                                 type = assemblyFoundry.GetType (componentName, out source, out ns);
542                                                 return type;
543                                         } catch { }
544                                 }
545
546                                 string msg = String.Format ("Type {0} not registered for prefix {1}", componentName, tagPrefix);
547                                 throw new ApplicationException (msg);
548                         }
549                 }
550         }
551 }