/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* Portions Copyright Apache Software Foundation.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.apache.catalina.util;
import java.lang.Process;
import java.io.File;
import java.io.Writer;
import java.io.Reader;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Locale;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Cookie;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
// import org.apache.catalina.util.StringManager;
/**
* Encapsulates the Process environment and rules to derive
* that environment from the servlet container and request information.
* @author Martin Dengler [root@martindengler.com]
* @version $Revision: 1.4 $, $Date: 2007/05/05 05:32:30 $
* @since Tomcat 4.0
*/
public class ProcessEnvironment {
private static com.sun.org.apache.commons.logging.Log log=
com.sun.org.apache.commons.logging.LogFactory.getLog( ProcessEnvironment.class );
/** context of the enclosing servlet */
private ServletContext context = null;
/** real file system directory of the enclosing servlet's web app */
private String webAppRootDir = null;
/** context path of enclosing servlet */
private String contextPath = null;
/** pathInfo for the current request */
protected String pathInfo = null;
/** servlet URI of the enclosing servlet */
private String servletPath = null;
/** derived process environment */
protected Hashtable env = null;
/** command to be invoked */
protected String command = null;
/** whether or not this object is valid or not */
protected boolean valid = false;
/** the debugging detail level for this instance. */
protected int debug = 0;
/** process' desired working directory */
protected File workingDirectory = null;
/**
* Creates a ProcessEnvironment and derives the necessary environment,
* working directory, command, etc.
* @param req HttpServletRequest for information provided by
* the Servlet API
* @param context ServletContext for information provided by
* the Servlet API
*/
public ProcessEnvironment(HttpServletRequest req,
ServletContext context) {
this(req, context, 0);
}
/**
* Creates a ProcessEnvironment and derives the necessary environment,
* working directory, command, etc.
* @param req HttpServletRequest for information provided by
* the Servlet API
* @param context ServletContext for information provided by
* the Servlet API
* @param debug int debug level (0 == none, 4 == medium, 6 == lots)
*/
public ProcessEnvironment(HttpServletRequest req,
ServletContext context, int debug) {
this.debug = debug;
setupFromContext(context);
setupFromRequest(req);
this.valid = deriveProcessEnvironment(req);
log(this.getClass().getName() + "() ctor, debug level " + debug);
}
/**
* Uses the ServletContext to set some process variables
* @param context ServletContext for information provided by
* the Servlet API
*/
protected void setupFromContext(ServletContext context) {
this.context = context;
this.webAppRootDir = context.getRealPath("/");
}
/**
* Uses the HttpServletRequest to set most process variables
* @param req HttpServletRequest for information provided by
* the Servlet API
*/
protected void setupFromRequest(HttpServletRequest req) {
this.contextPath = req.getContextPath();
this.pathInfo = req.getPathInfo();
this.servletPath = req.getServletPath();
}
/**
* Print important process environment information in an
* easy-to-read HTML table
* @return HTML string containing process environment info
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("<TABLE border=2>");
sb.append("<tr><th colspan=2 bgcolor=grey>");
sb.append("ProcessEnvironment Info</th></tr>");
sb.append("<tr><td>Debug Level</td><td>");
sb.append(debug);
sb.append("</td></tr>");
sb.append("<tr><td>Validity:</td><td>");
sb.append(isValid());
sb.append("</td></tr>");
if (isValid()) {
Enumeration envk = env.keys();
while (envk.hasMoreElements()) {
String s = (String)envk.nextElement();
sb.append("<tr><td>");
sb.append(s);
sb.append("</td><td>");
sb.append(blanksToString((String)env.get(s),
"[will be set to blank]"));
sb.append("</td></tr>");
}
}
sb.append("<tr><td colspan=2><HR></td></tr>");
sb.append("<tr><td>Derived Command</td><td>");
sb.append(nullsToBlanks(command));
sb.append("</td></tr>");
sb.append("<tr><td>Working Directory</td><td>");
if (workingDirectory != null) {
sb.append(workingDirectory.toString());
}
sb.append("</td></tr>");
sb.append("</TABLE><p>end.");
return sb.toString();
}
/**
* Gets derived command string
* @return command string
*/
public String getCommand() {
return command;
}
/**
* Sets the desired command string
* @param String command as desired
* @return command string
*/
protected String setCommand(String command) {
return command;
}
/**
* Gets this process' derived working directory
* @return working directory
*/
public File getWorkingDirectory() {
return workingDirectory;
}
/**
* Gets process' environment
* @return process' environment
*/
public Hashtable getEnvironment() {
return env;
}
/**
* Sets process' environment
* @param process' environment
* @return Hashtable to which the process' environment was set
*/
public Hashtable setEnvironment(Hashtable env) {
this.env = env;
return this.env;
}
/**
* Gets validity status
* @return true if this environment is valid, false otherwise
*/
public boolean isValid() {
return valid;
}
/**
* Converts null strings to blank strings ("")
* @param string to be converted if necessary
* @return a non-null string, either the original or the empty string
* ("") if the original was <code>null</code>
*/
protected String nullsToBlanks(String s) {
return nullsToString(s, "");
}
/**
* Converts null strings to another string
* @param string to be converted if necessary
* @param string to return instead of a null string
* @return a non-null string, either the original or the substitute
* string if the original was <code>null</code>
*/
protected String nullsToString(String couldBeNull, String subForNulls) {
return (couldBeNull == null ? subForNulls : couldBeNull);
}
/**
* Converts blank strings to another string
* @param string to be converted if necessary
* @param string to return instead of a blank string
* @return a non-null string, either the original or the substitute
* string if the original was <code>null</code> or empty ("")
*/
protected String blanksToString(String couldBeBlank,
String subForBlanks) {
return (("".equals(couldBeBlank) || couldBeBlank == null) ?
subForBlanks : couldBeBlank);
}
protected void log(String s) {
if (log.isDebugEnabled())
log.debug(s);
}
/**
* Constructs the Process environment to be supplied to the invoked
* process. Defines an environment no environment variables.
* <p>
* Should be overriden by subclasses to perform useful setup.
* </p>
*
* @param HttpServletRequest request associated with the
* Process' invocation
* @return true if environment was set OK, false if there was a problem
* and no environment was set
*/
protected boolean deriveProcessEnvironment(HttpServletRequest req) {
Hashtable envp = new Hashtable();
command = getCommand();
if (command != null) {
workingDirectory = new
File(command.substring(0,
command.lastIndexOf(File.separator)));
envp.put("X_TOMCAT_COMMAND_PATH", command); //for kicks
}
this.env = envp;
return true;
}
/**
* Gets the root directory of the web application to which this process\
* belongs
* @return root directory
*/
public String getWebAppRootDir() {
return webAppRootDir;
}
public String getContextPath(){
return contextPath;
}
public ServletContext getContext(){
return context;
}
public String getServletPath(){
return servletPath;
}
}
|