2 // System.Web.Compilation.AspComponentFoundry
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections;
33 using System.Reflection;
36 using System.Collections.Generic;
38 using System.Web.Configuration;
42 namespace System.Web.Compilation
44 internal class AspComponentFoundry
46 private Hashtable foundries;
48 public AspComponentFoundry ()
51 foundries = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
53 foundries = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
54 CaseInsensitiveComparer.DefaultInvariant);
57 Assembly sw = typeof (AspComponentFoundry).Assembly;
58 RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
59 RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
62 RegisterConfigControls ();
66 public Type GetComponentType (string foundryName, string tag)
68 Foundry foundry = foundries [foundryName] as Foundry;
72 return foundry.GetType (tag);
75 public void RegisterFoundry (string foundryName,
79 AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
80 InternalRegister (foundryName, foundry);
83 public void RegisterFoundry (string foundryName,
87 TagNameFoundry foundry = new TagNameFoundry (tagName, type);
88 InternalRegister (foundryName, foundry);
92 public void RegisterFoundry (string foundryName,
96 TagNameFoundry foundry = new TagNameFoundry (tagName, source);
97 InternalRegister (foundryName, foundry);
100 // Look up the controls/namespaces defined in the config
101 // file(s), resolve the assemblies but do not compile the types.
102 void RegisterConfigControls ()
104 PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
108 TagPrefixCollection controls = pages.Controls;
109 if (controls == null || controls.Count == 0)
112 Dictionary <string, Assembly> assemblyCache = new Dictionary <string, Assembly> ();
113 IList appCode = BuildManager.CodeAssemblies;
114 bool haveCodeAssemblies = appCode != null && appCode.Count > 0;
116 foreach (TagPrefixInfo tpi in controls) {
117 if (!String.IsNullOrEmpty (tpi.TagName))
118 RegisterFoundry (tpi.TagPrefix, tpi.TagName, tpi.Source);
119 else if (String.IsNullOrEmpty (tpi.Assembly)) {
120 if (haveCodeAssemblies) {
121 foreach (object o in appCode) {
125 RegisterFoundry (tpi.TagPrefix, asm, tpi.Namespace);
128 } else if (!String.IsNullOrEmpty (tpi.Namespace))
129 RegisterFoundry (tpi.TagPrefix, GetAssemblyByName (assemblyCache, tpi.Assembly), tpi.Namespace);
133 Assembly GetAssemblyByName (Dictionary <string, Assembly> cache, string name)
135 if (cache.ContainsKey (name))
138 Assembly assembly = null;
139 Exception error = null;
140 if (name.IndexOf (',') != -1) {
142 assembly = Assembly.Load (name);
143 } catch (Exception e) { error = e; }
146 if (assembly == null) {
148 assembly = Assembly.LoadWithPartialName (name);
149 } catch (Exception e) { error = e; }
152 if (assembly == null)
153 throw new HttpException ("Assembly " + name + " not found", error);
159 void InternalRegister (string foundryName, Foundry foundry)
161 object f = foundries [foundryName];
162 if (f is CompoundFoundry) {
163 ((CompoundFoundry) f).Add (foundry);
164 } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
165 // If more than 1 namespace/assembly specified, the last one is used.
166 foundries [foundryName] = foundry;
167 } else if (f != null) {
168 CompoundFoundry compound = new CompoundFoundry (foundryName);
169 compound.Add ((Foundry) f);
170 compound.Add (foundry);
171 foundries [foundryName] = compound;
175 public bool LookupFoundry (string foundryName)
177 return foundries.Contains (foundryName);
180 abstract class Foundry
182 public abstract Type GetType (string componentName);
186 class TagNameFoundry : Foundry
194 public bool FromWebConfig {
195 get { return source != null; }
198 public TagNameFoundry (string tagName, string source)
200 this.tagName = tagName;
201 this.source = source;
205 public TagNameFoundry (string tagName, Type type)
207 this.tagName = tagName;
211 public override Type GetType (string componentName)
213 if (0 != String.Compare (componentName, tagName, true))
225 HttpContext context = HttpContext.Current;
229 if (VirtualPathUtility.IsAppRelative (source)) {
231 realpath = context.Request.MapPath (source);
233 vpath = VirtualPathUtility.ToAppRelative (source);
237 if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
240 ArrayList other_deps = new ArrayList ();
241 type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, context);
243 AspGenerator.AddTypeToCache (other_deps, realpath, type);
244 WebConfigurationManager.ExtraAssemblies.Add (type.Assembly.Location);
252 public string TagName {
253 get { return tagName; }
257 class AssemblyFoundry : Foundry
262 public AssemblyFoundry (Assembly assembly, string nameSpace)
264 this.assembly = assembly;
265 this.nameSpace = nameSpace;
268 public override Type GetType (string componentName)
270 if (assembly != null)
271 return assembly.GetType (nameSpace + "." + componentName, true, true);
276 class CompoundFoundry : Foundry
278 AssemblyFoundry assemblyFoundry;
282 public CompoundFoundry (string tagPrefix)
284 this.tagPrefix = tagPrefix;
286 tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
288 tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
289 CaseInsensitiveComparer.DefaultInvariant);
293 public void Add (Foundry foundry)
295 if (foundry is AssemblyFoundry) {
296 assemblyFoundry = (AssemblyFoundry) foundry;
300 TagNameFoundry tn = (TagNameFoundry) foundry;
301 string tagName = tn.TagName;
302 if (tagnames.Contains (tagName)) {
304 if (tn.FromWebConfig)
307 string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
308 throw new ApplicationException (msg);
310 tagnames.Add (tagName, foundry);
313 public override Type GetType (string componentName)
316 Foundry foundry = tagnames [componentName] as Foundry;
318 return foundry.GetType (componentName);
320 if (assemblyFoundry != null) {
322 type = assemblyFoundry.GetType (componentName);
327 string msg = String.Format ("Type {0} not registered for prefix {1}",
328 componentName, tagPrefix);
329 throw new ApplicationException (msg);