1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.quickstart;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileReader;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.net.URLClassLoader;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map;
32
33 import com.thoughtworks.xstream.XStream;
34 import com.thoughtworks.xstream.io.xml.DomDriver;
35
36 /***
37 * The QuickStart main program.
38 *
39 */
40 public class QuickStart {
41 public static void main(String[] args) throws FileNotFoundException, MalformedURLException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
42 if (args.length != 3 && args.length != 0) {
43 System.err.println("QuickStart must be either invoked with three arguments or no arguments:");
44 System.err.println("[contextPath] [webapp] [sources]");
45 System.err.println("");
46 System.err.println("Ex: java -jar struts.jar //");
47 System.err.println(" quickstart /sandbox sandbox/src/webapp sandbox/src/java");
48 System.err.println("");
49 System.err.println("OR");
50 System.err.println("");
51 System.err.println("Ex: java -jar struts.jar quickstart");
52 System.err.println(" Where a 'quickstart.xml' file exists in your working directory");
53 return;
54 }
55
56 Configuration c;
57 if (args.length == 0) {
58 XStream xstream = new XStream(new DomDriver());
59 xstream.alias("configuration", Configuration.class);
60 xstream.alias("extendsConfig", String.class);
61 xstream.alias("port", int.class);
62 xstream.alias("context", String.class);
63 xstream.alias("dir", String.class);
64 xstream.alias("path", String.class);
65 xstream.alias("webDir", Mapping.class);
66 File config = new File("quickstart.xml");
67 if (!config.exists()) {
68
69 System.err.println("Could not find quickstart.xml!");
70 System.err.println("Tip: quickstart.xml must exist in your working directory");
71 System.err.println("");
72 System.err.println("Alternatively, if you your deployment is simple, try launching");
73 System.err.println("QuickStart using the simple command line options rather than");
74 System.err.println("Relying on quickstart.xml existing");
75 return;
76 }
77
78 c = (Configuration) xstream.fromXML(new FileReader(config));
79 c.resolveDirs(config.getParent());
80 c.resolveExtensions(config.getParent(), xstream);
81 } else {
82 c = new Configuration();
83 c.setContext(args[0]);
84 c.setPort(new Integer(8080));
85 ArrayList webDirs = new ArrayList();
86 webDirs.add(new Mapping("/", args[1]));
87 c.setWebDirs(webDirs);
88 ArrayList sources = new ArrayList();
89 sources.add(args[2]);
90 c.setSources(sources);
91 ArrayList classDirs = new ArrayList();
92 classDirs.add(args[1] + "/WEB-INF/classes");
93 classDirs.add(args[2]);
94 c.setClassDirs(classDirs);
95 ArrayList libs = new ArrayList();
96 libs.add("lib");
97 c.setLibs(libs);
98
99 c.resolveDirs(new File(".").getParent());
100 }
101
102
103 if (c.validate()) {
104 return;
105 }
106
107
108 System.out.println("Launching Jetty with the following configuration:");
109 System.out.println("Jars/Directory of jars:");
110 for (Iterator iterator = c.getLibs().iterator(); iterator.hasNext();) {
111 String s = (String) iterator.next();
112 System.out.println(" " + s);
113 }
114 System.out.println("Directories of classes:");
115 for (Iterator iterator = c.getClassDirs().iterator(); iterator.hasNext();) {
116 String s = (String) iterator.next();
117 System.out.println(" " + s);
118 }
119 if (c.getSources() != null) {
120 System.out.println("Sources:");
121 for (Iterator iterator = c.getSources().iterator(); iterator.hasNext();) {
122 String s = (String) iterator.next();
123 System.out.println(" " + s);
124 }
125 }
126 System.out.println("WebApp directories:");
127 for (Iterator iterator = c.getMappings().entrySet().iterator(); iterator.hasNext();) {
128 Map.Entry entry = (Map.Entry) iterator.next();
129 System.out.println(entry.getKey() + " -> " + entry.getValue());
130 }
131
132
133 List libs = c.getLibs();
134 List classDirs = c.getClassDirs();
135 ClassLoader parent = new MultiDirClassLoader((String[]) libs.toArray(new String[libs.size()]),
136 (String[]) classDirs.toArray(new String[classDirs.size()]),
137 Thread.currentThread().getContextClassLoader());
138
139 if (c.getSources() != null) {
140 System.out.print("Automatic compiling of classes no longer supported.");
141 }
142 URLClassLoader url = new MyURLClassLoader(parent);
143 Thread.currentThread().setContextClassLoader(url);
144
145 Class clazz = url.loadClass("org.apache.struts2.quickstart.JettyServer");
146 Method method = clazz.getDeclaredMethod("startServer",
147 new Class[]{int.class, String.class, List.class, Map.class, String.class});
148 method.invoke(null, new Object[]{c.port, c.getContext(), c.getPathPriority(), c.getMappings(), c.getResolver()});
149
150 System.out.println("");
151 System.out.println("********************************************************");
152 System.out.println("Quick-started at http://localhost:" + c.getPort() + c.getContext());
153 System.out.println("You may now edit your Java classes and web files without");
154 System.out.println(" deploying or restarting.");
155 System.out.println("********************************************************");
156 }
157
158 static class MyURLClassLoader extends URLClassLoader {
159 private ClassLoader parent;
160
161 public MyURLClassLoader(ClassLoader parent) {
162 super(new URL[0], parent);
163 this.parent = parent;
164 }
165
166 public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
167 if (name.startsWith("org.xml.") || name.startsWith("org.w3c.")
168 || name.startsWith("java.") || name.startsWith("javax.")
169 || name.startsWith("sun.") || name.startsWith("com.sun.")) {
170 return super.loadClass(name, resolve);
171 }
172
173 ClassLoader parent = getParent();
174
175 Class c = findLoadedClass(name);
176 if (c == null) {
177 try {
178 c = findClass(name);
179 } catch (Throwable t) {
180
181 c = parent.loadClass(name);
182 }
183 }
184 if (resolve) {
185 resolveClass(c);
186 }
187
188 return c;
189 }
190
191 public URL getResource(String name) {
192 URL url = findResource(name);
193 if (url == null && parent != null) {
194 url = parent.getResource(name);
195 }
196
197 return url;
198 }
199 }
200 }