KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > ProcessEnvironment


1
2
3 /*
4  * The contents of this file are subject to the terms
5  * of the Common Development and Distribution License
6  * (the "License"). You may not use this file except
7  * in compliance with the License.
8  *
9  * You can obtain a copy of the license at
10  * glassfish/bootstrap/legal/CDDLv1.0.txt or
11  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
12  * See the License for the specific language governing
13  * permissions and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL
16  * HEADER in each file and include the License file at
17  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
18  * add the following below this CDDL HEADER, with the
19  * fields enclosed by brackets "[]" replaced with your
20  * own identifying information: Portions Copyright [yyyy]
21  * [name of copyright owner]
22  *
23  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24  *
25  * Portions Copyright Apache Software Foundation.
26  */

27
28 package org.apache.catalina.util;
29
30 import java.lang.Process JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.Writer JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.io.BufferedWriter JavaDoc;
36 import java.io.BufferedReader JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39 import java.io.InputStreamReader JavaDoc;
40 import java.io.OutputStreamWriter JavaDoc;
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.BufferedOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.net.URLEncoder JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Vector JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.util.StringTokenizer JavaDoc;
49 import java.util.Locale JavaDoc;
50 import java.util.Date JavaDoc;
51 import javax.servlet.ServletException JavaDoc;
52 import javax.servlet.ServletOutputStream JavaDoc;
53 import javax.servlet.ServletContext JavaDoc;
54 import javax.servlet.ServletConfig JavaDoc;
55 import javax.servlet.http.HttpServlet JavaDoc;
56 import javax.servlet.http.HttpServletRequest JavaDoc;
57 import javax.servlet.http.HttpServletResponse JavaDoc;
58 import javax.servlet.http.HttpSession JavaDoc;
59 import javax.servlet.http.Cookie JavaDoc;
60 import org.apache.catalina.Context;
61 import org.apache.catalina.Wrapper;
62 // import org.apache.catalina.util.StringManager;
63

64
65
66 /**
67  * Encapsulates the Process environment and rules to derive
68  * that environment from the servlet container and request information.
69  * @author Martin Dengler [root@martindengler.com]
70  * @version $Revision: 1.3 $, $Date: 2006/03/12 01:27:08 $
71  * @since Tomcat 4.0
72  */

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 JavaDoc.class );
78
79     /** context of the enclosing servlet */
80     private ServletContext JavaDoc context = null;
81
82     /** real file system directory of the enclosing servlet's web app */
83     private String JavaDoc webAppRootDir = null;
84
85     /** context path of enclosing servlet */
86     private String JavaDoc contextPath = null;
87
88     /** pathInfo for the current request */
89     protected String JavaDoc pathInfo = null;
90
91     /** servlet URI of the enclosing servlet */
92     private String JavaDoc servletPath = null;
93
94     /** derived process environment */
95     protected Hashtable JavaDoc env = null;
96
97     /** command to be invoked */
98     protected String JavaDoc command = null;
99
100     /** whether or not this object is valid or not */
101     protected boolean valid = false;
102
103     /** the debugging detail level for this instance. */
104     protected int debug = 0;
105
106     /** process' desired working directory */
107     protected File JavaDoc workingDirectory = null;
108
109
110     /**
111      * Creates a ProcessEnvironment and derives the necessary environment,
112      * working directory, command, etc.
113      * @param req HttpServletRequest for information provided by
114      * the Servlet API
115      * @param context ServletContext for information provided by
116      * the Servlet API
117      */

118     public ProcessEnvironment(HttpServletRequest JavaDoc req,
119         ServletContext JavaDoc context) {
120         this(req, context, 0);
121     }
122
123
124     /**
125      * Creates a ProcessEnvironment and derives the necessary environment,
126      * working directory, command, etc.
127      * @param req HttpServletRequest for information provided by
128      * the Servlet API
129      * @param context ServletContext for information provided by
130      * the Servlet API
131      * @param debug int debug level (0 == none, 4 == medium, 6 == lots)
132      */

