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 public void RegisterAssemblyFoundry (string foundryName,
104 AssemblyFoundry foundry = new AssemblyFoundry (assemblyName, nameSpace);
105 InternalRegister (foundryName, foundry);
108 void RegisterConfigControls ()
110 PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
114 TagPrefixCollection controls = pages.Controls;
115 if (controls == null || controls.Count == 0)
118 IList appCode = BuildManager.CodeAssemblies;
119 bool haveCodeAssemblies = appCode != null && appCode.Count > 0;
121 foreach (TagPrefixInfo tpi in controls) {
122 if (!String.IsNullOrEmpty (tpi.TagName))
123 RegisterFoundry (tpi.TagPrefix, tpi.TagName, tpi.Source);
124 else if (String.IsNullOrEmpty (tpi.Assembly)) {
125 if (haveCodeAssemblies) {
126 foreach (object o in appCode) {
130 RegisterFoundry (tpi.TagPrefix, asm, tpi.Namespace);
133 } else if (!String.IsNullOrEmpty (tpi.Namespace))
134 RegisterAssemblyFoundry (tpi.TagPrefix,
141 void InternalRegister (string foundryName, Foundry foundry)
143 object f = foundries [foundryName];
144 if (f is CompoundFoundry) {
145 ((CompoundFoundry) f).Add (foundry);
146 } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
147 // If more than 1 namespace/assembly specified, the last one is used.
148 foundries [foundryName] = foundry;
149 } else if (f != null) {
150 CompoundFoundry compound = new CompoundFoundry (foundryName);
151 compound.Add ((Foundry) f);
152 compound.Add (foundry);
153 foundries [foundryName] = compound;
157 public bool LookupFoundry (string foundryName)
159 return foundries.Contains (foundryName);
162 abstract class Foundry
164 public abstract Type GetType (string componentName);
168 class TagNameFoundry : Foundry
176 public bool FromWebConfig {
177 get { return source != null; }
180 public TagNameFoundry (string tagName, string source)
182 this.tagName = tagName;
183 this.source = source;
187 public TagNameFoundry (string tagName, Type type)
189 this.tagName = tagName;
193 public override Type GetType (string componentName)
195 if (0 != String.Compare (componentName, tagName, true))
207 HttpContext context = HttpContext.Current;
211 if (VirtualPathUtility.IsAppRelative (source)) {
213 realpath = context.Request.MapPath (source);
215 vpath = VirtualPathUtility.ToAppRelative (source);
219 if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
222 ArrayList other_deps = new ArrayList ();
223 type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, context);
225 AspGenerator.AddTypeToCache (other_deps, realpath, type);
226 WebConfigurationManager.ExtraAssemblies.Add (type.Assembly.Location);
234 public string TagName {
235 get { return tagName; }
239 class AssemblyFoundry : Foundry
245 Dictionary <string, Assembly> assemblyCache;
248 public AssemblyFoundry (Assembly assembly, string nameSpace)
250 this.assembly = assembly;
251 this.nameSpace = nameSpace;
253 if (assembly != null)
254 this.assemblyName = assembly.FullName;
256 this.assemblyName = null;
261 public AssemblyFoundry (string assemblyName, string nameSpace)
263 this.assembly = null;
264 this.nameSpace = nameSpace;
265 this.assemblyName = assemblyName;
269 public override Type GetType (string componentName)
272 if (assembly == null && assemblyName != null)
273 assembly = GetAssemblyByName (assemblyName, true);
275 string typeName = String.Format ("{0}.{1}", nameSpace, componentName);
276 if (assembly != null)
277 return assembly.GetType (typeName, true, true);
280 IList tla = BuildManager.TopLevelAssemblies;
281 if (tla != null && tla.Count > 0) {
283 foreach (Assembly asm in tla) {
286 ret = asm.GetType (typeName, false, true);
296 Assembly GetAssemblyByName (string name, bool throwOnMissing)
298 if (assemblyCache == null)
299 assemblyCache = new Dictionary <string, Assembly> ();
301 if (assemblyCache.ContainsKey (name))
302 return assemblyCache [name];
304 Assembly assembly = null;
305 Exception error = null;
306 if (name.IndexOf (',') != -1) {
308 assembly = Assembly.Load (name);
309 } catch (Exception e) { error = e; }
312 if (assembly == null) {
314 assembly = Assembly.LoadWithPartialName (name);
315 } catch (Exception e) { error = e; }
318 if (assembly == null)
320 throw new HttpException ("Assembly " + name + " not found", error);
324 assemblyCache.Add (name, assembly);
330 class CompoundFoundry : Foundry
332 AssemblyFoundry assemblyFoundry;
336 public CompoundFoundry (string tagPrefix)
338 this.tagPrefix = tagPrefix;
340 tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
342 tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
343 CaseInsensitiveComparer.DefaultInvariant);
347 public void Add (Foundry foundry)
349 if (foundry is AssemblyFoundry) {
350 assemblyFoundry = (AssemblyFoundry) foundry;
354 TagNameFoundry tn = (TagNameFoundry) foundry;
355 string tagName = tn.TagName;
356 if (tagnames.Contains (tagName)) {
358 if (tn.FromWebConfig)
361 string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
362 throw new ApplicationException (msg);
364 tagnames.Add (tagName, foundry);
367 public override Type GetType (string componentName)
370 Foundry foundry = tagnames [componentName] as Foundry;
372 return foundry.GetType (componentName);
374 if (assemblyFoundry != null) {
376 type = assemblyFoundry.GetType (componentName);
381 string msg = String.Format ("Type {0} not registered for prefix {1}",
382 componentName, tagPrefix);
383 throw new ApplicationException (msg);