01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.quickstart;
06:
07: import java.net.URLClassLoader;
08: import java.net.URL;
09: import java.net.MalformedURLException;
10: import java.io.File;
11: import java.util.ArrayList;
12:
13: /**
14: * Integration with Jetty.
15: *
16: * @author patrick
17: */
18: public class MultiDirClassLoader extends URLClassLoader {
19: private ClassLoader parent;
20:
21: public MultiDirClassLoader(String[] dirs, String[] cps,
22: ClassLoader parent) throws MalformedURLException {
23: super (getAllURLs(dirs, cps), parent);
24: this .parent = parent;
25: }
26:
27: public Class loadClass(String name) throws ClassNotFoundException {
28: Class aClass;
29:
30: try {
31: aClass = parent.loadClass(name);
32: if (aClass != null) {
33: return aClass;
34: }
35: } catch (ClassNotFoundException e) {
36: // ok, keep trying
37: }
38:
39: return super .loadClass(name);
40: }
41:
42: public URL getResource(String name) {
43: URL url = findResource(name);
44: if (url == null && parent != null) {
45: url = parent.getResource(name);
46: }
47:
48: return url;
49: }
50:
51: private static URL[] getAllURLs(String[] dirs, String[] cps)
52: throws MalformedURLException {
53: ArrayList urls = new ArrayList();
54:
55: for (int i = 0; i < cps.length; i++) {
56: String cp = cps[i];
57: urls.add(new File(cp).toURL());
58: }
59:
60: for (int i = 0; i < dirs.length; i++) {
61: String dir = dirs[i];
62: File file = new File(dir);
63: findJars(file, urls);
64: }
65:
66: return (URL[]) urls.toArray(new URL[urls.size()]);
67: }
68:
69: private static void findJars(File file, ArrayList fileList)
70: throws MalformedURLException {
71: if (file.isDirectory()) {
72: File[] files = file.listFiles();
73: for (int i = 0; i < files.length; i++) {
74: File f = files[i];
75: findJars(f, fileList);
76: }
77: } else if (file.getName().endsWith(".jar")) {
78: // Manually exclude the local license file so that it's possible to run
79: // clustering.
80: if (!file.getName().equals("tangosol-license-local.jar")) {
81: fileList.add(file.toURL());
82: }
83: }
84: }
85: }
|