001: /*
002: * ProcessEnvironment.java $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/util/ProcessEnvironment.java,v 1.2 2001/07/22 20:25:13 pier Exp $
003: * $Revision: 1.2 $, $Date: 2001/07/22 20:25:13 $
004: *
005: * ====================================================================
006: *
007: * The Apache Software License, Version 1.1
008: *
009: * Copyright (c) 1999 The Apache Software Foundation. All rights
010: * reserved.
011: *
012: * Redistribution and use in source and binary forms, with or without
013: * modification, are permitted provided that the following conditions
014: * are met:
015: *
016: * 1. Redistributions of source code must retain the above copyright
017: * notice, this list of conditions and the following disclaimer.
018: *
019: * 2. Redistributions in binary form must reproduce the above copyright
020: * notice, this list of conditions and the following disclaimer in
021: * the documentation and/or other materials provided with the
022: * distribution.
023: *
024: * 3. The end-user documentation included with the redistribution, if
025: * any, must include the following acknowlegement:
026: * "This product includes software developed by the
027: * Apache Software Foundation (http://www.apache.org/)."
028: * Alternately, this acknowlegement may appear in the software itself,
029: * if and wherever such third-party acknowlegements normally appear.
030: *
031: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
032: * Foundation" must not be used to endorse or promote products derived
033: * from this software without prior written permission. For written
034: * permission, please contact apache@apache.org.
035: *
036: * 5. Products derived from this software may not be called "Apache"
037: * nor may "Apache" appear in their names without prior written
038: * permission of the Apache Group.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
044: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
045: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
046: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Apache Software Foundation. For more
056: * information on the Apache Software Foundation, please see
057: * <http://www.apache.org/>.
058: *
059: *
060: */
061:
062: package org.apache.catalina.util;
063:
064: import java.lang.Process;
065: import java.io.File;
066: import java.io.Writer;
067: import java.io.Reader;
068: import java.io.PrintWriter;
069: import java.io.BufferedWriter;
070: import java.io.BufferedReader;
071: import java.io.InputStream;
072: import java.io.OutputStream;
073: import java.io.InputStreamReader;
074: import java.io.OutputStreamWriter;
075: import java.io.BufferedInputStream;
076: import java.io.BufferedOutputStream;
077: import java.io.IOException;
078: import java.net.URLEncoder;
079: import java.util.Hashtable;
080: import java.util.Vector;
081: import java.util.Enumeration;
082: import java.util.StringTokenizer;
083: import java.util.Locale;
084: import java.util.Date;
085: import javax.servlet.ServletException;
086: import javax.servlet.ServletOutputStream;
087: import javax.servlet.ServletContext;
088: import javax.servlet.ServletConfig;
089: import javax.servlet.http.HttpServlet;
090: import javax.servlet.http.HttpServletRequest;
091: import javax.servlet.http.HttpServletResponse;
092: import javax.servlet.http.HttpSession;
093: import javax.servlet.http.Cookie;
094: import org.apache.catalina.Context;
095: import org.apache.catalina.Wrapper;
096:
097: // import org.apache.catalina.util.StringManager;
098:
099: /**
100: * Encapsulates the Process environment and rules to derive
101: * that environment from the servlet container and request information.
102: * @author Martin Dengler [root@martindengler.com]
103: * @version $Revision: 1.2 $, $Date: 2001/07/22 20:25:13 $
104: * @since Tomcat 4.0
105: */
106: public class ProcessEnvironment {
107: /** context of the enclosing servlet */
108: private ServletContext context = null;
109:
110: /** real file system directory of the enclosing servlet's web app */
111: private String webAppRootDir = null;
112:
113: /** context path of enclosing servlet */
114: private String contextPath = null;
115:
116: /** pathInfo for the current request */
117: protected String pathInfo = null;
118:
119: /** servlet URI of the enclosing servlet */
120: private String servletPath = null;
121:
122: /** derived process environment */
123: protected Hashtable env = null;
124:
125: /** command to be invoked */
126: protected String command = null;
127:
128: /** whether or not this object is valid or not */
129: protected boolean valid = false;
130:
131: /** the debugging detail level for this instance. */
132: protected int debug = 0;
133:
134: /** process' desired working directory */
135: protected File workingDirectory = null;
136:
137: /**
138: * Creates a ProcessEnvironment and derives the necessary environment,
139: * working directory, command, etc.
140: * @param req HttpServletRequest for information provided by
141: * the Servlet API
142: * @param context ServletContext for information provided by
143: * the Servlet API
144: */
145: public ProcessEnvironment(HttpServletRequest req,
146: ServletContext context) {
147: this (req, context, 0);
148: }
149:
150: /**
151: * Creates a ProcessEnvironment and derives the necessary environment,
152: * working directory, command, etc.
153: * @param req HttpServletRequest for information provided by
154: * the Servlet API
155: * @param context ServletContext for information provided by
156: * the Servlet API
157: * @param debug int debug level (0 == none, 4 == medium, 6 == lots)
158: */
159: public ProcessEnvironment(HttpServletRequest req,
160: ServletContext context, int debug) {
161: this .debug = debug;
162: setupFromContext(context);
163: setupFromRequest(req);
164: this .valid = deriveProcessEnvironment(req);
165: log(this .getClass().getName() + "() ctor, debug level " + debug);
166: }
167:
168: /**
169: * Uses the ServletContext to set some process variables
170: * @param context ServletContext for information provided by
171: * the Servlet API
172: */
173: protected void setupFromContext(ServletContext context) {
174: this .context = context;
175: this .webAppRootDir = context.getRealPath("/");
176: }
177:
178: /**
179: * Uses the HttpServletRequest to set most process variables
180: * @param req HttpServletRequest for information provided by
181: * the Servlet API
182: */
183: protected void setupFromRequest(HttpServletRequest req) {
184: this .contextPath = req.getContextPath();
185: this .pathInfo = req.getPathInfo();
186: this .servletPath = req.getServletPath();
187: }
188:
189: /**
190: * Print important process environment information in an
191: * easy-to-read HTML table
192: * @return HTML string containing process environment info
193: */
194: public String toString() {
195: StringBuffer sb = new StringBuffer();
196: sb.append("<TABLE border=2>");
197: sb.append("<tr><th colspan=2 bgcolor=grey>");
198: sb.append("ProcessEnvironment Info</th></tr>");
199: sb.append("<tr><td>Debug Level</td><td>");
200: sb.append(debug);
201: sb.append("</td></tr>");
202: sb.append("<tr><td>Validity:</td><td>");
203: sb.append(isValid());
204: sb.append("</td></tr>");
205: if (isValid()) {
206: Enumeration envk = env.keys();
207: while (envk.hasMoreElements()) {
208: String s = (String) envk.nextElement();
209: sb.append("<tr><td>");
210: sb.append(s);
211: sb.append("</td><td>");
212: sb.append(blanksToString((String) env.get(s),
213: "[will be set to blank]"));
214: sb.append("</td></tr>");
215: }
216: }
217: sb.append("<tr><td colspan=2><HR></td></tr>");
218: sb.append("<tr><td>Derived Command</td><td>");
219: sb.append(nullsToBlanks(command));
220: sb.append("</td></tr>");
221: sb.append("<tr><td>Working Directory</td><td>");
222: if (workingDirectory != null) {
223: sb.append(workingDirectory.toString());
224: }
225: sb.append("</td></tr>");
226: sb.append("</TABLE><p>end.");
227: return sb.toString();
228: }
229:
230: /**
231: * Gets derived command string
232: * @return command string
233: */
234: public String getCommand() {
235: return command;
236: }
237:
238: /**
239: * Sets the desired command string
240: * @param String command as desired
241: * @return command string
242: */
243: protected String setCommand(String command) {
244: return command;
245: }
246:
247: /**
248: * Gets this process' derived working directory
249: * @return working directory
250: */
251: public File getWorkingDirectory() {
252: return workingDirectory;
253: }
254:
255: /**
256: * Gets process' environment
257: * @return process' environment
258: */
259: public Hashtable getEnvironment() {
260: return env;
261: }
262:
263: /**
264: * Sets process' environment
265: * @param process' environment
266: * @return Hashtable to which the process' environment was set
267: */
268: public Hashtable setEnvironment(Hashtable env) {
269: this .env = env;
270: return this .env;
271: }
272:
273: /**
274: * Gets validity status
275: * @return true if this environment is valid, false otherwise
276: */
277: public boolean isValid() {
278: return valid;
279: }
280:
281: /**
282: * Converts null strings to blank strings ("")
283: * @param string to be converted if necessary
284: * @return a non-null string, either the original or the empty string
285: * ("") if the original was <code>null</code>
286: */
287: protected String nullsToBlanks(String s) {
288: return nullsToString(s, "");
289: }
290:
291: /**
292: * Converts null strings to another string
293: * @param string to be converted if necessary
294: * @param string to return instead of a null string
295: * @return a non-null string, either the original or the substitute
296: * string if the original was <code>null</code>
297: */
298: protected String nullsToString(String couldBeNull,
299: String subForNulls) {
300: return (couldBeNull == null ? subForNulls : couldBeNull);
301: }
302:
303: /**
304: * Converts blank strings to another string
305: * @param string to be converted if necessary
306: * @param string to return instead of a blank string
307: * @return a non-null string, either the original or the substitute
308: * string if the original was <code>null</code> or empty ("")
309: */
310: protected String blanksToString(String couldBeBlank,
311: String subForBlanks) {
312: return (("".equals(couldBeBlank) || couldBeBlank == null) ? subForBlanks
313: : couldBeBlank);
314: }
315:
316: protected void log(String s) {
317: System.out.println(s);
318: }
319:
320: /**
321: * Constructs the Process environment to be supplied to the invoked
322: * process. Defines an environment no environment variables.
323: * <p>
324: * Should be overriden by subclasses to perform useful setup.
325: * </p>
326: *
327: * @param HttpServletRequest request associated with the
328: * Process' invocation
329: * @return true if environment was set OK, false if there was a problem
330: * and no environment was set
331: */
332: protected boolean deriveProcessEnvironment(HttpServletRequest req) {
333:
334: Hashtable envp = new Hashtable();
335: command = getCommand();
336: if (command != null) {
337: workingDirectory = new File(command.substring(0, command
338: .lastIndexOf(File.separator)));
339: envp.put("X_TOMCAT_COMMAND_PATH", command); //for kicks
340: }
341: this .env = envp;
342: return true;
343: }
344:
345: /**
346: * Gets the root directory of the web application to which this process\
347: * belongs
348: * @return root directory
349: */
350: public String getWebAppRootDir() {
351: return webAppRootDir;
352: }
353:
354: public String getContextPath() {
355: return contextPath;
356: }
357:
358: public ServletContext getContext() {
359: return context;
360: }
361:
362: public String getServletPath() {
363: return servletPath;
364: }
365: }
|