1 package com.opensymphony.webwork; 2 3 import java.io.File ; 4 import java.lang.reflect.Method ; 5 import java.net.MalformedURLException ; 6 import java.net.URL ; 7 import java.net.URLClassLoader ; 8 import java.util.ArrayList ; 9 10 15 public class Main { 16 public static void main(String [] args) { 17 ArrayList urls = new ArrayList (); 18 try { 19 findJars(new File ("lib"), urls); 20 urls.add(new File ("webwork-2.2.jar").toURL()); 21 } catch (MalformedURLException e) { 22 e.printStackTrace(); 23 System.out.println("Could not find URLs -- see stack trace."); 24 } 25 26 String command = args[0]; 27 if ("prototype".equals(command)) { 28 URLClassLoader cl = new URLClassLoader ((URL []) urls.toArray(new URL [urls.size()]), 29 Main.class.getClassLoader()); 30 Thread.currentThread().setContextClassLoader(cl); 31 try { 32 Class clazz = cl.loadClass("com.opensymphony.webwork.Prototype"); 33 Method main = clazz.getDeclaredMethod("main", new Class []{String [].class}); 34 main.invoke(null, new Object []{new String [0]}); 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 } 40 41 private static void findJars(File file, ArrayList urls) throws MalformedURLException { 42 File [] files = file.listFiles(); 43 for (int i = 0; i < files.length; i++) { 44 File f = files[i]; 45 if (f.isDirectory()) { 46 findJars(f, urls); 47 } else if (f.getName().endsWith(".jar")) { 48 urls.add(f.toURL()); 49 } 50 } 51 } 52 53 static class MyClassLoader extends URLClassLoader { 54 private ClassLoader parent; 55 56 public MyClassLoader(URL [] urls, ClassLoader parent) { 57 super(urls, parent); 58 this.parent = parent; 59 } 60 61 public Class loadClass(String name) throws ClassNotFoundException { 62 Class aClass = null; 63 try { 64 aClass = findClass(name); 65 } catch (ClassNotFoundException e) { 66 } 67 68 if (aClass != null) { 69 return aClass; 70 } else { 71 return super.loadClass(name); 72 } 73 } 74 75 public URL getResource(String name) { 76 URL url = findResource(name); 77 if (url == null && parent != null) { 78 url = super.getResource(name); 79 } 80 81 return url; 82 } 83 84 } 85 } 86 | Popular Tags |