J2EEUtils.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 string GetApplicationRealPath(ServletConfig config)
  38. {
  39. string realFs = config.getInitParameter(J2EEConsts.FILESYSTEM_ACCESS);
  40. if(realFs != null && realFs == J2EEConsts.ACCESS_FULL)
  41. {
  42. try
  43. {
  44. if(Path.IsPathRooted(config.getServletContext().getRealPath("")))
  45. return config.getServletContext().getRealPath("").Replace("\\","/").TrimEnd('/');
  46. }
  47. catch (ArgumentException e)
  48. {
  49. Console.WriteLine(e.Message);
  50. }
  51. catch (Exception e)
  52. {
  53. Console.WriteLine(e.Message);
  54. }
  55. }
  56. return IAppDomainConfig.WAR_ROOT_SYMBOL;
  57. }
  58. public static string GetApplicationPhysicalPath(ServletConfig config) {
  59. string path = "";
  60. ServletContext context = config.getServletContext();
  61. string appDir = config.getInitParameter(IAppDomainConfig.APP_DIR_NAME);
  62. // Console.WriteLine("appdir = {0}", appDir);
  63. if (appDir != null)
  64. {
  65. try
  66. {
  67. java.io.File f = new java.io.File(appDir);
  68. if(f.exists())
  69. {
  70. // Console.WriteLine("Physical path= {0}", appDir);
  71. path = appDir;
  72. }
  73. }
  74. catch (Exception e)
  75. {
  76. Console.WriteLine(e.Message + appDir + "is invalid or unaccessible." +
  77. " If " + appDir + " really exists, check your security permissions");
  78. };
  79. }
  80. if (path == "")
  81. {
  82. path = GetApplicationRealPath(config);
  83. }
  84. if (!path.EndsWith ("/") && !path.EndsWith ("\\"))
  85. path += "/";
  86. // Console.WriteLine("Physical path= {0}", path);
  87. return path;
  88. }
  89. public static int RunProc(string[] cmd)
  90. {
  91. java.lang.Runtime rt = java.lang.Runtime.getRuntime();
  92. java.lang.Process proc = rt.exec(cmd);
  93. StreamGobbler errorGobbler = new
  94. StreamGobbler(proc.getErrorStream(), "ERROR");
  95. StreamGobbler outputGobbler = new
  96. StreamGobbler(proc.getInputStream(), "OUTPUT");
  97. errorGobbler.start();
  98. outputGobbler.start();
  99. int exitVal = proc.waitFor();
  100. return exitVal;
  101. }
  102. }
  103. public class StreamGobbler : java.lang.Thread
  104. {
  105. java.io.InputStream _is;
  106. String _type;
  107. public StreamGobbler(java.io.InputStream ins, String type)
  108. {
  109. this._is = ins;
  110. this._type = type;
  111. }
  112. public override void run()
  113. {
  114. try
  115. {
  116. java.io.InputStreamReader isr = new java.io.InputStreamReader(_is);
  117. java.io.BufferedReader br = new java.io.BufferedReader(isr);
  118. String line=null;
  119. while ( (line = br.readLine()) != null)
  120. {
  121. #if DEBUG
  122. Console.WriteLine(_type + ">" + line);
  123. #endif
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. #if DEBUG
  129. Console.WriteLine(ex);
  130. #endif
  131. }
  132. }
  133. }
  134. }