1 2 3 27 28 package org.apache.catalina.util; 29 30 import java.lang.Process ; 31 import java.io.File ; 32 import java.io.Writer ; 33 import java.io.Reader ; 34 import java.io.PrintWriter ; 35 import java.io.BufferedWriter ; 36 import java.io.BufferedReader ; 37 import java.io.InputStream ; 38 import java.io.OutputStream ; 39 import java.io.InputStreamReader ; 40 import java.io.OutputStreamWriter ; 41 import java.io.BufferedInputStream ; 42 import java.io.BufferedOutputStream ; 43 import java.io.IOException ; 44 import java.net.URLEncoder ; 45 import java.util.Hashtable ; 46 import java.util.Vector ; 47 import java.util.Enumeration ; 48 import java.util.StringTokenizer ; 49 import java.util.Locale ; 50 import java.util.Date ; 51 import javax.servlet.ServletException ; 52 import javax.servlet.ServletOutputStream ; 53 import javax.servlet.ServletContext ; 54 import javax.servlet.ServletConfig ; 55 import javax.servlet.http.HttpServlet ; 56 import javax.servlet.http.HttpServletRequest ; 57 import javax.servlet.http.HttpServletResponse ; 58 import javax.servlet.http.HttpSession ; 59 import javax.servlet.http.Cookie ; 60 import org.apache.catalina.Context; 61 import org.apache.catalina.Wrapper; 62 64 65 66 73 public class ProcessEnvironment { 74 75 76 private static com.sun.org.apache.commons.logging.Log log= 77 com.sun.org.apache.commons.logging.LogFactory.getLog( ProcessEnvironment .class ); 78 79 80 private ServletContext context = null; 81 82 83 private String webAppRootDir = null; 84 85 86 private String contextPath = null; 87 88 89 protected String pathInfo = null; 90 91 92 private String servletPath = null; 93 94 95 protected Hashtable env = null; 96 97 98 protected String command = null; 99 100 101 protected boolean valid = false; 102 103 104 protected int debug = 0; 105 106 107 protected File workingDirectory = null; 108 109 110 118 public ProcessEnvironment(HttpServletRequest req, 119 ServletContext context) { 120 this(req, context, 0); 121 } 122 123 124 133 public ProcessEnvironment(HttpServletRequest req, 134 ServletContext context, int debug) { 135 this.debug = debug; 136 setupFromContext(context); 137 setupFromRequest(req); 138 this.valid = deriveProcessEnvironment(req); 139 log(this.getClass().getName() + "() ctor, debug level " + debug); 140 } 141 142 143 148 protected void setupFromContext(ServletContext context) { 149 this.context = context; 150 this.webAppRootDir = context.getRealPath("/"); 151 } 152 153 154 159 protected void setupFromRequest(HttpServletRequest req) { 160 this.contextPath = req.getContextPath(); 161 this.pathInfo = req.getPathInfo(); 162 this.servletPath = req.getServletPath(); 163 } 164 165 166 171 public String toString() { 172 StringBuffer sb = new StringBuffer (); 173 sb.append("<TABLE border=2>"); 174 sb.append("<tr><th colspan=2 bgcolor=grey>"); 175 sb.append("ProcessEnvironment Info</th></tr>"); 176 sb.append("<tr><td>Debug Level</td><td>"); 177 sb.append(debug); 178 sb.append("</td></tr>"); 179 sb.append("<tr><td>Validity:</td><td>"); 180 sb.append(isValid()); 181 sb.append("</td></tr>"); 182 if (isValid()) { 183 Enumeration envk = env.keys(); 184 while (envk.hasMoreElements()) { 185 String s = (String )envk.nextElement(); 186 sb.append("<tr><td>"); 187 sb.append(s); 188 sb.append("</td><td>"); 189 sb.append(blanksToString((String )env.get(s), 190 "[will be set to blank]")); 191 sb.append("</td></tr>"); 192 } 193 } 194 sb.append("<tr><td colspan=2><HR></td></tr>"); 195 sb.append("<tr><td>Derived Command</td><td>"); 196 sb.append(nullsToBlanks(command)); 197 sb.append("</td></tr>"); 198 sb.append("<tr><td>Working Directory</td><td>"); 199 if (workingDirectory != null) { 200 sb.append(workingDirectory.toString()); 201 } 202 sb.append("</td></tr>"); 203 sb.append("</TABLE><p>end."); 204 return sb.toString(); 205 } 206 207 208 212 public String getCommand() { 213 return command; 214 } 215 216 217 222 protected String setCommand(String command) { 223 return command; 224 } 225 226 227 231 public File getWorkingDirectory() { 232 return workingDirectory; 233 } 234 235 236 240 public Hashtable getEnvironment() { 241 return env; 242 } 243 244 245 250 public Hashtable setEnvironment(Hashtable env) { 251 this.env = env; 252 return this.env; 253 } 254 255 256 260 public boolean isValid() { 261 return valid; 262 } 263 264 265 271 protected String nullsToBlanks(String s) { 272 return nullsToString(s, ""); 273 } 274 275 276 283 protected String nullsToString(String couldBeNull, String subForNulls) { 284 return (couldBeNull == null ? subForNulls : couldBeNull); 285 } 286 287 288 295 protected String blanksToString(String couldBeBlank, 296 String subForBlanks) { 297 return (("".equals(couldBeBlank) || couldBeBlank == null) ? 298 subForBlanks : couldBeBlank); 299 } 300 301 302 protected void log(String s) { 303 if (log.isDebugEnabled()) 304 log.debug(s); 305 } 306 307 308 320 protected boolean deriveProcessEnvironment(HttpServletRequest req) { 321 322 Hashtable envp = new Hashtable (); 323 command = getCommand(); 324 if (command != null) { 325 workingDirectory = new 326 File (command.substring(0, 327 command.lastIndexOf(File.separator))); 328 envp.put("X_TOMCAT_COMMAND_PATH", command); } 330 this.env = envp; 331 return true; 332 } 333 334 335 340 public String getWebAppRootDir() { 341 return webAppRootDir; 342 } 343 344 345 public String getContextPath(){ 346 return contextPath; 347 } 348 349 350 public ServletContext getContext(){ 351 return context; 352 } 353 354 355 public String getServletPath(){ 356 return servletPath; 357 } 358 } 359 | Popular Tags |