KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > Prototype


1 package com.opensymphony.webwork;
2
3 import org.apache.commons.jci.CompilingClassLoader;
4 import org.apache.commons.jci.compilers.eclipse.EclipseJavaCompiler;
5 import org.mortbay.http.SocketListener;
6 import org.mortbay.jetty.Server;
7 import org.mortbay.jetty.servlet.WebApplicationContext;
8 import org.mortbay.util.FileResource;
9 import org.mortbay.util.JarResource;
10 import org.mortbay.util.Resource;
11
12 import java.io.File JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.net.URLClassLoader JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.StringTokenizer JavaDoc;
18
19 /**
20  * User: plightbo
21  * Date: Aug 7, 2005
22  * Time: 7:14:01 PM
23  */

24 public class Prototype {
25     public static void main(String JavaDoc[] args) {
26         if (args.length != 3) {
27             System.err.println("prototype must be invoked with three argumenets:");
28             System.err.println("[contextPath] [webapp] [sources]");
29             System.err.println("");
30             System.err.println("Ex: java -jar webwork-launcher.jar \\");
31             System.err.println(" prototype /sandbox webapps/sandbox/src/webapp webapps/sandbox/src/java");
32             return;
33         }
34
35         String JavaDoc contextPath = args[0];
36         String JavaDoc webapp = args[1];
37
38         if (webapp == null) {
39             System.out.println("webapp must be specified as an exploded war");
40             return;
41         }
42
43         String JavaDoc sources = args[2];
44         if (sources == null) {
45             System.out.println("-Dsources must be specified as a comma-separated list of Java source paths.");
46             return;
47         }
48
49         try {
50             // set up files and urls
51
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sources, ",");
52             ArrayList JavaDoc fileList = new ArrayList JavaDoc();
53             while (st.hasMoreTokens()) {
54                 String JavaDoc token = st.nextToken();
55                 if (!token.endsWith("/")) {
56                     token = token + "/";
57                 }
58                 fileList.add(new File JavaDoc(token));
59             }
60             fileList.add(new File JavaDoc(webapp + "/WEB-INF/classes/"));
61             File JavaDoc[] files = (File JavaDoc[]) fileList.toArray(new File JavaDoc[fileList.size()]);
62
63             URL JavaDoc[] urls = new URL JavaDoc[files.length];
64             for (int i = 0; i < files.length; i++) {
65                 File JavaDoc file = files[i];
66                 if (!file.exists()) {
67                     throw new RuntimeException JavaDoc("Source dir does not exist: " + file.toString());
68                 }
69                 urls[i] = file.getCanonicalFile().toURL();
70             }
71
72             // deal with classloader
73
ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
74             for (int i = 0; i < files.length; i++) {
75                 File JavaDoc file = files[i];
76                 parent = new CompilingClassLoader(parent, file, new EclipseJavaCompiler());
77             }
78             URLClassLoader JavaDoc url = new MyURLClassLoader(urls, parent);
79             Thread.currentThread().setContextClassLoader(url);
80
81             // finally, start the server!
82
Server server = new Server();
83             SocketListener socketListener = new SocketListener();
84             socketListener.setPort(8080);
85             server.addListener(socketListener);
86
87             WebApplicationContext ctx = new PrototypeWebAppContext(webapp);
88             ctx.setContextPath(contextPath);
89             ctx.setClassLoader(url);
90             server.addContext("localhost", ctx);
91
92             server.start();
93         } catch (Exception JavaDoc e) {
94             e.printStackTrace();
95         }
96
97     }
98
99     static class PrototypeWebAppContext extends WebApplicationContext {
100         public PrototypeWebAppContext() {
101         }
102
103         public PrototypeWebAppContext(String JavaDoc string) {
104             super(string);
105         }
106
107         public Resource getResource(String JavaDoc string) throws IOException JavaDoc {
108             if (string.startsWith("/WEB-INF/lib/")) {
109                 String JavaDoc jar = string.substring("/WEB-INF/lib/".length());
110                 ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
111                 while (parent != null) {
112                     if (parent instanceof URLClassLoader JavaDoc) {
113                         URL JavaDoc[] urls = ((URLClassLoader JavaDoc) parent).getURLs();
114                         for (int i = 0; i < urls.length; i++) {
115                             URL JavaDoc url = urls[i];
116                             if (url.toExternalForm().endsWith(jar)) {
117                                 return JarResource.newResource(url);
118                             }
119                         }
120                     }
121
122                     parent = parent.getParent();
123                 }
124             }
125
126             // still haven't found what we're looking for?
127
// Alright, let's just hack this to work in IDEA
128
if (string.equals("/webwork")) {
129                 return FileResource.newResource("src/java/META-INF/taglib.tld");
130             }
131
132             return super.getResource(string);
133         }
134     }
135
136     static class MyURLClassLoader extends URLClassLoader JavaDoc {
137         private ClassLoader JavaDoc parent;
138
139         public MyURLClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
140             super(urls, parent);
141             this.parent = parent;
142         }
143
144         public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
145             Class JavaDoc aClass = null;
146
147             try {
148                 aClass = parent.loadClass(name);
149                 if (aClass != null) {
150                     return aClass;
151                 }
152             } catch (ClassNotFoundException JavaDoc e) {
153             }
154
155             return super.loadClass(name);
156         }
157
158         public URL JavaDoc getResource(String JavaDoc name) {
159             URL JavaDoc url = findResource(name);
160             if (url == null && parent != null) {
161                 url = parent.getResource(name);
162             }
163
164             return url;
165         }
166     }
167 }
168
Popular Tags