001: package com.opensymphony.webwork;
002:
003: import java.io.File;
004: import java.io.FilenameFilter;
005: import java.lang.reflect.Method;
006: import java.net.MalformedURLException;
007: import java.net.URL;
008: import java.net.URLClassLoader;
009: import java.util.ArrayList;
010: import java.util.Collections;
011: import java.util.List;
012:
013: /**
014: * <!-- START SNIPPET: javadoc -->
015: *
016: * WebWork comes with various related tools included in the webwork jar file. You can access these tools by simply
017: * unpacking the WebWork distribution and running <b>java -jar webwork.jar</b>. WebWork will automatically include all
018: * jars in the same directory as the webwork.jar file as well as all jars in the <i>lib</i> directory. This means you
019: * can invoke these tools either from within the standard directory structure found in the WebWork distribution, or from
020: * within your WEB-INF/lib directory.
021: *
022: * <p/> You can access the help information for these tools by simply running the jar without any arguments.
023: *
024: * <!-- END SNIPPET: javadoc -->
025: *
026: * @author plightbo
027: * @author tmjee
028: * @version $Date: 2007-04-12 14:06:48 +0200 (Do, 12 Apr 2007) $ $Id: Main.java 2895 2007-04-12 12:06:48Z tm_jee $
029: */
030: public class Main {
031: public static void main(String[] args) {
032: if (args.length == 0) {
033: System.out.println("Usage:");
034: System.out
035: .println(" java -jar webwork.jar [command] (optional command args)");
036: System.out.println("");
037: System.out
038: .println("Where [command] is one of the following:");
039: System.out.println(" quickstart");
040: System.out.println(" quickstart:xxx");
041: System.out.println(" sitegraph");
042: System.out.println(" sitegraph:xxx");
043: System.out.println("");
044: System.out
045: .println("Execute the commands for additional usage instructions.");
046: System.out
047: .println("Note: the *:xxx commands are just shortcuts for ");
048: System.out
049: .println(" running the command on a webapp in the webapps dir.");
050: System.out
051: .println(" For example, 'quickstart:sandbox' will start QuickStart");
052: System.out
053: .println(" automatically for the webapp 'sandbox'.");
054: return;
055: }
056:
057: // check the JDK version
058: String version = System.getProperty("java.version");
059: boolean jdk14 = (version.indexOf("1.4") != -1);
060: boolean jdk13 = (version.indexOf("1.3") != -1);
061: boolean jdk12 = (version.indexOf("1.2") != -1);
062: boolean jdk11 = (version.indexOf("1.1") != -1);
063: boolean jdkOk = ((!jdk14) && (!jdk13) && (!jdk12) && (!jdk11));
064:
065: String javaHome = System.getProperty("java.home");
066: ArrayList urls = new ArrayList();
067: try {
068: findJars(new File("lib"), urls);
069:
070: // use all the jars in the current that aren't the src jar
071: File wd = new File(".");
072: File[] jars = wd.listFiles(new FilenameFilter() {
073: public boolean accept(File dir, String name) {
074: return name.endsWith(".jar")
075: && name.indexOf("-src.") == -1;
076: }
077: });
078: for (int i = 0; i < jars.length; i++) {
079: File jar = jars[i];
080: urls.add(jar.toURL());
081: }
082:
083: // ... but there might not be any (ie: we're in development in IDEA), so use this as backup
084: urls.add(new File(System.getProperty("webwork.classes",
085: "build/java")).toURL());
086: urls.add(new File(System.getProperty("xwork.classes",
087: "../xwork/build/java/")).toURL());
088:
089: // load tools.jar from JAVA_HOME
090: File tools = new File(javaHome, "lib/tools.jar");
091: if (!tools.exists()) {
092: // hmm, not there, how about java.home?
093: tools = new File(javaHome, "../lib/tools.jar");
094: }
095: if (!tools.exists()) {
096: // try the OS X common path
097: tools = new File(javaHome, "../Classes/classes.jar");
098: }
099: if (!tools.exists()) {
100: // try the other OS X common path
101: tools = new File(javaHome, "../Classes/classes.jar");
102: }
103: if (!tools.exists()) {
104: // did the user specify it by hand?
105: String prop = System.getProperty("tools");
106: if (prop != null) {
107: tools = new File(prop);
108: }
109: }
110: if (!tools.exists()) {
111: System.out
112: .println("Error: Could not find tools.jar! Please do one of the following: ");
113: System.out.println("");
114: System.out
115: .println(" - Use the JDK's JVM (ie: c:\\jdk1.5.0\\bin\\java)");
116: System.out
117: .println(" - Specify JAVA_HOME to point to your JDK 1.5 home");
118: System.out
119: .println(" - Specify a direct path to tools.jar via, as shown below:");
120: System.out.println("");
121: System.out
122: .println(" java -Dtools=/path/to/tools.jar -jar webwork-launcher.jar ...");
123: return;
124: }
125:
126: // finally, add the verified tools.jar
127: urls.add(tools.toURL());
128: } catch (MalformedURLException e) {
129: e.printStackTrace();
130: System.out
131: .println("Could not find URLs -- see stack trace.");
132: }
133:
134: String command = args[0];
135: String[] programArgs = new String[args.length - 1];
136: System.arraycopy(args, 1, programArgs, 0, programArgs.length);
137: if (command.startsWith("quickstart:")) {
138: command = "quickstart";
139: String name = checkWebAppArgs(args);
140: programArgs = new String[] { "/" + name,
141: "webapps/" + name + "/src/webapp",
142: "webapps/" + name + "/src/java" };
143: }
144:
145: if ("quickstart".equals(command)) {
146: if (!jdkOk) {
147: System.out
148: .println("Sorry, but QuickStart only runs on Java 1.5.");
149: System.out.println("You are running: " + version);
150: System.out
151: .println("Please try again with Java 1.5 or greater, or deploy");
152: System.out
153: .println(" as a normal J2EE webapp to use Java 1.4.");
154: return;
155: }
156:
157: launch("com.opensymphony.webwork.quickstart.QuickStart",
158: programArgs, urls);
159: return;
160: }
161:
162: if (command.startsWith("sitegraph:")) {
163: command = "sitegraph";
164: String name = checkWebAppArgs(args);
165: programArgs = new String[] { "-config",
166: "webapps/" + name + "/src/webapp/WEB-INF/classes",
167: "-views", "webapps/" + name + "/src/webapp",
168: "-output", "." };
169: }
170:
171: if ("sitegraph".equals(command)) {
172: launch("com.opensymphony.webwork.sitegraph.SiteGraph",
173: programArgs, urls);
174: }
175: }
176:
177: private static String checkWebAppArgs(String[] args) {
178: int colon = args[0].indexOf(':');
179: String name = null;
180: try {
181: name = args[0].substring(colon + 1);
182: } catch (Exception e) {
183: //this is OK to skip
184: }
185: if (name == null || name.equals("")) {
186: System.out
187: .println("Error: you must specify the webapp you wish");
188: System.out
189: .println(" to deploy. The webapp name must be the");
190: System.out
191: .println(" name of the directory found in webapps/.");
192: System.out.println("");
193: System.out
194: .println("Example: java -jar webwork.jar quickstart:sandbox");
195: System.exit(1);
196: }
197:
198: return name;
199: }
200:
201: private static void launch(String program, String[] programArgs,
202: List urls) {
203: Collections.reverse(urls);
204: URL[] urlArray = (URL[]) urls.toArray(new URL[urls.size()]);
205: URLClassLoader cl = new MainClassLoader(urlArray);
206: Thread.currentThread().setContextClassLoader(cl);
207: try {
208: Class clazz = cl.loadClass(program);
209: Method main = clazz.getDeclaredMethod("main",
210: new Class[] { String[].class });
211: main.invoke(null, new Object[] { programArgs });
212: } catch (Exception e) {
213: e.printStackTrace();
214: }
215: }
216:
217: private static void findJars(File file, ArrayList urls)
218: throws MalformedURLException {
219: File[] files = file.listFiles();
220: if (files == null) {
221: return;
222: }
223:
224: for (int i = 0; i < files.length; i++) {
225: File f = files[i];
226: if (f.isDirectory()) {
227: findJars(f, urls);
228: } else if (f.getName().endsWith(".jar")) {
229: if (isValid(f.getName())) {
230: urls.add(f.toURL());
231: }
232: }
233: }
234: }
235:
236: private static boolean isValid(String name) {
237: return !"dom.jar".equals(name);
238: }
239:
240: /**
241: * Reverses the typical order of classloading to defer only to the parent if the current class loader can't be
242: * found. This is required to allow for the launcher to be embedded within webwork.jar (otherwise the dependencies
243: * wouldn't be found by the system ClassLoader when invoking using "java -jar webwork.jar ...").
244: */
245: public static class MainClassLoader extends URLClassLoader {
246: public MainClassLoader(URL[] urls) {
247: super (urls);
248: }
249:
250: public Class loadClass(String name, boolean resolve)
251: throws ClassNotFoundException {
252: if (name.startsWith("org.xml.")
253: || name.startsWith("org.w3c.")
254: || name.startsWith("java.")
255: || name.startsWith("javax.")
256: || name.startsWith("sun.")
257: || name.startsWith("com.sun.")) {
258: return super .loadClass(name, resolve);
259: }
260:
261: ClassLoader parent = getParent();
262: // First, check if the class has already been loaded
263: Class c = findLoadedClass(name);
264: if (c == null) {
265: try {
266: c = findClass(name);
267: } catch (Throwable t) {
268: // If still not found, only then ask the parent
269: c = parent.loadClass(name);
270: }
271: }
272: if (resolve) {
273: resolveClass(c);
274: }
275:
276: return c;
277: }
278: }
279: }
|