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 ; 13 import java.io.IOException ; 14 import java.net.URL ; 15 import java.net.URLClassLoader ; 16 import java.util.ArrayList ; 17 import java.util.StringTokenizer ; 18 19 24 public class Prototype { 25 public static void main(String [] 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 contextPath = args[0]; 36 String 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 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 StringTokenizer st = new StringTokenizer (sources, ","); 52 ArrayList fileList = new ArrayList (); 53 while (st.hasMoreTokens()) { 54 String token = st.nextToken(); 55 if (!token.endsWith("/")) { 56 token = token + "/"; 57 } 58 fileList.add(new File (token)); 59 } 60 fileList.add(new File (webapp + "/WEB-INF/classes/")); 61 File [] files = (File []) fileList.toArray(new File [fileList.size()]); 62 63 URL [] urls = new URL [files.length]; 64 for (int i = 0; i < files.length; i++) { 65 File file = files[i]; 66 if (!file.exists()) { 67 throw new RuntimeException ("Source dir does not exist: " + file.toString()); 68 } 69 urls[i] = file.getCanonicalFile().toURL(); 70 } 71 72 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 74 for (int i = 0; i < files.length; i++) { 75 File file = files[i]; 76 parent = new CompilingClassLoader(parent, file, new EclipseJavaCompiler()); 77 } 78 URLClassLoader url = new MyURLClassLoader(urls, parent); 79 Thread.currentThread().setContextClassLoader(url); 80 81 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 e) { 94 e.printStackTrace(); 95 } 96 97 } 98 99 static class PrototypeWebAppContext extends WebApplicationContext { 100 public PrototypeWebAppContext() { 101 } 102 103 public PrototypeWebAppContext(String string) { 104 super(string); 105 } 106 107 public Resource getResource(String string) throws IOException { 108 if (string.startsWith("/WEB-INF/lib/")) { 109 String jar = string.substring("/WEB-INF/lib/".length()); 110 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 111 while (parent != null) { 112 if (parent instanceof URLClassLoader ) { 113 URL [] urls = ((URLClassLoader ) parent).getURLs(); 114 for (int i = 0; i < urls.length; i++) { 115 URL 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 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 { 137 private ClassLoader parent; 138 139 public MyURLClassLoader(URL [] urls, ClassLoader parent) { 140 super(urls, parent); 141 this.parent = parent; 142 } 143 144 public Class loadClass(String name) throws ClassNotFoundException { 145 Class aClass = null; 146 147 try { 148 aClass = parent.loadClass(name); 149 if (aClass != null) { 150 return aClass; 151 } 152 } catch (ClassNotFoundException e) { 153 } 154 155 return super.loadClass(name); 156 } 157 158 public URL getResource(String name) { 159 URL 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 |