001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.util;
018:
019: import java.io.File;
020: import java.util.Enumeration;
021: import java.util.Hashtable;
022:
023: import javax.servlet.ServletContext;
024: import javax.servlet.http.HttpServletRequest;
025:
026: /**
027: * Encapsulates the Process environment and rules to derive
028: * that environment from the servlet container and request information.
029: * @author Martin Dengler [root@martindengler.com]
030: * @version $Revision: 1.4 $, $Date: 2004/05/26 16:21:04 $
031: * @since Tomcat 4.0
032: */
033: public class ProcessEnvironment {
034: /** context of the enclosing servlet */
035: private ServletContext context = null;
036:
037: /** real file system directory of the enclosing servlet's web app */
038: private String webAppRootDir = null;
039:
040: /** context path of enclosing servlet */
041: private String contextPath = null;
042:
043: /** pathInfo for the current request */
044: protected String pathInfo = null;
045:
046: /** servlet URI of the enclosing servlet */
047: private String servletPath = null;
048:
049: /** derived process environment */
050: protected Hashtable env = null;
051:
052: /** command to be invoked */
053: protected String command = null;
054:
055: /** whether or not this object is valid or not */
056: protected boolean valid = false;
057:
058: /** the debugging detail level for this instance. */
059: protected int debug = 0;
060:
061: /** process' desired working directory */
062: protected File workingDirectory = null;
063:
064: /**
065: * Creates a ProcessEnvironment and derives the necessary environment,
066: * working directory, command, etc.
067: * @param req HttpServletRequest for information provided by
068: * the Servlet API
069: * @param context ServletContext for information provided by
070: * the Servlet API
071: */
072: public ProcessEnvironment(HttpServletRequest req,
073: ServletContext context) {
074: this (req, context, 0);
075: }
076:
077: /**
078: * Creates a ProcessEnvironment and derives the necessary environment,
079: * working directory, command, etc.
080: * @param req HttpServletRequest for information provided by
081: * the Servlet API
082: * @param context ServletContext for information provided by
083: * the Servlet API
084: * @param debug int debug level (0 == none, 4 == medium, 6 == lots)
085: */
086: public ProcessEnvironment(HttpServletRequest req,
087: ServletContext context, int debug) {
088: this .debug = debug;
089: setupFromContext(context);
090: setupFromRequest(req);
091: this .valid = deriveProcessEnvironment(req);
092: log(this .getClass().getName() + "() ctor, debug level " + debug);
093: }
094:
095: /**
096: * Uses the ServletContext to set some process variables
097: * @param context ServletContext for information provided by
098: * the Servlet API
099: */
100: protected void setupFromContext(ServletContext context) {
101: this .context = context;
102: this .webAppRootDir = context.getRealPath("/");
103: }
104:
105: /**
106: * Uses the HttpServletRequest to set most process variables
107: * @param req HttpServletRequest for information provided by
108: * the Servlet API
109: */
110: protected void setupFromRequest(HttpServletRequest req) {
111: this .contextPath = req.getContextPath();
112: this .pathInfo = req.getPathInfo();
113: this .servletPath = req.getServletPath();
114: }
115:
116: /**
117: * Print important process environment information in an
118: * easy-to-read HTML table
119: * @return HTML string containing process environment info
120: */
121: public String toString() {
122: StringBuffer sb = new StringBuffer();
123: sb.append("<TABLE border=2>");
124: sb.append("<tr><th colspan=2 bgcolor=grey>");
125: sb.append("ProcessEnvironment Info</th></tr>");
126: sb.append("<tr><td>Debug Level</td><td>");
127: sb.append(debug);
128: sb.append("</td></tr>");
129: sb.append("<tr><td>Validity:</td><td>");
130: sb.append(isValid());
131: sb.append("</td></tr>");
132: if (isValid()) {
133: Enumeration envk = env.keys();
134: while (envk.hasMoreElements()) {
135: String s = (String) envk.nextElement();
136: sb.append("<tr><td>");
137: sb.append(s);
138: sb.append("</td><td>");
139: sb.append(blanksToString((String) env.get(s),
140: "[will be set to blank]"));
141: sb.append("</td></tr>");
142: }
143: }
144: sb.append("<tr><td colspan=2><HR></td></tr>");
145: sb.append("<tr><td>Derived Command</td><td>");
146: sb.append(nullsToBlanks(command));
147: sb.append("</td></tr>");
148: sb.append("<tr><td>Working Directory</td><td>");
149: if (workingDirectory != null) {
150: sb.append(workingDirectory.toString());
151: }
152: sb.append("</td></tr>");
153: sb.append("</TABLE><p>end.");
154: return sb.toString();
155: }
156:
157: /**
158: * Gets derived command string
159: * @return command string
160: */
161: public String getCommand() {
162: return command;
163: }
164:
165: /**
166: * Sets the desired command string
167: * @param command String command as desired
168: * @return command string
169: */
170: protected String setCommand(String command) {
171: return command;
172: }
173:
174: /**
175: * Gets this process' derived working directory
176: * @return working directory
177: */
178: public File getWorkingDirectory() {
179: return workingDirectory;
180: }
181:
182: /**
183: * Gets process' environment
184: * @return process' environment
185: */
186: public Hashtable getEnvironment() {
187: return env;
188: }
189:
190: /**
191: * Sets process' environment
192: * @param env process' environment
193: * @return Hashtable to which the process' environment was set
194: */
195: public Hashtable setEnvironment(Hashtable env) {
196: this .env = env;
197: return this .env;
198: }
199:
200: /**
201: * Gets validity status
202: * @return true if this environment is valid, false otherwise
203: */
204: public boolean isValid() {
205: return valid;
206: }
207:
208: /**
209: * Converts null strings to blank strings ("")
210: * @param s string to be converted if necessary
211: * @return a non-null string, either the original or the empty string
212: * ("") if the original was <code>null</code>
213: */
214: protected String nullsToBlanks(String s) {
215: return nullsToString(s, "");
216: }
217:
218: /**
219: * Converts null strings to another string
220: * @param couldBeNull string to be converted if necessary
221: * @param subForNulls string to return instead of a null string
222: * @return a non-null string, either the original or the substitute
223: * string if the original was <code>null</code>
224: */
225: protected String nullsToString(String couldBeNull,
226: String subForNulls) {
227: return (couldBeNull == null ? subForNulls : couldBeNull);
228: }
229:
230: /**
231: * Converts blank strings to another string
232: * @param couldBeBlank string to be converted if necessary
233: * @param subForBlanks string to return instead of a blank string
234: * @return a non-null string, either the original or the substitute
235: * string if the original was <code>null</code> or empty ("")
236: */
237: protected String blanksToString(String couldBeBlank,
238: String subForBlanks) {
239: return (("".equals(couldBeBlank) || couldBeBlank == null) ? subForBlanks
240: : couldBeBlank);
241: }
242:
243: protected void log(String s) {
244: System.out.println(s);
245: }
246:
247: /**
248: * Constructs the Process environment to be supplied to the invoked
249: * process. Defines an environment no environment variables.
250: * <p>
251: * Should be overriden by subclasses to perform useful setup.
252: * </p>
253: *
254: * @param req request associated with the
255: * Process' invocation
256: * @return true if environment was set OK, false if there was a problem
257: * and no environment was set
258: */
259: protected boolean deriveProcessEnvironment(HttpServletRequest req) {
260:
261: Hashtable envp = new Hashtable();
262: command = getCommand();
263: if (command != null) {
264: workingDirectory = new File(command.substring(0, command
265: .lastIndexOf(File.separator)));
266: envp.put("X_TOMCAT_COMMAND_PATH", command); //for kicks
267: }
268: this .env = envp;
269: return true;
270: }
271:
272: /**
273: * Gets the root directory of the web application to which this process\
274: * belongs
275: * @return root directory
276: */
277: public String getWebAppRootDir() {
278: return webAppRootDir;
279: }
280:
281: public String getContextPath() {
282: return contextPath;
283: }
284:
285: public ServletContext getContext() {
286: return context;
287: }
288:
289: public String getServletPath() {
290: return servletPath;
291: }
292: }
|