J2EEUtils.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.Web.Util;
  26. using System.IO;
  27. using [email protected];
  28. using vmw.common;
  29. using javax.servlet;
  30. namespace System.Web.J2EE
  31. {
  32. public class J2EEUtils
  33. {
  34. public J2EEUtils()
  35. {
  36. }
  37. public static bool FileExists(string fileName)
  38. {
  39. //First reduce virtual or phys path
  40. if (fileName.StartsWith(IAppDomainConfig.WAR_ROOT_SYMBOL))
  41. fileName = fileName.Substring(IAppDomainConfig.WAR_ROOT_SYMBOL.Length);
  42. if (fileName.StartsWith(HttpRuntime.AppDomainAppVirtualPath + "/"))
  43. fileName = fileName.Substring(HttpRuntime.AppDomainAppVirtualPath.Length + 1);
  44. if (fileName.StartsWith(HttpRuntime.AppDomainAppPath))
  45. fileName = fileName.Substring(HttpRuntime.AppDomainAppPath.Length);
  46. IResourceLoader resLoader = (IResourceLoader)AppDomain.CurrentDomain.GetData("GH_ResourceLoader");
  47. if (resLoader == null)
  48. throw new HttpException("Unexpected exception in GHUtils.FileExists method. Resource loader not initialized under current domain");
  49. java.net.URL res = resLoader.getResource(fileName);
  50. if (res == null)
  51. {
  52. if(fileName.StartsWith("/"))
  53. fileName = fileName.Substring(1);
  54. java.lang.ClassLoader cl = (java.lang.ClassLoader)AppDomain.CurrentDomain.GetData("GH_ContextClassLoader");
  55. if (cl != null)
  56. res = cl.getResource(fileName);
  57. }
  58. if (res == null)
  59. return false;
  60. return true;
  61. }
  62. public static string GetApplicationRealPath(ServletConfig config)
  63. {
  64. string realFs = config.getInitParameter(J2EEConsts.FILESYSTEM_ACCESS);
  65. if(realFs != null && realFs == J2EEConsts.ACCESS_FULL)
  66. {
  67. try
  68. {
  69. if(Path.IsPathRooted(config.getServletContext().getRealPath("")))
  70. return config.getServletContext().getRealPath("").Replace("\\","/").TrimEnd('/');
  71. }
  72. catch (ArgumentException e)
  73. {
  74. Console.WriteLine(e.Message);
  75. }
  76. catch (Exception e)
  77. {
  78. Console.WriteLine(e.Message);
  79. }
  80. }
  81. return IAppDomainConfig.WAR_ROOT_SYMBOL;
  82. }
  83. public static string GetApplicationPhysicalPath(ServletConfig config) {
  84. string path = "";
  85. ServletContext context = config.getServletContext();
  86. string appDir = config.getInitParameter(IAppDomainConfig.APP_DIR_NAME);
  87. Console.WriteLine("appdir = {0}", appDir);
  88. if (appDir != null)
  89. {
  90. try
  91. {
  92. java.io.File f = new java.io.File(appDir);
  93. if(f.exists())
  94. {
  95. Console.WriteLine("Physical path= {0}", appDir);
  96. path = appDir;
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. Console.WriteLine(e.Message + appDir + "is invalid or unaccessible." +
  102. " If " + appDir + " really exists, check your security permissions");
  103. };
  104. }
  105. if (path == "")
  106. {
  107. path = GetApplicationRealPath(config);
  108. }
  109. if (!path.EndsWith ("/") && !path.EndsWith ("\\"))
  110. path += "/";
  111. Console.WriteLine("Physical path= {0}", path);
  112. return path;
  113. }
  114. public static int RunProc(string[] cmd)
  115. {
  116. java.lang.Runtime rt = java.lang.Runtime.getRuntime();
  117. java.lang.Process proc = rt.exec(cmd);
  118. StreamGobbler errorGobbler = new
  119. StreamGobbler(proc.getErrorStream(), "ERROR");
  120. StreamGobbler outputGobbler = new
  121. StreamGobbler(proc.getInputStream(), "OUTPUT");
  122. errorGobbler.start();
  123. outputGobbler.start();
  124. int exitVal = proc.waitFor();
  125. return exitVal;
  126. }
  127. }
  128. public class StreamGobbler : java.lang.Thread
  129. {
  130. java.io.InputStream _is;
  131. String _type;
  132. public StreamGobbler(java.io.InputStream ins, String type)
  133. {
  134. this._is = ins;
  135. this._type = type;
  136. }
  137. public override void run()
  138. {
  139. try
  140. {
  141. java.io.InputStreamReader isr = new java.io.InputStreamReader(_is);
  142. java.io.BufferedReader br = new java.io.BufferedReader(isr);
  143. String line=null;
  144. while ( (line = br.readLine()) != null)
  145. {
  146. #if DEBUG
  147. Console.WriteLine(_type + ">" + line);
  148. #endif
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. #if DEBUG
  154. Console.WriteLine(ex);
  155. #endif
  156. }
  157. }
  158. }
  159. }