KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.opensymphony.webwork;
2
3 import java.io.File JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.net.URLClassLoader JavaDoc;
8 import java.util.ArrayList JavaDoc;
9
10 /**
11  * User: plightbo
12  * Date: Aug 9, 2005
13  * Time: 12:22:06 AM
14  */

15 public class Main {
16     public static void main(String JavaDoc[] args) {
17         ArrayList JavaDoc urls = new ArrayList JavaDoc();
18         try {
19             findJars(new File JavaDoc("lib"), urls);
20             urls.add(new File JavaDoc("webwork-2.2.jar").toURL());
21         } catch (MalformedURLException JavaDoc e) {
22             e.printStackTrace();
23             System.out.println("Could not find URLs -- see stack trace.");
24         }
25
26         String JavaDoc command = args[0];
27         if ("prototype".equals(command)) {
28             URLClassLoader JavaDoc cl = new URLClassLoader JavaDoc((URL JavaDoc[]) urls.toArray(new URL JavaDoc[urls.size()]),
29                     Main.class.getClassLoader());
30             Thread.currentThread().setContextClassLoader(cl);
31             try {
32                 Class JavaDoc clazz = cl.loadClass("com.opensymphony.webwork.Prototype");
33                 Method JavaDoc main = clazz.getDeclaredMethod("main", new Class JavaDoc[]{String JavaDoc[].class});
34                 main.invoke(null, new Object JavaDoc[]{new String JavaDoc[0]});
35             } catch (Exception JavaDoc e) {
36                 e.printStackTrace();
37             }
38         }
39     }
40
41     private static void findJars(File JavaDoc file, ArrayList JavaDoc urls) throws MalformedURLException JavaDoc {
42         File JavaDoc[] files = file.listFiles();
43         for (int i = 0; i < files.length; i++) {
44             File JavaDoc 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 JavaDoc {
54         private ClassLoader JavaDoc parent;
55
56         public MyClassLoader(URL JavaDoc[] urls, ClassLoader JavaDoc parent) {
57             super(urls, parent);
58             this.parent = parent;
59         }
60
61         public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
62             Class JavaDoc aClass = null;
63             try {
64                 aClass = findClass(name);
65             } catch (ClassNotFoundException JavaDoc e) {
66             }
67
68             if (aClass != null) {
69                 return aClass;
70             } else {
71                 return super.loadClass(name);
72             }
73         }
74
75         public URL JavaDoc getResource(String JavaDoc name) {
76             URL JavaDoc 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