AspComponentFoundry.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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.Collections.Generic;
  32. using System.Globalization;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Web;
  36. using System.Web.Configuration;
  37. using System.Web.UI;
  38. using System.Web.Util;
  39. namespace System.Web.Compilation
  40. {
  41. class AspComponentFoundry
  42. {
  43. Hashtable foundries;
  44. Dictionary <string, AspComponent> components;
  45. Dictionary <string, AspComponent> Components {
  46. get {
  47. if (components == null)
  48. components = new Dictionary <string, AspComponent> (StringComparer.OrdinalIgnoreCase);
  49. return components;
  50. }
  51. }
  52. public AspComponentFoundry ()
  53. {
  54. foundries = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  55. Assembly sw = typeof (AspComponentFoundry).Assembly;
  56. RegisterFoundry ("asp", sw, "System.Web.UI.WebControls");
  57. RegisterFoundry ("", "object", typeof (System.Web.UI.ObjectTag));
  58. RegisterConfigControls ();
  59. }
  60. public AspComponent GetComponent (string tagName)
  61. {
  62. if (tagName == null || tagName.Length == 0)
  63. return null;
  64. if (components != null) {
  65. AspComponent ret;
  66. if (components.TryGetValue (tagName, out ret))
  67. return ret;
  68. }
  69. string foundryName, tag;
  70. int colon = tagName.IndexOf (':');
  71. if (colon > -1) {
  72. if (colon == 0)
  73. throw new Exception ("Empty TagPrefix is not valid.");
  74. if (colon + 1 == tagName.Length)
  75. return null;
  76. foundryName = tagName.Substring (0, colon);
  77. tag = tagName.Substring (colon + 1);
  78. } else {
  79. foundryName = String.Empty;
  80. tag = tagName;
  81. }
  82. object o = foundries [foundryName];
  83. if (o == null)
  84. return null;
  85. Foundry foundry = o as Foundry;
  86. if (foundry != null)
  87. return CreateComponent (foundry, tagName, foundryName, tag);
  88. ArrayList af = o as ArrayList;
  89. if (af == null)
  90. return null;
  91. AspComponent component = null;
  92. Exception e = null;
  93. foreach (Foundry f in af) {
  94. try {
  95. component = CreateComponent (f, tagName, foundryName, tag);
  96. if (component != null)
  97. return component;
  98. } catch (Exception ex) {
  99. e = ex;
  100. }
  101. }
  102. if (e != null)
  103. throw e;
  104. return null;
  105. }
  106. AspComponent CreateComponent (Foundry foundry, string tagName, string prefix, string tag)
  107. {
  108. string source, ns;
  109. Type type;
  110. type = foundry.GetType (tag, out source, out ns);
  111. if (type == null)
  112. return null;
  113. AspComponent ret = new AspComponent (type, ns, prefix, source, foundry.FromConfig);
  114. Dictionary <string, AspComponent> components = Components;
  115. components.Add (tagName, ret);
  116. return ret;
  117. }
  118. public void RegisterFoundry (string foundryName, Assembly assembly, string nameSpace)
  119. {
  120. RegisterFoundry (foundryName, assembly, nameSpace, false);
  121. }
  122. public void RegisterFoundry (string foundryName,
  123. Assembly assembly,
  124. string nameSpace,
  125. bool fromConfig)
  126. {
  127. AssemblyFoundry foundry = new AssemblyFoundry (assembly, nameSpace);
  128. foundry.FromConfig = fromConfig;
  129. InternalRegister (foundryName, foundry, fromConfig);
  130. }
  131. public void RegisterFoundry (string foundryName, string tagName, Type type)
  132. {
  133. RegisterFoundry (foundryName, tagName, type, false);
  134. }
  135. public void RegisterFoundry (string foundryName,
  136. string tagName,
  137. Type type,
  138. bool fromConfig)
  139. {
  140. TagNameFoundry foundry = new TagNameFoundry (tagName, type);
  141. foundry.FromConfig = fromConfig;
  142. InternalRegister (foundryName, foundry, fromConfig);
  143. }
  144. public void RegisterFoundry (string foundryName, string tagName, string source)
  145. {
  146. RegisterFoundry (foundryName, tagName, source, false);
  147. }
  148. public void RegisterFoundry (string foundryName,
  149. string tagName,
  150. string source,
  151. bool fromConfig)
  152. {
  153. TagNameFoundry foundry = new TagNameFoundry (tagName, source);
  154. foundry.FromConfig = fromConfig;
  155. InternalRegister (foundryName, foundry, fromConfig);
  156. }
  157. public void RegisterAssemblyFoundry (string foundryName,
  158. string assemblyName,
  159. string nameSpace,
  160. bool fromConfig)
  161. {
  162. AssemblyFoundry foundry = new AssemblyFoundry (assemblyName, nameSpace);
  163. foundry.FromConfig = fromConfig;
  164. InternalRegister (foundryName, foundry, fromConfig);
  165. }
  166. void RegisterConfigControls ()
  167. {
  168. PagesSection pages = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  169. if (pages == null)
  170. return;
  171. TagPrefixCollection controls = pages.Controls;
  172. if (controls == null || controls.Count == 0)
  173. return;
  174. IList appCode = BuildManager.CodeAssemblies;
  175. bool haveCodeAssemblies = appCode != null && appCode.Count > 0;
  176. Assembly asm;
  177. foreach (TagPrefixInfo tpi in controls) {
  178. if (!String.IsNullOrEmpty (tpi.TagName))
  179. RegisterFoundry (tpi.TagPrefix, tpi.TagName, tpi.Source, true);
  180. else if (String.IsNullOrEmpty (tpi.Assembly)) {
  181. if (haveCodeAssemblies) {
  182. foreach (object o in appCode) {
  183. asm = o as Assembly;
  184. if (asm == null)
  185. continue;
  186. RegisterFoundry (tpi.TagPrefix, asm, tpi.Namespace, true);
  187. }
  188. }
  189. } else if (!String.IsNullOrEmpty (tpi.Namespace))
  190. RegisterAssemblyFoundry (tpi.TagPrefix,
  191. tpi.Assembly,
  192. tpi.Namespace,
  193. true);
  194. }
  195. }
  196. void InternalRegister (string foundryName, Foundry foundry, bool fromConfig)
  197. {
  198. object f = foundries [foundryName];
  199. Foundry newFoundry = null;
  200. if (f is CompoundFoundry) {
  201. ((CompoundFoundry) f).Add (foundry);
  202. return;
  203. } else if (f == null || f is ArrayList || (f is AssemblyFoundry && foundry is AssemblyFoundry)) {
  204. newFoundry = foundry;
  205. } else if (f != null) {
  206. CompoundFoundry compound = new CompoundFoundry (foundryName);
  207. compound.Add ((Foundry) f);
  208. compound.Add (foundry);
  209. newFoundry = foundry;
  210. newFoundry.FromConfig = fromConfig;
  211. }
  212. if (newFoundry == null)
  213. return;
  214. if (f == null) {
  215. foundries [foundryName] = newFoundry;
  216. return;
  217. }
  218. ArrayList af = f as ArrayList;
  219. if (af == null) {
  220. af = new ArrayList (2);
  221. af.Add (f);
  222. foundries [foundryName] = af;
  223. }
  224. if (newFoundry is AssemblyFoundry) {
  225. object o;
  226. for (int i = 0; i < af.Count; i++) {
  227. o = af [i];
  228. if (o is AssemblyFoundry) {
  229. af.Insert (i, newFoundry);
  230. return;
  231. }
  232. }
  233. af.Add (newFoundry);
  234. } else
  235. af.Insert (0, newFoundry);
  236. }
  237. public bool LookupFoundry (string foundryName)
  238. {
  239. return foundries.Contains (foundryName);
  240. }
  241. abstract class Foundry
  242. {
  243. bool _fromConfig;
  244. public bool FromConfig {
  245. get { return _fromConfig; }
  246. set { _fromConfig = value; }
  247. }
  248. public abstract Type GetType (string componentName, out string source, out string ns);
  249. }
  250. class TagNameFoundry : Foundry
  251. {
  252. string tagName;
  253. Type type;
  254. string source;
  255. public bool FromWebConfig {
  256. get { return source != null; }
  257. }
  258. public TagNameFoundry (string tagName, string source)
  259. {
  260. this.tagName = tagName;
  261. this.source = source;
  262. }
  263. public TagNameFoundry (string tagName, Type type)
  264. {
  265. this.tagName = tagName;
  266. this.type = type;
  267. }
  268. public override Type GetType (string componentName, out string source, out string ns)
  269. {
  270. source = null;
  271. ns = null;
  272. if (0 != String.Compare (componentName, tagName, true, Helpers.InvariantCulture))
  273. return null;
  274. source = this.source;
  275. return LoadType ();
  276. }
  277. Type LoadType ()
  278. {
  279. if (type != null)
  280. return type;
  281. HttpContext context = HttpContext.Current;
  282. string vpath;
  283. string realpath;
  284. if (VirtualPathUtility.IsAppRelative (source)) {
  285. vpath = source;
  286. realpath = context.Request.MapPath (source);
  287. } else {
  288. vpath = VirtualPathUtility.ToAppRelative (source);
  289. realpath = source;
  290. }
  291. if ((type = CachingCompiler.GetTypeFromCache (realpath)) != null)
  292. return type;
  293. ArrayList other_deps = new ArrayList ();
  294. type = BuildManager.GetCompiledType (vpath);
  295. if (type != null) {
  296. AspGenerator.AddTypeToCache (other_deps, realpath, type);
  297. BuildManager.AddToReferencedAssemblies (type.Assembly);
  298. }
  299. return type;
  300. }
  301. public string TagName {
  302. get { return tagName; }
  303. }
  304. }
  305. class AssemblyFoundry : Foundry
  306. {
  307. string nameSpace;
  308. Assembly assembly;
  309. string assemblyName;
  310. Dictionary <string, Assembly> assemblyCache;
  311. public AssemblyFoundry (Assembly assembly, string nameSpace)
  312. {
  313. this.assembly = assembly;
  314. this.nameSpace = nameSpace;
  315. if (assembly != null)
  316. this.assemblyName = assembly.FullName;
  317. else
  318. this.assemblyName = null;
  319. }
  320. public AssemblyFoundry (string assemblyName, string nameSpace)
  321. {
  322. this.assembly = null;
  323. this.nameSpace = nameSpace;
  324. this.assemblyName = assemblyName;
  325. }
  326. public override Type GetType (string componentName, out string source, out string ns)
  327. {
  328. source = null;
  329. ns = nameSpace;
  330. if (assembly == null && assemblyName != null)
  331. assembly = GetAssemblyByName (assemblyName, true);
  332. string typeName = String.Concat (nameSpace, ".", componentName);
  333. if (assembly != null)
  334. return assembly.GetType (typeName, false, true);
  335. IList tla = BuildManager.TopLevelAssemblies;
  336. if (tla != null && tla.Count > 0) {
  337. Type ret = null;
  338. foreach (Assembly asm in tla) {
  339. if (asm == null)
  340. continue;
  341. ret = asm.GetType (typeName, false, true);
  342. if (ret != null)
  343. return ret;
  344. }
  345. }
  346. return null;
  347. }
  348. Assembly GetAssemblyByName (string name, bool throwOnMissing)
  349. {
  350. if (assemblyCache == null)
  351. assemblyCache = new Dictionary <string, Assembly> ();
  352. if (assemblyCache.ContainsKey (name))
  353. return assemblyCache [name];
  354. Assembly assembly = null;
  355. Exception error = null;
  356. if (name.IndexOf (',') != -1) {
  357. try {
  358. assembly = Assembly.Load (name);
  359. } catch (Exception e) { error = e; }
  360. }
  361. if (assembly == null) {
  362. try {
  363. assembly = Assembly.LoadWithPartialName (name);
  364. } catch (Exception e) { error = e; }
  365. }
  366. if (assembly == null)
  367. if (throwOnMissing)
  368. throw new HttpException ("Assembly " + name + " not found", error);
  369. else
  370. return null;
  371. assemblyCache.Add (name, assembly);
  372. return assembly;
  373. }
  374. }
  375. class CompoundFoundry : Foundry
  376. {
  377. AssemblyFoundry assemblyFoundry;
  378. Hashtable tagnames;
  379. string tagPrefix;
  380. public CompoundFoundry (string tagPrefix)
  381. {
  382. this.tagPrefix = tagPrefix;
  383. tagnames = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
  384. }
  385. public void Add (Foundry foundry)
  386. {
  387. if (foundry is AssemblyFoundry) {
  388. assemblyFoundry = (AssemblyFoundry) foundry;
  389. return;
  390. }
  391. TagNameFoundry tn = (TagNameFoundry) foundry;
  392. string tagName = tn.TagName;
  393. if (tagnames.Contains (tagName)) {
  394. if (tn.FromWebConfig)
  395. return;
  396. string msg = String.Format ("{0}:{1} already registered.", tagPrefix, tagName);
  397. throw new ApplicationException (msg);
  398. }
  399. tagnames.Add (tagName, foundry);
  400. }
  401. public override Type GetType (string componentName, out string source, out string ns)
  402. {
  403. source = null;
  404. ns = null;
  405. Type type = null;
  406. Foundry foundry = tagnames [componentName] as Foundry;
  407. if (foundry != null)
  408. return foundry.GetType (componentName, out source, out ns);
  409. if (assemblyFoundry != null) {
  410. try {
  411. type = assemblyFoundry.GetType (componentName, out source, out ns);
  412. return type;
  413. } catch { }
  414. }
  415. string msg = String.Format ("Type {0} not registered for prefix {1}", componentName, tagPrefix);
  416. throw new ApplicationException (msg);
  417. }
  418. }
  419. }
  420. }