133     public ProcessEnvironment(HttpServletRequest JavaDoc req,
134         ServletContext JavaDoc 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     /**
144      * Uses the ServletContext to set some process variables
145      * @param context ServletContext for information provided by
146      * the Servlet API
147      */

148     protected void setupFromContext(ServletContext JavaDoc context) {
149         this.context = context;
150         this.webAppRootDir = context.getRealPath("/");
151     }
152
153
154     /**
155      * Uses the HttpServletRequest to set most process variables
156      * @param req HttpServletRequest for information provided by
157      * the Servlet API
158      */

159     protected void setupFromRequest(HttpServletRequest JavaDoc req) {
160         this.contextPath = req.getContextPath();
161         this.pathInfo = req.getPathInfo();
162         this.servletPath = req.getServletPath();
163     }
164
165
166     /**
167      * Print important process environment information in an
168      * easy-to-read HTML table
169      * @return HTML string containing process environment info
170      */

171     public String JavaDoc toString() {
172         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc envk = env.keys();
184             while (envk.hasMoreElements()) {
185                 String JavaDoc s = (String JavaDoc)envk.nextElement();
186                 sb.append("<tr><td>");
187                 sb.append(s);
188                 sb.append("</td><td>");
189                 sb.append(blanksToString((String JavaDoc)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     /**
209      * Gets derived command string
210      * @return command string
211      */

212     public String JavaDoc getCommand() {
213         return command;
214     }
215
216
217     /**
218      * Sets the desired command string
219      * @param String command as desired
220      * @return command string
221      */

222     protected String JavaDoc setCommand(String JavaDoc command) {
223         return command;
224     }
225
226
227     /**
228      * Gets this process' derived working directory
229      * @return working directory
230      */

231     public File JavaDoc getWorkingDirectory() {
232         return workingDirectory;
233     }
234
235
236     /**
237      * Gets process' environment
238      * @return process' environment
239      */

240     public Hashtable JavaDoc getEnvironment() {
241         return env;
242     }
243
244
245     /**
246      * Sets process' environment
247      * @param process' environment
248      * @return Hashtable to which the process' environment was set
249      */

250     public Hashtable JavaDoc setEnvironment(Hashtable JavaDoc env) {
251         this.env = env;
252         return this.env;
253     }
254
255
256     /**
257      * Gets validity status
258      * @return true if this environment is valid, false otherwise
259      */

260     public boolean isValid() {
261         return valid;
262     }
263
264
265     /**
266      * Converts null strings to blank strings ("")
267      * @param string to be converted if necessary
268      * @return a non-null string, either the original or the empty string
269      * ("") if the original was <code>null</code>
270      */

271     protected String JavaDoc nullsToBlanks(String JavaDoc s) {
272         return nullsToString(s, "");
273     }
274
275
276     /**
277      * Converts null strings to another string
278      * @param string to be converted if necessary
279      * @param string to return instead of a null string
280      * @return a non-null string, either the original or the substitute
281      * string if the original was <code>null</code>
282      */

283     protected String JavaDoc nullsToString(String JavaDoc couldBeNull, String JavaDoc subForNulls) {
284         return (couldBeNull == null ? subForNulls : couldBeNull);
285     }
286
287
288     /**
289      * Converts blank strings to another string
290      * @param string to be converted if necessary
291      * @param string to return instead of a blank string
292      * @return a non-null string, either the original or the substitute
293      * string if the original was <code>null</code> or empty ("")
294      */

295     protected String JavaDoc blanksToString(String JavaDoc couldBeBlank,
296         String JavaDoc subForBlanks) {
297             return (("".equals(couldBeBlank) || couldBeBlank == null) ?
298                 subForBlanks : couldBeBlank);
299     }
300
301
302     protected void log(String JavaDoc s) {
303         if (log.isDebugEnabled())
304             log.debug(s);
305     }
306
307
308     /**
309      * Constructs the Process environment to be supplied to the invoked
310      * process. Defines an environment no environment variables.
311      * <p>
312      * Should be overriden by subclasses to perform useful setup.
313      * </p>
314      *
315      * @param HttpServletRequest request associated with the
316      * Process' invocation
317      * @return true if environment was set OK, false if there was a problem
318      * and no environment was set
319      */

320     protected boolean deriveProcessEnvironment(HttpServletRequest JavaDoc req) {
321
322         Hashtable JavaDoc envp = new Hashtable JavaDoc();
323         command = getCommand();
324         if (command != null) {
325             workingDirectory = new
326                 File JavaDoc(command.substring(0,
327                 command.lastIndexOf(File.separator)));
328                 envp.put("X_TOMCAT_COMMAND_PATH", command); //for kicks
329
}
330         this.env = envp;
331         return true;
332     }
333
334
335     /**
336      * Gets the root directory of the web application to which this process\
337      * belongs
338      * @return root directory
339      */

340     public String JavaDoc getWebAppRootDir() {
341         return webAppRootDir;
342     }
343
344
345     public String JavaDoc getContextPath(){
346             return contextPath;
347         }
348
349
350     public ServletContext JavaDoc getContext(){
351             return context;
352         }
353
354
355     public String JavaDoc getServletPath(){
356             return servletPath;
357         }
358 }
359
Popular Tags