1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.startup;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
/**
* <p>General purpose wrapper for command line tools that should execute in an
* environment with the common class loader environment set up by Catalina.
* This should be executed from a command line script that conforms to
* the following requirements:</p>
* <ul>
* <li>Passes the <code>catalina.home</code> system property configured with
* the pathname of the Tomcat installation directory.</li>
* <li>Sets the system classpath to include <code>bootstrap.jar</code> and
* <code>$JAVA_HOME/lib/tools.jar</code>.</li>
* </ul>
*
* <p>The command line to execute the tool looks like:</p>
* <pre>
* java -classpath $CLASSPATH org.apache.catalina.startup.Tool \
* ${options} ${classname} ${arguments}
* </pre>
*
* <p>with the following replacement contents:
* <ul>
* <li><strong>${options}</strong> - Command line options for this Tool wrapper.
* The following options are supported:
* <ul>
* <li><em>-ant</em> : Set the <code>ant.home</code> system property
* to corresponding to the value of <code>catalina.home</code>
* (useful when your command line tool runs Ant).</li>
* <li><em>-common</em> : Add <code>common/classes</code> and
* <code>common/lib</codE) to the class loader repositories.</li>
* <li><em>-server</em> : Add <code>server/classes</code> and
* <code>server/lib</code> to the class loader repositories.</li>
* <li><em>-shared</em> : Add <code>shared/classes</code> and
* <code>shared/lib</code> to the class loader repositories.</li>
* </ul>
* <li><strong>${classname}</strong> - Fully qualified Java class name of the
* application's main class.</li>
* <li><strong>${arguments}</strong> - Command line arguments to be passed to
* the application's <code>main()</code> method.</li>
* </ul>
*
* @author Craig R. McClanahan
*
*/
public final class Tool {
private static Log log = LogFactory.getLog(Tool.class);
// ------------------------------------------------------- Static Variables
/**
* Set <code>ant.home</code> system property?
*/
private static boolean ant = false;
/**
* The pathname of our installation base directory.
*/
private static String catalinaHome = System.getProperty("catalina.home");
/**
* Include common classes in the repositories?
*/
private static boolean common = false;
/**
* Include server classes in the repositories?
*/
private static boolean server = false;
/**
* Include shared classes in the repositories?
*/
private static boolean shared = false;
// ----------------------------------------------------------- Main Program
/**
* The main program for the bootstrap.
*
* @param args Command line arguments to be processed
*/
public static void main(String args[]) {
// Verify that "catalina.home" was passed.
if (catalinaHome == null) {
log.error("Must set 'catalina.home' system property");
System.exit(1);
}
// Process command line options
int index = 0;
while (true) {
if (index == args.length) {
usage();
System.exit(1);
}
if ("-ant".equals(args[index]))
ant = true;
else if ("-common".equals(args[index]))
common = true;
else if ("-server".equals(args[index]))
server = true;
else if ("-shared".equals(args[index]))
shared = true;
else
break;
index++;
}
if (index > args.length) {
usage();
System.exit(1);
}
// Set "ant.home" if requested
if (ant)
System.setProperty("ant.home", catalinaHome);
// Construct the class loader we will be using
ClassLoader classLoader = null;
try {
ArrayList packed = new ArrayList();
ArrayList unpacked = new ArrayList();
unpacked.add(new File(catalinaHome, "classes"));
packed.add(new File(catalinaHome, "lib"));
if (common) {
unpacked.add(new File(catalinaHome,
"common" + File.separator + "classes"));
packed.add(new File(catalinaHome,
"common" + File.separator + "lib"));
}
if (server) {
unpacked.add(new File(catalinaHome,
"server" + File.separator + "classes"));
packed.add(new File(catalinaHome,
"server" + File.separator + "lib"));
}
if (shared) {
unpacked.add(new File(catalinaHome,
"shared" + File.separator + "classes"));
packed.add(new File(catalinaHome,
"shared" + File.separator + "lib"));
}
classLoader =
ClassLoaderFactory.createClassLoader
((File[]) unpacked.toArray(new File[0]),
(File[]) packed.toArray(new File[0]),
null);
} catch (Throwable t) {
log.error("Class loader creation threw exception", t);
System.exit(1);
}
Thread.currentThread().setContextClassLoader(classLoader);
// Load our application class
Class clazz = null;
String className = args[index++];
try {
if (log.isDebugEnabled())
log.debug("Loading application class " + className);
clazz = classLoader.loadClass(className);
} catch (Throwable t) {
log.error("Exception creating instance of " + className, t);
System.exit(1);
}
// Locate the static main() method of the application class
Method method = null;
String params[] = new String[args.length - index];
System.arraycopy(args, index, params, 0, params.length);
try {
if (log.isDebugEnabled())
log.debug("Identifying main() method");
String methodName = "main";
Class paramTypes[] = new Class[1];
paramTypes[0] = params.getClass();
method = clazz.getMethod(methodName, paramTypes);
} catch (Throwable t) {
log.error("Exception locating main() method", t);
System.exit(1);
}
// Invoke the main method of the application class
try {
if (log.isDebugEnabled())
log.debug("Calling main() method");
Object paramValues[] = new Object[1];
paramValues[0] = params;
method.invoke(null, paramValues);
} catch (Throwable t) {
log.error("Exception calling main() method", t);
System.exit(1);
}
}
/**
* Display usage information about this tool.
*/
private static void usage() {
log.info("Usage: java org.apache.catalina.startup.Tool [<options>] <class> [<arguments>]");
}
}
|