FileDocCategorySizeDatePackage
ProcessEnvironment.javaAPI DocGlassfish v2 API11950Fri May 04 22:32:30 BST 2007org.apache.catalina.util

ProcessEnvironment

public class ProcessEnvironment extends Object
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

Fields Summary
private static com.sun.org.apache.commons.logging.Log
log
private ServletContext
context
context of the enclosing servlet
private String
webAppRootDir
real file system directory of the enclosing servlet's web app
private String
contextPath
context path of enclosing servlet
protected String
pathInfo
pathInfo for the current request
private String
servletPath
servlet URI of the enclosing servlet
protected Hashtable
env
derived process environment
protected String
command
command to be invoked
protected boolean
valid
whether or not this object is valid or not
protected int
debug
the debugging detail level for this instance.
protected File
workingDirectory
process' desired working directory
Constructors Summary
public ProcessEnvironment(HttpServletRequest req, ServletContext context)
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



                                                                                       
      
          
        this(req, context, 0);
    
public ProcessEnvironment(HttpServletRequest req, ServletContext context, int debug)
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)

            this.debug = debug;
            setupFromContext(context);
            setupFromRequest(req);
            this.valid = deriveProcessEnvironment(req);
            log(this.getClass().getName() + "() ctor, debug level " + debug);
    
Methods Summary
protected java.lang.StringblanksToString(java.lang.String couldBeBlank, java.lang.String subForBlanks)
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 null or empty ("")

            return (("".equals(couldBeBlank) || couldBeBlank == null) ?
                subForBlanks : couldBeBlank);
    
protected booleanderiveProcessEnvironment(javax.servlet.http.HttpServletRequest req)
Constructs the Process environment to be supplied to the invoked process. Defines an environment no environment variables.

Should be overriden by subclasses to perform useful setup.

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


        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;
    
public java.lang.StringgetCommand()
Gets derived command string

return
command string

        return command;
    
public javax.servlet.ServletContextgetContext()

            return context;
        
public java.lang.StringgetContextPath()

            return contextPath;
        
public java.util.HashtablegetEnvironment()
Gets process' environment

return
process' environment

        return env;
    
public java.lang.StringgetServletPath()

            return servletPath;
        
public java.lang.StringgetWebAppRootDir()
Gets the root directory of the web application to which this process\ belongs

return
root directory

        return webAppRootDir;
    
public java.io.FilegetWorkingDirectory()
Gets this process' derived working directory

return
working directory

        return workingDirectory;
    
public booleanisValid()
Gets validity status

return
true if this environment is valid, false otherwise

        return valid;
    
protected voidlog(java.lang.String s)

        if (log.isDebugEnabled())
            log.debug(s);
    
protected java.lang.StringnullsToBlanks(java.lang.String s)
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 null

        return nullsToString(s, "");
    
protected java.lang.StringnullsToString(java.lang.String couldBeNull, java.lang.String subForNulls)
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 null

        return (couldBeNull == null ? subForNulls : couldBeNull);
    
protected java.lang.StringsetCommand(java.lang.String command)
Sets the desired command string

param
String command as desired
return
command string

        return command;
    
public java.util.HashtablesetEnvironment(java.util.Hashtable env)
Sets process' environment

param
process' environment
return
Hashtable to which the process' environment was set

        this.env = env;
        return this.env;
    
protected voidsetupFromContext(javax.servlet.ServletContext context)
Uses the ServletContext to set some process variables

param
context ServletContext for information provided by the Servlet API

        this.context = context;
        this.webAppRootDir = context.getRealPath("/");
    
protected voidsetupFromRequest(javax.servlet.http.HttpServletRequest req)
Uses the HttpServletRequest to set most process variables

param
req HttpServletRequest for information provided by the Servlet API

        this.contextPath = req.getContextPath();
        this.pathInfo = req.getPathInfo();
        this.servletPath = req.getServletPath();
    
public java.lang.StringtoString()
Print important process environment information in an easy-to-read HTML table

return
HTML string containing process environment info

        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();