2
0

PageMapper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  3. //
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using System;
  25. using System.Xml;
  26. using System.IO;
  27. using System.Collections;
  28. using System.Web.Compilation;
  29. using System.Collections.Specialized;
  30. using System.Threading;
  31. using vmw.common;
  32. namespace System.Web.J2EE
  33. {
  34. /// <summary>
  35. /// Class that allows reading assemblies.xml file for getting information about different types.
  36. /// </summary>
  37. public class PageMapper
  38. {
  39. private static readonly string _fileListName = "/filelist.xml";
  40. private static readonly object LOCK_GETASSEMBLIESCACHEDDOCUMENT = new object();
  41. private static readonly object LOCK_GETFROMMAPPATHCACHE = new object();
  42. public static string GetFromMapPathCache(string key)
  43. {
  44. Hashtable answer = null;
  45. lock(LOCK_GETFROMMAPPATHCACHE)
  46. {
  47. answer = (Hashtable) AppDomain.CurrentDomain.GetData(J2EEConsts.MAP_PATH_CACHE);
  48. if (answer == null)
  49. {
  50. answer = new Hashtable();
  51. CachedDocumentTypeStorage storage = (CachedDocumentTypeStorage)GetAssembliesCachedDocument();
  52. IDictionaryEnumerator e = storage.GetEnumerator();
  53. e.Reset();
  54. while (e.MoveNext())
  55. {
  56. string currentFile = (string)((DictionaryEntry)e.Current).Key;
  57. answer[currentFile]= IAppDomainConfig.WAR_ROOT_SYMBOL + currentFile;
  58. }
  59. AppDomain.CurrentDomain.SetData(J2EEConsts.MAP_PATH_CACHE,answer);
  60. }
  61. }
  62. return (string)answer[key];
  63. }
  64. // UNUSED METHOD
  65. //The method was used by runtime to force file names casesensitivity
  66. // problem. The filelist.xml file should contain correct file names,
  67. // but currently it is unused
  68. public static void LoadFileList()
  69. {
  70. Hashtable hashTable = (Hashtable) AppDomain.CurrentDomain.GetData(J2EEConsts.FILE_LIST_FILE);
  71. if (hashTable == null)
  72. {
  73. XmlDocument doc;
  74. try
  75. {
  76. Stream fs = (Stream)IOUtils.getStream(_fileListName);
  77. if (fs == null)
  78. {
  79. AppDomain.CurrentDomain.SetData(J2EEConsts.FILE_LIST_FILE, new Hashtable());
  80. return;
  81. }
  82. doc = new XmlDocument();
  83. doc.Load(fs);
  84. }
  85. catch (Exception)
  86. {
  87. // Console.WriteLine("filelist.xml was not found!!!");
  88. AppDomain.CurrentDomain.SetData(J2EEConsts.FILE_LIST_FILE, new Hashtable());
  89. return;
  90. }
  91. // Console.WriteLine("filelist.xml was found!!!");
  92. if (doc != null && doc.DocumentElement.HasChildNodes)
  93. {
  94. hashTable = CollectionsUtil.CreateCaseInsensitiveHashtable();
  95. XmlNodeList nodeList = doc.DocumentElement.ChildNodes;
  96. for (int i = 0;i < nodeList.Count ; i++)
  97. {
  98. string fileName = nodeList.Item(i).InnerText;
  99. hashTable.Add(fileName,fileName);
  100. }
  101. AppDomain.CurrentDomain.SetData(J2EEConsts.FILE_LIST_FILE, hashTable);
  102. }
  103. }
  104. }
  105. private static ICachedXmlDoc GetAssembliesCachedDocument()
  106. {
  107. lock(LOCK_GETASSEMBLIESCACHEDDOCUMENT)
  108. {
  109. ICachedXmlDoc doc = (ICachedXmlDoc) AppDomain.CurrentDomain.GetData(J2EEConsts.ASSEMBLIES_FILE);
  110. if (doc == null)
  111. {
  112. doc = CreateDocument();
  113. if (doc != null)
  114. AppDomain.CurrentDomain.SetData(J2EEConsts.ASSEMBLIES_FILE, doc);
  115. }
  116. return doc;
  117. }
  118. }
  119. private static ICachedXmlDoc CreateDocument()
  120. {
  121. return new CachedDocumentTypeStorage();
  122. }
  123. public static Type GetObjectType(string url)
  124. {
  125. return GetCachedType(url);
  126. }
  127. private static Type GetCachedType(string url)
  128. {
  129. ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument();
  130. if (url.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL))
  131. url = url.Substring(IAppDomainConfig.WAR_ROOT_SYMBOL.Length);
  132. Type t = doc.Get(url);
  133. if (t == null)
  134. throw new HttpException(404,"The requested resource (" + url + ") is not available.");
  135. return t;
  136. }
  137. #region ICachedXmlDoc interface
  138. interface ICachedXmlDoc
  139. {
  140. Type Get(string key);
  141. //bool ContainsKey(object key);
  142. }
  143. #endregion
  144. #region CachedDocumentTypeStorage class
  145. class CachedDocumentTypeStorage : ICachedXmlDoc
  146. {
  147. public static readonly ICachedXmlDoc DEFAULT_DOC =
  148. new CachedDocumentTypeStorage(0);
  149. private static readonly int DEFAULT_PAGES_NUMBER = 25;
  150. private Hashtable _table;
  151. private CachedDocumentTypeStorage(int initTableSize)
  152. {
  153. _table = Hashtable.Synchronized(new Hashtable(initTableSize));
  154. }
  155. public CachedDocumentTypeStorage() :
  156. this(DEFAULT_PAGES_NUMBER)
  157. {}
  158. Type ICachedXmlDoc.Get(string o)
  159. {
  160. return GetTypeByURL(o);
  161. }
  162. internal IDictionaryEnumerator GetEnumerator()
  163. {
  164. return _table.GetEnumerator();
  165. }
  166. public Type GetTypeByURL(string url)
  167. {
  168. string lwUrl = url.ToLower();
  169. lock (_table)
  170. {
  171. object retVal = _table[lwUrl];
  172. if (retVal == null)
  173. {
  174. PageCompiler compiler = new PageCompiler(url);
  175. retVal = compiler.GetCachedType();
  176. _table[lwUrl] = retVal;
  177. }
  178. return (Type)retVal;
  179. }
  180. }
  181. }
  182. #endregion
  183. }
  184. public class PageCompiler
  185. {
  186. private static readonly string PAGE_XPATH = "preserve";
  187. private static readonly string ASSEM_ATTRIB_NAME = "assem";
  188. private static readonly string TYPE_ATTRIB_NAME = "type";
  189. private static string _parser = null;
  190. private Type _type = null;
  191. private string _xmlDescriptor = null;
  192. private string _url = null;
  193. private string _session = null;
  194. public PageCompiler(string url)
  195. {
  196. _url = url;
  197. _xmlDescriptor = GetDescFromUrl();
  198. _session = DateTime.Now.Ticks.ToString();
  199. }
  200. public Type GetCachedType()
  201. {
  202. if (_type != null)
  203. return _type;
  204. string typeName = null;
  205. //if the desciptor exists in the war - get the type
  206. string descPath = String.Join("/", new string[]{"assemblies", _xmlDescriptor});
  207. try
  208. {
  209. #if DEBUG
  210. Console.WriteLine(descPath);
  211. #endif
  212. Stream fs = (Stream)IOUtils.getStream("/" + descPath);
  213. if (fs != null)
  214. {
  215. typeName = GetTypeFromDescStream(fs);
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. #if DEBUG
  221. Console.WriteLine(ex);
  222. #endif
  223. //desc not in the war
  224. typeName = null;
  225. }
  226. if (typeName != null)
  227. {
  228. _type = Type.GetType(typeName);
  229. return _type;
  230. }
  231. string fileName = Path.GetFileName(_url);
  232. if (fileName.ToLower() != "global.asax"
  233. && fileName.ToLower() != "defaultwsdlhelpgenerator.aspx")
  234. {
  235. string fullFileName = HttpContext.Current.Request.MapPath(_url);
  236. if ( File.Exists(fullFileName) ) {
  237. //type not found - run aspxparser
  238. string[] command = GetParserCmd();
  239. if (J2EEUtils.RunProc(command) != 0)
  240. throw GetCompilerError();
  241. }
  242. else {
  243. string message = "The requested resource (" + _url + ") is not available.";
  244. throw new HttpException(404, message);
  245. }
  246. }
  247. //if the desciptor exists in the real app dir - get the type
  248. try
  249. {
  250. StreamReader sr = new StreamReader(HttpContext.Current.Request.MapPath("/" + descPath));
  251. typeName = GetTypeFromDescStream(sr.BaseStream);
  252. sr.Close();
  253. }
  254. catch (Exception ex)
  255. {
  256. Console.WriteLine(ex);
  257. throw ex;
  258. }
  259. if (typeName != null)
  260. {
  261. _type = Type.GetType(typeName);
  262. return _type;
  263. }
  264. return null;
  265. }
  266. private string GetTypeFromDescStream(Stream fs)
  267. {
  268. if (fs != null)
  269. {
  270. XmlDocument descXml = new XmlDocument();
  271. descXml.Load(fs);
  272. string assem = descXml.SelectSingleNode(PAGE_XPATH).Attributes[ASSEM_ATTRIB_NAME].Value;
  273. string shortType = descXml.SelectSingleNode(PAGE_XPATH).Attributes[TYPE_ATTRIB_NAME].Value;
  274. string typeName = String.Format("{0}, {1}",shortType,assem);
  275. fs.Close();
  276. return typeName;
  277. }
  278. return null;
  279. }
  280. private string[] GetParserCmd()
  281. {
  282. string[] cmd = new string[5];
  283. cmd[0] = GetParser();
  284. cmd[1] = "/aspxFiles:" + _url.Trim('/').Replace('/','\\');
  285. cmd[2] = "/session:" + _session;
  286. cmd[3] = "/appDir:" + (string)AppDomain.CurrentDomain.GetData(IAppDomainConfig.APP_PHYS_DIR);
  287. cmd[4] = "/compilepages";
  288. return cmd;
  289. }
  290. private string GetParser()
  291. {
  292. if (_parser == null)
  293. {
  294. StreamReader sr =
  295. File.OpenText(HttpContext.Current.Request.MapPath("/AspxParser.params"));
  296. _parser = sr.ReadLine();
  297. sr.Close();
  298. }
  299. return _parser;
  300. }
  301. private string GetDescFromUrl()
  302. {
  303. string fileName = Path.GetFileName(_url);
  304. if (fileName.ToLower() == "global.asax")
  305. return "global.asax.xml";
  306. string id = GetIdFromUrl(_url);
  307. string[] descName = new string[3] {fileName, id, ".xml"} ;
  308. return string.Concat(descName).ToLower();
  309. }
  310. private string GetIdFromUrl(string path)
  311. {
  312. path = path.Trim('/');
  313. string fileName = Path.GetFileName(path);
  314. string id = string.Empty;
  315. if (path.Length > fileName.Length)
  316. id = "." + path.Substring(0,path.Length - fileName.Length).Replace('/','_');
  317. return id;
  318. }
  319. private Exception GetCompilerError()
  320. {
  321. string _errFile = HttpContext.Current.Request.MapPath("/" + _session + ".vmwerr");
  322. if (!File.Exists(_errFile))
  323. throw new FileNotFoundException("Internal Error",_errFile);
  324. StreamReader sr = new StreamReader(_errFile);
  325. string message = string.Empty, line = null, file = null, lineInFile = "0";
  326. while ((line = sr.ReadLine()) != null)
  327. {
  328. if (line.StartsWith("Message: "))
  329. message = line.Substring("Message: ".Length);
  330. else if (line.StartsWith("File: "))
  331. file = line.Substring("File: ".Length);
  332. else if (line.StartsWith("Line: "))
  333. lineInFile = line.Substring("Line: ".Length);
  334. }
  335. sr.Close();
  336. if (file != null)
  337. {
  338. Location loc = new Location(null);
  339. loc.Filename = file;
  340. loc.BeginLine = int.Parse(lineInFile);
  341. return new ParseException(loc,message);
  342. }
  343. if (message.IndexOf(typeof(FileNotFoundException).Name) != -1 &&
  344. message.IndexOf(_url.Trim('\\','/').Replace('/','\\')) != -1)
  345. message = "The requested resource (" + _url + ") is not available.";
  346. return new HttpException(404,(message != null ? message : string.Empty));
  347. }
  348. }
  349. }