PageMapper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. public static string GetFromMapPathCache(string key)
  41. {
  42. Hashtable answer = (Hashtable) AppDomain.CurrentDomain.GetData(J2EEConsts.MAP_PATH_CACHE);
  43. if (answer == null)
  44. {
  45. answer = new Hashtable();
  46. CachedDocumentTypeStorage storage = (CachedDocumentTypeStorage)GetAssembliesCachedDocument();
  47. IDictionaryEnumerator e = storage.GetEnumerator();
  48. e.Reset();
  49. while (e.MoveNext())
  50. {
  51. string currentFile = (string)((DictionaryEntry)e.Current).Key;
  52. answer[currentFile]= IAppDomainConfig.WAR_ROOT_SYMBOL + currentFile;
  53. }
  54. AppDomain.CurrentDomain.SetData(J2EEConsts.MAP_PATH_CACHE,answer);
  55. }
  56. return (string)answer[key];
  57. }
  58. public static void LoadFileList()
  59. {
  60. Hashtable hashTable = (Hashtable) AppDomain.CurrentDomain.GetData(J2EEConsts.FILE_LIST_FILE);
  61. if (hashTable == null)
  62. {
  63. XmlDocument doc;
  64. try
  65. {
  66. Stream fs = (Stream)IOUtils.getStream(_fileListName);
  67. doc = new XmlDocument();
  68. doc.Load(fs);
  69. }
  70. catch (Exception)
  71. {
  72. // Console.WriteLine("filelist.xml was not found!!!");
  73. AppDomain.CurrentDomain.SetData(J2EEConsts.FILE_LIST_FILE, new Hashtable());
  74. return;
  75. }
  76. // Console.WriteLine("filelist.xml was found!!!");
  77. if (doc != null && doc.DocumentElement.HasChildNodes)
  78. {
  79. hashTable = CollectionsUtil.CreateCaseInsensitiveHashtable();
  80. XmlNodeList nodeList = doc.DocumentElement.ChildNodes;
  81. for (int i = 0;i < nodeList.Count ; i++)
  82. {
  83. string fileName = nodeList.Item(i).InnerText;
  84. hashTable.Add(fileName,fileName);
  85. }
  86. AppDomain.CurrentDomain.SetData(J2EEConsts.FILE_LIST_FILE, hashTable);
  87. }
  88. }
  89. }
  90. private static readonly object LOCK_OBJECT = new object();
  91. private static ICachedXmlDoc GetAssembliesCachedDocument()
  92. {
  93. lock(LOCK_OBJECT)
  94. {
  95. ICachedXmlDoc doc = (ICachedXmlDoc) AppDomain.CurrentDomain.GetData(J2EEConsts.ASSEMBLIES_FILE);
  96. if (doc == null)
  97. {
  98. doc = CreateDocument();
  99. if (doc != null)
  100. AppDomain.CurrentDomain.SetData(J2EEConsts.ASSEMBLIES_FILE, doc);
  101. }
  102. return doc;
  103. }
  104. }
  105. private static ICachedXmlDoc CreateDocument()
  106. {
  107. return new CachedDocumentTypeStorage();
  108. }
  109. public static Type GetObjectType(string url)
  110. {
  111. return GetCachedType(url);
  112. }
  113. private static Type GetCachedType(string url)
  114. {
  115. ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument();
  116. if (url.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL))
  117. url = url.Substring(IAppDomainConfig.WAR_ROOT_SYMBOL.Length);
  118. string query = url.ToLower();
  119. Type t = (Type) doc.Get(query);
  120. if (t == null)
  121. throw new HttpException(404,"The requested resource (" + url + ") is not available.");
  122. return t;
  123. }
  124. #region ICachedXmlDoc interface
  125. interface ICachedXmlDoc
  126. {
  127. object Get(object key);
  128. //bool ContainsKey(object key);
  129. }
  130. #endregion
  131. #region CachedDocumentTypeStorage class
  132. class CachedDocumentTypeStorage : ICachedXmlDoc
  133. {
  134. public static readonly ICachedXmlDoc DEFAULT_DOC =
  135. new CachedDocumentTypeStorage(0);
  136. private static readonly int DEFAULT_PAGES_NUMBER = 25;
  137. private Hashtable _table;
  138. private CachedDocumentTypeStorage(int initTableSize)
  139. {
  140. _table = new Hashtable(initTableSize);
  141. }
  142. public CachedDocumentTypeStorage() :
  143. this(DEFAULT_PAGES_NUMBER)
  144. {}
  145. object ICachedXmlDoc.Get(object o)
  146. {
  147. if(o is string)
  148. return GetTypeByURL((string) o);
  149. throw new ArgumentException("the key should be a string");
  150. }
  151. internal IDictionaryEnumerator GetEnumerator()
  152. {
  153. return _table.GetEnumerator();
  154. }
  155. public Type GetTypeByURL(string url)
  156. {
  157. if (!_table.ContainsKey(url))
  158. {
  159. PageCompiler compiler = new PageCompiler(url);
  160. _table[url] = compiler.GetCachedType();
  161. }
  162. return (Type)_table[url];
  163. }
  164. }
  165. #endregion
  166. }
  167. public class PageCompiler
  168. {
  169. private static readonly string PAGE_XPATH = "preserve";
  170. private static readonly string ASSEM_ATTRIB_NAME = "assem";
  171. private static readonly string TYPE_ATTRIB_NAME = "type";
  172. private static string _parser = null;
  173. private Type _type = null;
  174. private string _xmlDescriptor = null;
  175. private string _url = null;
  176. private string _session = null;
  177. public PageCompiler(string url)
  178. {
  179. _url = url;
  180. _xmlDescriptor = GetDescFromUrl();
  181. _session = DateTime.Now.Ticks.ToString();
  182. }
  183. public Type GetCachedType()
  184. {
  185. if (_type != null)
  186. return _type;
  187. string typeName = null;
  188. //if the desciptor exists in the war - get the type
  189. string descPath = String.Join("/", new string[]{"assemblies", _xmlDescriptor});
  190. try
  191. {
  192. #if DEBUG
  193. Console.WriteLine(descPath);
  194. #endif
  195. Stream fs = (Stream)IOUtils.getStream("/" + descPath);
  196. typeName = GetTypeFromDescStream(fs);
  197. }
  198. catch (Exception ex)
  199. {
  200. #if DEBUG
  201. Console.WriteLine(ex);
  202. #endif
  203. //desc not in the war
  204. typeName = null;
  205. }
  206. if (typeName != null)
  207. {
  208. _type = Type.GetType(typeName);
  209. return _type;
  210. }
  211. string fileName = Path.GetFileName(_url);
  212. if (fileName.ToLower() != "global.asax"
  213. && fileName.ToLower() != "defaultwsdlhelpgenerator.aspx")
  214. {
  215. //type not found - run aspxparser
  216. string[] command = GetParserCmd();
  217. if (J2EEUtils.RunProc(command) != 0)
  218. throw GetCompilerError();
  219. }
  220. //if the desciptor exists in the real app dir - get the type
  221. try
  222. {
  223. StreamReader sr = new StreamReader(HttpContext.Current.Request.MapPath("/" + descPath));
  224. typeName = GetTypeFromDescStream(sr.BaseStream);
  225. sr.Close();
  226. }
  227. catch (Exception ex)
  228. {
  229. Console.WriteLine(ex);
  230. throw ex;
  231. }
  232. if (typeName != null)
  233. {
  234. _type = Type.GetType(typeName);
  235. return _type;
  236. }
  237. return null;
  238. }
  239. private string GetTypeFromDescStream(Stream fs)
  240. {
  241. if (fs != null)
  242. {
  243. XmlDocument descXml = new XmlDocument();
  244. descXml.Load(fs);
  245. string assem = descXml.SelectSingleNode(PAGE_XPATH).Attributes[ASSEM_ATTRIB_NAME].Value;
  246. string shortType = descXml.SelectSingleNode(PAGE_XPATH).Attributes[TYPE_ATTRIB_NAME].Value;
  247. string typeName = String.Format("{0}, {1}",shortType,assem);
  248. fs.Close();
  249. return typeName;
  250. }
  251. return null;
  252. }
  253. private string[] GetParserCmd()
  254. {
  255. string[] cmd = new string[5];
  256. cmd[0] = GetParser();
  257. cmd[1] = "/aspxFiles:" + _url.Trim('/').Replace('/','\\');
  258. cmd[2] = "/session:" + _session;
  259. cmd[3] = "/appDir:" + (string)AppDomain.CurrentDomain.GetData(IAppDomainConfig.APP_PHYS_DIR);
  260. cmd[4] = "/compilepages";
  261. return cmd;
  262. }
  263. private string GetParser()
  264. {
  265. if (_parser == null)
  266. {
  267. StreamReader sr =
  268. File.OpenText(HttpContext.Current.Request.MapPath("/AspxParser.params"));
  269. _parser = sr.ReadLine();
  270. sr.Close();
  271. }
  272. return _parser;
  273. }
  274. private string GetDescFromUrl()
  275. {
  276. string fileName = Path.GetFileName(_url);
  277. if (fileName.ToLower() == "global.asax")
  278. return "global.asax.xml";
  279. string id = GetIdFromUrl(_url);
  280. string[] descName = new string[3] {fileName, id, ".xml"} ;
  281. return string.Concat(descName).ToLower();
  282. }
  283. private string GetIdFromUrl(string path)
  284. {
  285. path = path.Trim('/');
  286. string fileName = Path.GetFileName(path);
  287. string id = string.Empty;
  288. if (path.Length > fileName.Length)
  289. id = "." + path.Substring(0,path.Length - fileName.Length).Replace('/','_');
  290. return id;
  291. }
  292. private Exception GetCompilerError()
  293. {
  294. string _errFile = HttpContext.Current.Request.MapPath("/" + _session + ".vmwerr");
  295. if (!File.Exists(_errFile))
  296. throw new FileNotFoundException("Internal Error",_errFile);
  297. StreamReader sr = new StreamReader(_errFile);
  298. string message = string.Empty, line = null, file = null, lineInFile = "0";
  299. while ((line = sr.ReadLine()) != null)
  300. {
  301. if (line.StartsWith("Message: "))
  302. message = line.Substring("Message: ".Length);
  303. else if (line.StartsWith("File: "))
  304. file = line.Substring("File: ".Length);
  305. else if (line.StartsWith("Line: "))
  306. lineInFile = line.Substring("Line: ".Length);
  307. }
  308. sr.Close();
  309. if (file != null)
  310. {
  311. try {
  312. Location loc = new Location(null);
  313. loc.Filename = file;
  314. loc.BeginLine = int.Parse(lineInFile);
  315. return new ParseException(loc,message);
  316. }
  317. // If file doesn't exist, or is unreadable just go on and send
  318. // the original error message.
  319. catch(Exception e) {}
  320. }
  321. if (message.IndexOf(typeof(FileNotFoundException).Name) != -1 &&
  322. message.IndexOf(_url.Trim('\\','/').Replace('/','\\')) != -1)
  323. message = "The requested resource (" + _url + ") is not available.";
  324. return new HttpException(404,(message != null ? message : string.Empty));
  325. }
  326. }
  327. }
  328. namespace System.Web.GH
  329. {
  330. public class PageMapper : System.Web.J2EE.PageMapper
  331. {
  332. }
  333. }