AspComponentFoundry.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // System.Web.Compilation.AspComponentFoundry
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Reflection;
  33. #if NET_2_0
  34. using System.Collections.Generic;
  35. using System.Web;
  36. using System.Web.Configuration;
  37. using System.Web.UI;
  38. #endif
  39. namespace System.Web.Compilation
  40. {
  41. internal class AspComponentFoundry
  42. {
  43. private Hashtable foundries;
  44. public AspComponentFoundry ()
  45. {
  46. #if NET_2_0
  47. foundries = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  48. #else
  49. foundries = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  50. CaseInsensitiveComparer.DefaultInvariant);
  51. #endif
  52. Assembly sw = typeof (AspComponentFoundry).Assembly;
  53. RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
  54. RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
  55. #if NET_2_0
  56. RegisterConfigControls ();
  57. #endif
  58. }
  59. public Type GetComponentType (string foundryName, string tag)
  60. {
  61. Foundry foundry = foundries [foundryName] as Foundry;
  62. if (foundry == null)
  63. return null;
  64. return foundry.GetType (tag);
  65. }
  66. public void RegisterFoundry (string foundryName,
  67. Assembly assembly,
  68. string nameSpace)
  69. {
  70. AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
  71. InternalRegister (foundryName, foundry);
  72. }
  73. public void RegisterFoundry (string foundryName,
  74. string tagName,
  75. Type type)
  76. {
  77. TagNameFoundry foundry = new TagNameFoundry (tagName, type);
  78. InternalRegister (foundryName, foundry);
  79. }
  80. #if NET_2_0
  81. public void RegisterFoundry (string foundryName,
  82. string tagName,
  83. string source)
  84. {
  85. TagNameFoundry foundry = new TagNameFoundry (tagName, source);
  86. InternalRegister (foundryName, foundry);
  87. }
  88. // Look up the controls/namespaces defined in the config
  89. // file(s), resolve the assemblies but do not compile the types.
  90. void RegisterConfigControls ()
  91. {
  92. PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  93. if (pages == null)
  94. return;
  95. TagPrefixCollection controls = pages.Controls;
  96. if (controls == null || controls.Count == 0)
  97. return;
  98. Dictionary <string, Assembly> assemblyCache = new Dictionary <string, Assembly> ();
  99. IList appCode = BuildManager.CodeAssemblies;
  100. bool haveCodeAssemblies = appCode != null && appCode.Count > 0;
  101. Assembly asm;
  102. foreach (TagPrefixInfo tpi in controls) {
  103. if (!String.IsNullOrEmpty (tpi.TagName))
  104. RegisterFoundry (tpi.TagPrefix, tpi.TagName, tpi.Source);
  105. else if (String.IsNullOrEmpty (tpi.Assembly)) {
  106. if (haveCodeAssemblies) {
  107. foreach (object o in appCode) {
  108. asm = o as Assembly;
  109. if (asm == null)
  110. continue;
  111. RegisterFoundry (tpi.TagPrefix, asm, tpi.Namespace);
  112. }
  113. }
  114. } else if (!String.IsNullOrEmpty (tpi.Namespace))
  115. RegisterFoundry (tpi.TagPrefix, GetAssemblyByName (assemblyCache, tpi.Assembly), tpi.Namespace);
  116. }
  117. }
  118. Assembly GetAssemblyByName (Dictionary <string, Assembly> cache, string name)
  119. {
  120. if (cache.ContainsKey (name))
  121. return cache [name];
  122. Assembly assembly = null;
  123. Exception error = null;
  124. if (name.IndexOf (',') != -1) {
  125. try {
  126. assembly = Assembly.Load (name);
  127. } catch (Exception e) { error = e; }
  128. }
  129. if (assembly == null) {
  130. try {
  131. assembly = Assembly.LoadWithPartialName (name);
  132. } catch (Exception e) { error = e; }
  133. }
  134. if (assembly == null)
  135. throw new HttpException ("Assembly " + name + " not found", error);
  136. return assembly;
  137. }
  138. #endif
  139. void InternalRegister (string foundryName, Foundry foundry)
  140. {
  141. object f = foundries [foundryName];
  142. if (f is CompoundFoundry) {
  143. ((CompoundFoundry) f).Add (foundry);
  144. } else if (f == null || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
  145. // If more than 1 namespace/assembly specified, the last one is used.
  146. foundries [foundryName] = foundry;
  147. } else if (f != null) {
  148. CompoundFoundry compound = new CompoundFoundry (foundryName);
  149. compound.Add ((Foundry) f);
  150. compound.Add (foundry);
  151. foundries [foundryName] = compound;
  152. }
  153. }
  154. public bool LookupFoundry (string foundryName)
  155. {
  156. return foundries.Contains (foundryName);
  157. }
  158. abstract class Foundry
  159. {
  160. public abstract Type GetType (string componentName);
  161. }
  162. class TagNameFoundry : Foundry
  163. {
  164. string tagName;
  165. Type type;
  166. #if NET_2_0
  167. string source;
  168. public bool FromWebConfig {
  169. get { return source != null; }
  170. }
  171. public TagNameFoundry (string tagName, string source)
  172. {
  173. this.tagName = tagName;
  174. this.source = source;
  175. }
  176. #endif
  177. public TagNameFoundry (string tagName, Type type)
  178. {
  179. this.tagName = tagName;
  180. this.type = type;
  181. }
  182. public override Type GetType (string componentName)
  183. {
  184. if (0 != String.Compare (componentName, tagName, true))
  185. return null;
  186. return LoadType ();
  187. }
  188. Type LoadType ()
  189. {
  190. #if NET_2_0
  191. if (type != null)
  192. return type;
  193. HttpContext context = HttpContext.Current;
  194. string vpath;
  195. string realpath;
  196. if (VirtualPathUtility.IsAppRelative (source)) {
  197. vpath = source;
  198. realpath = context.Request.MapPath (source);
  199. } else {
  200. vpath = VirtualPathUtility.ToAppRelative (source);
  201. realpath = source;
  202. }
  203. if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
  204. return type;
  205. ArrayList other_deps = new ArrayList ();
  206. type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, context);
  207. if (type != null) {
  208. AspGenerator.AddTypeToCache (other_deps, realpath, type);
  209. WebConfigurationManager.ExtraAssemblies.Add (type.Assembly.Location);
  210. }
  211. return type;
  212. #else
  213. return type;
  214. #endif
  215. }
  216. public string TagName {
  217. get { return tagName; }
  218. }
  219. }
  220. class AssemblyFoundry : Foundry
  221. {
  222. string nameSpace;
  223. Assembly assembly;
  224. public AssemblyFoundry (Assembly assembly, string nameSpace)
  225. {
  226. this.assembly = assembly;
  227. this.nameSpace = nameSpace;
  228. }
  229. public override Type GetType (string componentName)
  230. {
  231. if (assembly != null)
  232. return assembly.GetType (nameSpace + "." + componentName, true, true);
  233. return null;
  234. }
  235. }
  236. class CompoundFoundry : Foundry
  237. {
  238. AssemblyFoundry assemblyFoundry;
  239. Hashtable tagnames;
  240. string tagPrefix;
  241. public CompoundFoundry (string tagPrefix)
  242. {
  243. this.tagPrefix = tagPrefix;
  244. #if NET_2_0
  245. tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  246. #else
  247. tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  248. CaseInsensitiveComparer.DefaultInvariant);
  249. #endif
  250. }
  251. public void Add (Foundry foundry)
  252. {
  253. if (foundry is AssemblyFoundry) {
  254. assemblyFoundry = (AssemblyFoundry) foundry;
  255. return;
  256. }
  257. TagNameFoundry tn = (TagNameFoundry) foundry;
  258. string tagName = tn.TagName;
  259. if (tagnames.Contains (tagName)) {
  260. #if NET_2_0
  261. if (tn.FromWebConfig)
  262. return;
  263. #endif
  264. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  265. throw new ApplicationException (msg);
  266. }
  267. tagnames.Add (tagName, foundry);
  268. }
  269. public override Type GetType (string componentName)
  270. {
  271. Type type = null;
  272. Foundry foundry = tagnames [componentName] as Foundry;
  273. if (foundry != null)
  274. return foundry.GetType (componentName);
  275. if (assemblyFoundry != null) {
  276. try {
  277. type = assemblyFoundry.GetType (componentName);
  278. return type;
  279. } catch { }
  280. }
  281. string msg = String.Format ("Type {0} not registered for prefix {1}",
  282. componentName, tagPrefix);
  283. throw new ApplicationException (msg);
  284. }
  285. }
  286. }
  287. }