01: package dalma.container;
02:
03: import org.apache.commons.javaflow.ContinuationClassLoader;
04: import org.apache.commons.javaflow.bytecode.transformation.bcel.BcelClassTransformer;
05: import org.apache.bcel.util.ClassLoaderRepository;
06:
07: import java.io.File;
08: import java.io.FilenameFilter;
09: import java.io.IOException;
10: import java.net.URL;
11: import java.net.URLClassLoader;
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: /**
16: * Builds a {@link ClassLoader}.
17: *
18: * @author Kohsuke Kawaguchi
19: */
20: class ClassLoaderBuilder {
21: /**
22: * List of {@link URL}s to be made available.
23: */
24: private final List<URL> urls = new ArrayList<URL>();
25:
26: private final ClassLoader parent;
27:
28: public ClassLoaderBuilder(ClassLoader parent) {
29: this .parent = parent;
30: }
31:
32: /**
33: * Adds all the jar files in the given directory.
34: */
35: public void addJarFiles(File dir) {
36: // list up *.jar files in the appDir
37: File[] jarFiles = dir.listFiles(new FilenameFilter() {
38: public boolean accept(File dir, String name) {
39: return name.endsWith(".jar");
40: }
41: });
42:
43: if (jarFiles == null)
44: return; // no such dir
45:
46: for (File jar : jarFiles)
47: addPathElement(jar);
48: }
49:
50: /**
51: * Adds a path element.
52: *
53: * @param dir
54: * A directory or a jar file.
55: */
56: public void addPathElement(File dir) {
57: try {
58: urls.add(dir.toURI().toURL());
59: } catch (IOException e) {
60: throw new AssertionError(e); // impossible
61: }
62: }
63:
64: public ClassLoader make() {
65: return new URLClassLoader(urls.toArray(new URL[urls.size()]),
66: parent);
67: }
68:
69: public ClassLoader makeContinuable() {
70: DelegatingRepository dr = new DelegatingRepository();
71: ClassLoader cl = new ContinuationClassLoader(urls
72: .toArray(new URL[urls.size()]), parent,
73: new BcelClassTransformer(dr));
74: dr.setRepository(new ClassLoaderRepository(cl));
75: return cl;
76: }
77: }
|