01: package org.drools.rule;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: import org.drools.rule.PackageCompilationData.PackageClassLoader;
08:
09: public class CompositePackageClassLoader extends ClassLoader {
10:
11: private final List classLoaders = new ArrayList();
12:
13: public CompositePackageClassLoader(
14: final ClassLoader parentClassLoader) {
15: super (parentClassLoader);
16: }
17:
18: public void addClassLoader(final ClassLoader classLoader) {
19: this .classLoaders.add(classLoader);
20: }
21:
22: public void removeClassLoader(final ClassLoader classLoader) {
23: for (final Iterator it = this .classLoaders.iterator(); it
24: .hasNext();) {
25: if (it.next() == classLoader) {
26: it.remove();
27: break;
28: }
29: }
30: }
31:
32: private Class compositeFastFindClass(final String name) {
33: for (final Iterator it = this .classLoaders.iterator(); it
34: .hasNext();) {
35: final DroolsClassLoader classLoader = (DroolsClassLoader) it
36: .next();
37: final Class clazz = classLoader.fastFindClass(name);
38: if (clazz != null) {
39: return clazz;
40: }
41: }
42: return null;
43: }
44:
45: /**
46: * Javadocs recommend that this method not be overloaded. We overload this so that we can prioritise the fastFindClass
47: * over method calls to parent.loadClass(name, false); and c = findBootstrapClass0(name); which the default implementation
48: * would first - hence why we call it "fastFindClass" instead of standard findClass, this indicates that we give it a
49: * higher priority than normal.
50: *
51: */
52: protected synchronized Class loadClass(final String name,
53: final boolean resolve) throws ClassNotFoundException {
54: Class clazz = findLoadedClass(name);
55:
56: if (clazz == null) {
57: clazz = compositeFastFindClass(name);
58:
59: if (clazz == null) {
60:
61: final ClassLoader parent = getParent();
62: if (parent != null) {
63: clazz = parent.loadClass(name);
64: } else {
65: throw new ClassNotFoundException(name);
66: }
67: }
68: }
69:
70: if (resolve) {
71: resolveClass(clazz);
72: }
73:
74: return clazz;
75: }
76:
77: protected Class findClass(final String name)
78: throws ClassNotFoundException {
79: final Class clazz = compositeFastFindClass(name);
80: if (clazz == null) {
81: throw new ClassNotFoundException(name);
82: }
83: return clazz;
84: }
85:
86: }
|