PageMapper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #if NET_2_0
  126. return GetCachedType(System.Web.Util.UrlUtils.RemoveDoubleSlashes(url));
  127. #else
  128. return GetCachedType(url);
  129. #endif
  130. }
  131. private static Type GetCachedType(string url)
  132. {
  133. ICachedXmlDoc doc = PageMapper.GetAssembliesCachedDocument();
  134. if (url.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL))
  135. url = url.Substring(IAppDomainConfig.WAR_ROOT_SYMBOL.Length);
  136. Type t = doc.Get(url);
  137. if (t == null)
  138. throw new HttpException(404,"The requested resource (" + url + ") is not available.");
  139. return t;
  140. }
  141. #region ICachedXmlDoc interface
  142. interface ICachedXmlDoc
  143. {
  144. Type Get(string key);
  145. //bool ContainsKey(object key);
  146. }
  147. #endregion
  148. #region CachedDocumentTypeStorage class
  149. class CachedDocumentTypeStorage : ICachedXmlDoc
  150. {
  151. public static readonly ICachedXmlDoc DEFAULT_DOC =
  152. new CachedDocumentTypeStorage(0);
  153. private static readonly int DEFAULT_PAGES_NUMBER = 25;
  154. private Hashtable _table;
  155. private CachedDocumentTypeStorage(int initTableSize)
  156. {
  157. _table = Hashtable.Synchronized(new Hashtable(initTableSize));
  158. }
  159. public CachedDocumentTypeStorage() :
  160. this(DEFAULT_PAGES_NUMBER)
  161. {}
  162. Type ICachedXmlDoc.Get(string o)
  163. {
  164. return GetTypeByURL(o);
  165. }
  166. internal IDictionaryEnumerator GetEnumerator()
  167. {
  168. return _table.GetEnumerator();
  169. }
  170. public Type GetTypeByURL(string url)
  171. {
  172. string lwUrl = url.ToLower();
  173. lock (_table)
  174. {
  175. object retVal = _table[lwUrl];
  176. if (retVal == null)
  177. {
  178. PageCompiler compiler = new PageCompiler(url);
  179. retVal = compiler.GetCachedType();
  180. _table[lwUrl] = retVal;
  181. }
  182. return (Type)retVal;
  183. }
  184. }
  185. }
  186. #endregion
  187. }
  188. public class PageCompiler
  189. {
  190. private static readonly string PAGE_XPATH = "preserve";
  191. private static readonly string ASSEM_ATTRIB_NAME = "assem";
  192. private static readonly string TYPE_ATTRIB_NAME = "type";
  193. private static string _parser = null;
  194. private Type _type = null;
  195. private string _xmlDescriptor = null;
  196. private string _url = null;
  197. private string _session = null;
  198. public PageCompiler(string url)
  199. {
  200. _url = url;
  201. _xmlDescriptor = GetDescFromUrl();
  202. _session = DateTime.Now.Ticks.ToString();
  203. }
  204. public Type GetCachedType()
  205. {
  206. if (_type != null)
  207. return _type;
  208. string typeName = null;
  209. //if the desciptor exists in the war - get the type
  210. string descPath = String.Join("/", new string[]{"assemblies", _xmlDescriptor});
  211. try
  212. {
  213. #if DEBUG
  214. Console.WriteLine(descPath);
  215. #endif
  216. Stream fs = (Stream)IOUtils.getStream("/" + descPath);
  217. if (fs != null)
  218. {
  219. typeName = GetTypeFromDescStream(fs);
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. #if DEBUG
  225. Console.WriteLine(ex);
  226. #endif
  227. //desc not in the war
  228. typeName = null;
  229. }
  230. if (typeName != null)
  231. {
  232. _type = Type.GetType(typeName);
  233. return _type;
  234. }
  235. string fileName = Path.GetFileName(_url);
  236. if (fileName.ToLower() != "defaultwsdlhelpgenerator.aspx")
  237. {
  238. string fullFileName = (fileName.ToLower() == "global.asax") ? _url : HttpContext.Current.Request.MapPath(_url);
  239. #if DEBUG
  240. Console.WriteLine("fullFileName=" + fullFileName);
  241. #endif
  242. if ( File.Exists(fullFileName) || Directory.Exists(fullFileName)) {
  243. //type not found - run aspxparser
  244. string[] command = GetParserCmd(fileName.ToLower() == "global.asax");
  245. if (J2EEUtils.RunProc(command) != 0)
  246. throw GetCompilerError();
  247. }
  248. else {
  249. string message = "The requested resource (" + _url + ") is not available.";
  250. throw new HttpException(404, message);
  251. }
  252. //if the desciptor exists in the real app dir - get the type
  253. try {
  254. StreamReader sr = new StreamReader (HttpContext.Current.Request.MapPath ("/" + descPath));
  255. typeName = GetTypeFromDescStream (sr.BaseStream);
  256. sr.Close ();
  257. }
  258. catch (Exception ex) {
  259. Console.WriteLine (ex);
  260. throw ex;
  261. }
  262. }
  263. else
  264. typeName = "ASP.defaultwsdlhelpgenerator_jvm_aspx";
  265. if (typeName != null)
  266. {
  267. _type = Type.GetType(typeName);
  268. return _type;
  269. }
  270. return null;
  271. }
  272. private string GetTypeFromDescStream(Stream fs)
  273. {
  274. if (fs != null)
  275. {
  276. XmlDocument descXml = new XmlDocument();
  277. descXml.Load(fs);
  278. string assem = descXml.SelectSingleNode(PAGE_XPATH).Attributes[ASSEM_ATTRIB_NAME].Value;
  279. string shortType = descXml.SelectSingleNode(PAGE_XPATH).Attributes[TYPE_ATTRIB_NAME].Value;
  280. string typeName = String.Format("{0}, {1}",shortType,assem);
  281. fs.Close();
  282. return typeName;
  283. }
  284. return null;
  285. }
  286. private string[] GetParserCmd(bool globalAsax)
  287. {
  288. string[] cmd = null;
  289. if (globalAsax)
  290. {
  291. cmd = new string[4];
  292. cmd[3] = "/buildglobalasax";
  293. }
  294. else
  295. {
  296. cmd = new string[5];
  297. cmd[3] = "/aspxFiles:" + _url;
  298. cmd[4] = "/compilepages";
  299. }
  300. cmd[0] = GetParser();
  301. cmd[1] = "/session:" + _session;
  302. cmd[2] = "/appDir:" + (string)AppDomain.CurrentDomain.GetData(IAppDomainConfig.APP_PHYS_DIR);
  303. return cmd;
  304. }
  305. private string GetParser()
  306. {
  307. if (_parser == null)
  308. {
  309. StreamReader sr =
  310. File.OpenText(HttpContext.Current.Request.MapPath("/AspxParser.params"));
  311. _parser = sr.ReadLine();
  312. sr.Close();
  313. }
  314. return _parser;
  315. }
  316. private string GetDescFromUrl()
  317. {
  318. string fileName = Path.GetFileName(_url);
  319. if (fileName.ToLower() == "global.asax")
  320. return "global.asax.xml";
  321. string id = GetIdFromUrl(_url);
  322. string[] descName = new string[3] {fileName, id, ".xml"} ;
  323. return string.Concat(descName).ToLower();
  324. }
  325. private string GetIdFromUrl(string path)
  326. {
  327. path = path.Trim('/');
  328. string fileName = Path.GetFileName(path);
  329. string id = string.Empty;
  330. path = path.Substring (path.IndexOf ("/") + 1);
  331. if (path.Length > fileName.Length)
  332. id = "." + path.Substring(0,path.Length - fileName.Length).Replace('/','_');
  333. return id;
  334. }
  335. private Exception GetCompilerError()
  336. {
  337. string _errFile = HttpContext.Current.Request.MapPath("/" + _session + ".vmwerr");
  338. if (!File.Exists(_errFile))
  339. throw new FileNotFoundException("Internal Error",_errFile);
  340. StreamReader sr = new StreamReader(_errFile);
  341. string message = string.Empty, line = null, file = null, lineInFile = "0";
  342. while ((line = sr.ReadLine()) != null)
  343. {
  344. if (line.StartsWith("Message: "))
  345. message = line.Substring("Message: ".Length);
  346. else if (line.StartsWith("File: "))
  347. file = line.Substring("File: ".Length);
  348. else if (line.StartsWith("Line: "))
  349. lineInFile = line.Substring("Line: ".Length);
  350. }
  351. sr.Close();
  352. if (file != null)
  353. {
  354. Location loc = new Location(null);
  355. loc.Filename = file;
  356. loc.BeginLine = int.Parse(lineInFile);
  357. return new ParseException(loc,message);
  358. }
  359. if (message.IndexOf(typeof(FileNotFoundException).Name) != -1 &&
  360. message.IndexOf(_url.Trim('\\','/').Replace('/','\\')) != -1)
  361. message = "The requested resource (" + _url + ") is not available.";
  362. return new HttpException(404,(message != null ? message : string.Empty));
  363. }
  364. }
  365. }