001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/startup/Tool.java,v 1.5 2002/04/01 19:51:31 patrickl Exp $
003: * $Revision: 1.5 $
004: * $Date: 2002/04/01 19:51:31 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.catalina.startup;
065:
066: import java.io.File;
067: import java.io.IOException;
068: import java.lang.reflect.Method;
069: import java.net.MalformedURLException;
070: import java.net.URL;
071: import java.util.ArrayList;
072: import org.apache.catalina.loader.Extension;
073: import org.apache.catalina.loader.StandardClassLoader;
074:
075: /**
076: * <p>General purpose wrapper for command line tools that should execute in an
077: * environment with the common class loader environment set up by Catalina.
078: * This should be executed from a command line script that conforms to
079: * the following requirements:</p>
080: * <ul>
081: * <li>Passes the <code>catalina.home</code> system property configured with
082: * the pathname of the Tomcat installation directory.</li>
083: * <li>Sets the system classpath to include <code>bootstrap.jar</code> and
084: * <code>$JAVA_HOME/lib/tools.jar</code>.</li>
085: * </ul>
086: *
087: * <p>The command line to execute the tool looks like:</p>
088: * <pre>
089: * java -classpath $CLASSPATH org.apache.catalina.startup.Tool \
090: * ${options} ${classname} ${arguments}
091: * </pre>
092: *
093: * <p>with the following replacement contents:
094: * <ul>
095: * <li><strong>${options}</strong> - Command line options for this Tool wrapper.
096: * The following options are supported:
097: * <ul>
098: * <li><em>-ant</em> : Set the <code>ant.home</code> system property
099: * to corresponding to the value of <code>catalina.home</code>
100: * (useful when your command line tool runs Ant).</li>
101: * <li><em>-common</em> : Add <code>common/classes</code> and
102: * <code>common/lib</codE) to the class loader repositories.</li>
103: * <li><em>-debug</em> : Enable debugging messages from this wrapper.</li>
104: * <li><em>-server</em> : Add <code>server/classes</code> and
105: * <code>server/lib</code> to the class loader repositories.</li>
106: * <li><em>-shared</em> : Add <code>shared/classes</code> and
107: * <code>shared/lib</code> to the class loader repositories.</li>
108: * </ul>
109: * <li><strong>${classname}</strong> - Fully qualified Java class name of the
110: * application's main class.</li>
111: * <li><strong>${arguments}</strong> - Command line arguments to be passed to
112: * the application's <code>main()</code> method.</li>
113: * </ul>
114: *
115: * @author Craig R. McClanahan
116: * @version $Revision: 1.5 $ $Date: 2002/04/01 19:51:31 $
117: */
118:
119: public final class Tool {
120:
121: // ------------------------------------------------------- Static Variables
122:
123: /**
124: * Set <code>ant.home</code> system property?
125: */
126: private static boolean ant = false;
127:
128: /**
129: * The pathname of our installation base directory.
130: */
131: private static String catalinaHome = System
132: .getProperty("catalina.home");
133:
134: /**
135: * Include common classes in the repositories?
136: */
137: private static boolean common = false;
138:
139: /**
140: * Enable debugging detail messages?
141: */
142: private static boolean debug = false;
143:
144: /**
145: * Include server classes in the repositories?
146: */
147: private static boolean server = false;
148:
149: /**
150: * Include shared classes in the repositories?
151: */
152: private static boolean shared = false;
153:
154: // ----------------------------------------------------------- Main Program
155:
156: /**
157: * The main program for the bootstrap.
158: *
159: * @param args Command line arguments to be processed
160: */
161: public static void main(String args[]) {
162:
163: // Verify that "catalina.home" was passed.
164: if (catalinaHome == null) {
165: log("Must set 'catalina.home' system property");
166: System.exit(1);
167: }
168:
169: // Process command line options
170: int index = 0;
171: while (true) {
172: if (index == args.length) {
173: usage();
174: System.exit(1);
175: }
176: if ("-ant".equals(args[index]))
177: ant = true;
178: else if ("-common".equals(args[index]))
179: common = true;
180: else if ("-debug".equals(args[index]))
181: debug = true;
182: else if ("-server".equals(args[index]))
183: server = true;
184: else if ("-shared".equals(args[index]))
185: shared = true;
186: else
187: break;
188: index++;
189: }
190: if (index > args.length) {
191: usage();
192: System.exit(1);
193: }
194:
195: // Set "ant.home" if requested
196: if (ant)
197: System.setProperty("ant.home", catalinaHome);
198:
199: // Construct the class loader we will be using
200: ClassLoader classLoader = null;
201: try {
202: if (debug) {
203: log("Constructing class loader");
204: ClassLoaderFactory.setDebug(1);
205: }
206: ArrayList packed = new ArrayList();
207: ArrayList unpacked = new ArrayList();
208: unpacked.add(new File(catalinaHome, "classes"));
209: packed.add(new File(catalinaHome, "lib"));
210: if (common) {
211: unpacked.add(new File(catalinaHome, "common"
212: + File.separator + "classes"));
213: packed.add(new File(catalinaHome, "common"
214: + File.separator + "lib"));
215: }
216: if (server) {
217: unpacked.add(new File(catalinaHome, "server"
218: + File.separator + "classes"));
219: packed.add(new File(catalinaHome, "server"
220: + File.separator + "lib"));
221: }
222: if (shared) {
223: unpacked.add(new File(catalinaHome, "shared"
224: + File.separator + "classes"));
225: packed.add(new File(catalinaHome, "shared"
226: + File.separator + "lib"));
227: }
228: classLoader = ClassLoaderFactory.createClassLoader(
229: (File[]) unpacked.toArray(new File[0]),
230: (File[]) packed.toArray(new File[0]), null);
231: } catch (Throwable t) {
232: log("Class loader creation threw exception", t);
233: System.exit(1);
234: }
235: Thread.currentThread().setContextClassLoader(classLoader);
236:
237: // Load our application class
238: Class clazz = null;
239: String className = args[index++];
240: try {
241: if (debug)
242: log("Loading application class " + className);
243: clazz = classLoader.loadClass(className);
244: } catch (Throwable t) {
245: log("Exception creating instance of " + className, t);
246: System.exit(1);
247: }
248:
249: // Locate the static main() method of the application class
250: Method method = null;
251: String params[] = new String[args.length - index];
252: System.arraycopy(args, index, params, 0, params.length);
253: try {
254: if (debug)
255: log("Identifying main() method");
256: String methodName = "main";
257: Class paramTypes[] = new Class[1];
258: paramTypes[0] = params.getClass();
259: method = clazz.getMethod(methodName, paramTypes);
260: } catch (Throwable t) {
261: log("Exception locating main() method", t);
262: System.exit(1);
263: }
264:
265: // Invoke the main method of the application class
266: try {
267: if (debug)
268: log("Calling main() method");
269: Object paramValues[] = new Object[1];
270: paramValues[0] = params;
271: method.invoke(null, paramValues);
272: } catch (Throwable t) {
273: log("Exception calling main() method", t);
274: System.exit(1);
275: }
276:
277: }
278:
279: /**
280: * Log a debugging detail message.
281: *
282: * @param message The message to be logged
283: */
284: private static void log(String message) {
285:
286: System.out.print("Tool: ");
287: System.out.println(message);
288:
289: }
290:
291: /**
292: * Log a debugging detail message with an exception.
293: *
294: * @param message The message to be logged
295: * @param exception The exception to be logged
296: */
297: private static void log(String message, Throwable exception) {
298:
299: log(message);
300: exception.printStackTrace(System.out);
301:
302: }
303:
304: /**
305: * Display usage information about this tool.
306: */
307: private static void usage() {
308:
309: log("Usage: java org.apache.catalina.startup.Tool [<options>] <class> [<arguments>]");
310:
311: }
312:
313: }
|