AspComponentFoundry.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. object o = foundries [foundryName];
  62. if (o == null)
  63. return null;
  64. if (o is Foundry)
  65. return ((Foundry)o).GetType (tag);
  66. ArrayList af = o as ArrayList;
  67. if (af == null)
  68. return null;
  69. Type t;
  70. Exception e = null;
  71. foreach (Foundry f in af) {
  72. try {
  73. t = f.GetType (tag);
  74. if (t != null)
  75. return t;
  76. } catch (Exception ex) {
  77. e = ex;
  78. }
  79. }
  80. if (e != null)
  81. throw e;
  82. return null;
  83. }
  84. public void RegisterFoundry (string foundryName,
  85. Assembly assembly,
  86. string nameSpace)
  87. {
  88. AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
  89. InternalRegister (foundryName, foundry);
  90. }
  91. public void RegisterFoundry (string foundryName,
  92. string tagName,
  93. Type type)
  94. {
  95. TagNameFoundry foundry = new TagNameFoundry (tagName, type);
  96. InternalRegister (foundryName, foundry);
  97. }
  98. #if NET_2_0
  99. public void RegisterFoundry (string foundryName,
  100. string tagName,
  101. string source)
  102. {
  103. TagNameFoundry foundry = new TagNameFoundry (tagName, source);
  104. InternalRegister (foundryName, foundry);
  105. }
  106. public void RegisterAssemblyFoundry (string foundryName,
  107. string assemblyName,
  108. string nameSpace)
  109. {
  110. AssemblyFoundry foundry = new AssemblyFoundry (assemblyName, nameSpace);
  111. InternalRegister (foundryName, foundry);
  112. }
  113. void RegisterConfigControls ()
  114. {
  115. PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  116. if (pages == null)
  117. return;
  118. TagPrefixCollection controls = pages.Controls;
  119. if (controls == null || controls.Count == 0)
  120. return;
  121. IList appCode = BuildManager.CodeAssemblies;
  122. bool haveCodeAssemblies = appCode != null && appCode.Count > 0;
  123. Assembly asm;
  124. foreach (TagPrefixInfo tpi in controls) {
  125. if (!String.IsNullOrEmpty (tpi.TagName))
  126. RegisterFoundry (tpi.TagPrefix, tpi.TagName, tpi.Source);
  127. else if (String.IsNullOrEmpty (tpi.Assembly)) {
  128. if (haveCodeAssemblies) {
  129. foreach (object o in appCode) {
  130. asm = o as Assembly;
  131. if (asm == null)
  132. continue;
  133. RegisterFoundry (tpi.TagPrefix, asm, tpi.Namespace);
  134. }
  135. }
  136. } else if (!String.IsNullOrEmpty (tpi.Namespace))
  137. RegisterAssemblyFoundry (tpi.TagPrefix,
  138. tpi.Assembly,
  139. tpi.Namespace);
  140. }
  141. }
  142. #endif
  143. void InternalRegister (string foundryName, Foundry foundry)
  144. {
  145. object f = foundries [foundryName];
  146. Foundry newFoundry = null;
  147. if (f is CompoundFoundry) {
  148. ((CompoundFoundry) f).Add (foundry);
  149. return;
  150. } else if (f == null || f is ArrayList || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
  151. newFoundry = foundry;
  152. } else if (f != null) {
  153. CompoundFoundry compound = new CompoundFoundry (foundryName);
  154. compound.Add ((Foundry) f);
  155. compound.Add (foundry);
  156. newFoundry = foundry;
  157. }
  158. if (newFoundry == null)
  159. return;
  160. if (f == null) {
  161. foundries [foundryName] = newFoundry;
  162. return;
  163. }
  164. ArrayList af = f as ArrayList;
  165. if (af == null) {
  166. af = new ArrayList (2);
  167. af.Add (f);
  168. foundries [foundryName] = af;
  169. }
  170. af.Insert (0, newFoundry);
  171. }
  172. public bool LookupFoundry (string foundryName)
  173. {
  174. return foundries.Contains (foundryName);
  175. }
  176. abstract class Foundry
  177. {
  178. public abstract Type GetType (string componentName);
  179. }
  180. class TagNameFoundry : Foundry
  181. {
  182. string tagName;
  183. Type type;
  184. #if NET_2_0
  185. string source;
  186. public bool FromWebConfig {
  187. get { return source != null; }
  188. }
  189. public TagNameFoundry (string tagName, string source)
  190. {
  191. this.tagName = tagName;
  192. this.source = source;
  193. }
  194. #endif
  195. public TagNameFoundry (string tagName, Type type)
  196. {
  197. this.tagName = tagName;
  198. this.type = type;
  199. }
  200. public override Type GetType (string componentName)
  201. {
  202. if (0 != String.Compare (componentName, tagName, true))
  203. return null;
  204. return LoadType ();
  205. }
  206. Type LoadType ()
  207. {
  208. #if NET_2_0
  209. if (type != null)
  210. return type;
  211. HttpContext context = HttpContext.Current;
  212. string vpath;
  213. string realpath;
  214. if (VirtualPathUtility.IsAppRelative (source)) {
  215. vpath = source;
  216. realpath = context.Request.MapPath (source);
  217. } else {
  218. vpath = VirtualPathUtility.ToAppRelative (source);
  219. realpath = source;
  220. }
  221. if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
  222. return type;
  223. ArrayList other_deps = new ArrayList ();
  224. type = UserControlParser.GetCompiledType (vpath, realpath, other_deps, context);
  225. if (type != null) {
  226. AspGenerator.AddTypeToCache (other_deps, realpath, type);
  227. WebConfigurationManager.ExtraAssemblies.Add (type.Assembly.Location);
  228. }
  229. return type;
  230. #else
  231. return type;
  232. #endif
  233. }
  234. public string TagName {
  235. get { return tagName; }
  236. }
  237. }
  238. class AssemblyFoundry : Foundry
  239. {
  240. string nameSpace;
  241. Assembly assembly;
  242. #if NET_2_0
  243. string assemblyName;
  244. Dictionary <string, Assembly> assemblyCache;
  245. #endif
  246. public AssemblyFoundry (Assembly assembly, string nameSpace)
  247. {
  248. this.assembly = assembly;
  249. this.nameSpace = nameSpace;
  250. #if NET_2_0
  251. if (assembly != null)
  252. this.assemblyName = assembly.FullName;
  253. else
  254. this.assemblyName = null;
  255. #endif
  256. }
  257. #if NET_2_0
  258. public AssemblyFoundry (string assemblyName, string nameSpace)
  259. {
  260. this.assembly = null;
  261. this.nameSpace = nameSpace;
  262. this.assemblyName = assemblyName;
  263. }
  264. #endif
  265. public override Type GetType (string componentName)
  266. {
  267. #if NET_2_0
  268. if (assembly == null && assemblyName != null)
  269. assembly = GetAssemblyByName (assemblyName, true);
  270. #endif
  271. string typeName = String.Concat (nameSpace, ".", componentName);
  272. if (assembly != null)
  273. return assembly.GetType (typeName, true, true);
  274. #if NET_2_0
  275. IList tla = BuildManager.TopLevelAssemblies;
  276. if (tla != null && tla.Count > 0) {
  277. Type ret = null;
  278. foreach (Assembly asm in tla) {
  279. if (asm == null)
  280. continue;
  281. ret = asm.GetType (typeName, false, true);
  282. if (ret != null)
  283. return ret;
  284. }
  285. }
  286. #endif
  287. return null;
  288. }
  289. #if NET_2_0
  290. Assembly GetAssemblyByName (string name, bool throwOnMissing)
  291. {
  292. if (assemblyCache == null)
  293. assemblyCache = new Dictionary <string, Assembly> ();
  294. if (assemblyCache.ContainsKey (name))
  295. return assemblyCache [name];
  296. Assembly assembly = null;
  297. Exception error = null;
  298. if (name.IndexOf (',') != -1) {
  299. try {
  300. assembly = Assembly.Load (name);
  301. } catch (Exception e) { error = e; }
  302. }
  303. if (assembly == null) {
  304. try {
  305. assembly = Assembly.LoadWithPartialName (name);
  306. } catch (Exception e) { error = e; }
  307. }
  308. if (assembly == null)
  309. if (throwOnMissing)
  310. throw new HttpException ("Assembly " + name + " not found", error);
  311. else
  312. return null;
  313. assemblyCache.Add (name, assembly);
  314. return assembly;
  315. }
  316. #endif
  317. }
  318. class CompoundFoundry : Foundry
  319. {
  320. AssemblyFoundry assemblyFoundry;
  321. Hashtable tagnames;
  322. string tagPrefix;
  323. public CompoundFoundry (string tagPrefix)
  324. {
  325. this.tagPrefix = tagPrefix;
  326. #if NET_2_0
  327. tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  328. #else
  329. tagnames = new Hashtable (CaseInsensitiveHashCodeProvider.DefaultInvariant,
  330. CaseInsensitiveComparer.DefaultInvariant);
  331. #endif
  332. }
  333. public void Add (Foundry foundry)
  334. {
  335. if (foundry is AssemblyFoundry) {
  336. assemblyFoundry = (AssemblyFoundry) foundry;
  337. return;
  338. }
  339. TagNameFoundry tn = (TagNameFoundry) foundry;
  340. string tagName = tn.TagName;
  341. if (tagnames.Contains (tagName)) {
  342. #if NET_2_0
  343. if (tn.FromWebConfig)
  344. return;
  345. #endif
  346. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  347. throw new ApplicationException (msg);
  348. }
  349. tagnames.Add (tagName, foundry);
  350. }
  351. public override Type GetType (string componentName)
  352. {
  353. Type type = null;
  354. Foundry foundry = tagnames [componentName] as Foundry;
  355. if (foundry != null)
  356. return foundry.GetType (componentName);
  357. if (assemblyFoundry != null) {
  358. try {
  359. type = assemblyFoundry.GetType (componentName);
  360. return type;
  361. } catch { }
  362. }
  363. string msg = String.Format ("Type {0} not registered for prefix {1}",
  364. componentName, tagPrefix);
  365. throw new ApplicationException (msg);
  366. }
  367. }
  368. }
  369. }