1 /*
2 * Copyright 1999,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.catalina.util;
18
19 import java.io.File;
20 import java.util.Enumeration;
21 import java.util.Hashtable;
22
23 import javax.servlet.ServletContext;
24 import javax.servlet.http.HttpServletRequest;
25
26
27
28 /**
29 * Encapsulates the Process environment and rules to derive
30 * that environment from the servlet container and request information.
31 * @author Martin Dengler [root@martindengler.com]
32 * @version $Revision: 303236 $, $Date: 2004-09-16 19:19:54 -0400 (Thu, 16 Sep 2004) $
33 * @since Tomcat 4.0
34 */
35 public class ProcessEnvironment {
36
37 private static org.apache.commons.logging.Log log=
38 org.apache.commons.logging.LogFactory.getLog( ProcessEnvironment.class );
39
40 /** context of the enclosing servlet */
41 private ServletContext context = null;
42
43 /** real file system directory of the enclosing servlet's web app */
44 private String webAppRootDir = null;
45
46 /** context path of enclosing servlet */
47 private String contextPath = null;
48
49 /** pathInfo for the current request */
50 protected String pathInfo = null;
51
52 /** servlet URI of the enclosing servlet */
53 private String servletPath = null;
54
55 /** derived process environment */
56 protected Hashtable env = null;
57
58 /** command to be invoked */
59 protected String command = null;
60
61 /** whether or not this object is valid or not */
62 protected boolean valid = false;
63
64 /** the debugging detail level for this instance. */
65 protected int debug = 0;
66
67 /** process' desired working directory */
68 protected File workingDirectory = null;
69
70
71 /**
72 * Creates a ProcessEnvironment and derives the necessary environment,
73 * working directory, command, etc.
74 * @param req HttpServletRequest for information provided by
75 * the Servlet API
76 * @param context ServletContext for information provided by
77 * the Servlet API
78 */
79 public ProcessEnvironment(HttpServletRequest req,
80 ServletContext context) {
81 this(req, context, 0);
82 }
83
84
85 /**
86 * Creates a ProcessEnvironment and derives the necessary environment,
87 * working directory, command, etc.
88 * @param req HttpServletRequest for information provided by
89 * the Servlet API
90 * @param context ServletContext for information provided by
91 * the Servlet API
92 * @param debug int debug level (0 == none, 4 == medium, 6 == lots)
93 */
94 public ProcessEnvironment(HttpServletRequest req,
95 ServletContext context, int debug) {
96 this.debug = debug;
97 setupFromContext(context);
98 setupFromRequest(req);
99 this.valid = deriveProcessEnvironment(req);
100 if (log.isDebugEnabled())
101 log.debug(this.getClass().getName() + "() ctor, debug level " +
102 debug);
103 }
104
105
106 /**
107 * Uses the ServletContext to set some process variables
108 * @param context ServletContext for information provided by
109 * the Servlet API
110 */
111 protected void setupFromContext(ServletContext context) {
112 this.context = context;
113 this.webAppRootDir = context.getRealPath("/");
114 }
115
116
117 /**
118 * Uses the HttpServletRequest to set most process variables
119 * @param req HttpServletRequest for information provided by
120 * the Servlet API
121 */
122 protected void setupFromRequest(HttpServletRequest req) {
123 this.contextPath = req.getContextPath();
124 this.pathInfo = req.getPathInfo();
125 this.servletPath = req.getServletPath();
126 }
127
128
129 /**
130 * Print important process environment information in an
131 * easy-to-read HTML table
132 * @return HTML string containing process environment info
133 */
134 public String toString() {
135 StringBuffer sb = new StringBuffer();
136 sb.append("<TABLE border=2>");
137 sb.append("<tr><th colspan=2 bgcolor=grey>");
138 sb.append("ProcessEnvironment Info</th></tr>");
139 sb.append("<tr><td>Debug Level</td><td>");
140 sb.append(debug);
141 sb.append("</td></tr>");
142 sb.append("<tr><td>Validity:</td><td>");
143 sb.append(isValid());
144 sb.append("</td></tr>");
145 if (isValid()) {
146 Enumeration envk = env.keys();
147 while (envk.hasMoreElements()) {
148 String s = (String)envk.nextElement();
149 sb.append("<tr><td>");
150 sb.append(s);
151 sb.append("</td><td>");
152 sb.append(blanksToString((String)env.get(s),
153 "[will be set to blank]"));
154 sb.append("</td></tr>");
155 }
156 }
157 sb.append("<tr><td colspan=2><HR></td></tr>");
158 sb.append("<tr><td>Derived Command</td><td>");
159 sb.append(nullsToBlanks(command));
160 sb.append("</td></tr>");
161 sb.append("<tr><td>Working Directory</td><td>");
162 if (workingDirectory != null) {
163 sb.append(workingDirectory.toString());
164 }
165 sb.append("</td></tr>");
166 sb.append("</TABLE><p>end.");
167 return sb.toString();
168 }
169
170
171 /**
172 * Gets derived command string
173 * @return command string
174 */
175 public String getCommand() {
176 return command;
177 }
178
179
180 /**
181 * Sets the desired command string
182 * @param command String command as desired
183 * @return command string
184 */
185 protected String setCommand(String command) {
186 return command;
187 }
188
189
190 /**
191 * Gets this process' derived working directory
192 * @return working directory
193 */
194 public File getWorkingDirectory() {
195 return workingDirectory;
196 }
197
198
199 /**
200 * Gets process' environment
201 * @return process' environment
202 */
203 public Hashtable getEnvironment() {
204 return env;
205 }
206
207
208 /**
209 * Sets process' environment
210 * @param env process' environment
211 * @return Hashtable to which the process' environment was set
212 */
213 public Hashtable setEnvironment(Hashtable env) {
214 this.env = env;
215 return this.env;
216 }
217
218
219 /**
220 * Gets validity status
221 * @return true if this environment is valid, false otherwise
222 */
223 public boolean isValid() {
224 return valid;
225 }
226
227
228 /**
229 * Converts null strings to blank strings ("")
230 * @param s string to be converted if necessary
231 * @return a non-null string, either the original or the empty string
232 * ("") if the original was <code>null</code>
233 */
234 protected String nullsToBlanks(String s) {
235 return nullsToString(s, "");
236 }
237
238
239 /**
240 * Converts null strings to another string
241 * @param couldBeNull string to be converted if necessary
242 * @param subForNulls string to return instead of a null string
243 * @return a non-null string, either the original or the substitute
244 * string if the original was <code>null</code>
245 */
246 protected String nullsToString(String couldBeNull, String subForNulls) {
247 return (couldBeNull == null ? subForNulls : couldBeNull);
248 }
249
250
251 /**
252 * Converts blank strings to another string
253 * @param couldBeBlank string to be converted if necessary
254 * @param subForBlanks string to return instead of a blank string
255 * @return a non-null string, either the original or the substitute
256 * string if the original was <code>null</code> or empty ("")
257 */
258 protected String blanksToString(String couldBeBlank,
259 String subForBlanks) {
260 return (("".equals(couldBeBlank) || couldBeBlank == null) ?
261 subForBlanks : couldBeBlank);
262 }
263
264
265 /**
266 * Constructs the Process environment to be supplied to the invoked
267 * process. Defines an environment no environment variables.
268 * <p>
269 * Should be overriden by subclasses to perform useful setup.
270 * </p>
271 *
272 * @param req request associated with the
273 * Process' invocation
274 * @return true if environment was set OK, false if there was a problem
275 * and no environment was set
276 */
277 protected boolean deriveProcessEnvironment(HttpServletRequest req) {
278
279 Hashtable envp = new Hashtable();
280 command = getCommand();
281 if (command != null) {
282 workingDirectory = new
283 File(command.substring(0,
284 command.lastIndexOf(File.separator)));
285 envp.put("X_TOMCAT_COMMAND_PATH", command); //for kicks
286 }
287 this.env = envp;
288 return true;
289 }
290
291
292 /**
293 * Gets the root directory of the web application to which this process\
294 * belongs
295 * @return root directory
296 */
297 public String getWebAppRootDir() {
298 return webAppRootDir;
299 }
300
301
302 public String getContextPath(){
303 return contextPath;
304 }
305
306
307 public ServletContext getContext(){
308 return context;
309 }
310
311
312 public String getServletPath(){
313 return servletPath;
314 }
315 }