01: /*
02: * @(#)MultiClassLoader.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution of
07: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.compiler;
10:
11: import java.util.Stack;
12:
13: public class MultiClassLoader extends ClassLoader {
14:
15: private ClassLoader[] loaders;
16:
17: public MultiClassLoader(ClassLoader cl1, ClassLoader cl2) {
18: this (cl1, new ClassLoader[] { cl2 });
19: }
20:
21: public MultiClassLoader(ClassLoader parent, ClassLoader[] cl) {
22: super (parent);
23: this .loaders = init(cl);
24: }
25:
26: ClassLoader[] init(ClassLoader[] base) {
27: if (base == null || base.length < 1) {
28: return null;
29: }
30: Stack v = new Stack();
31: ClassLoader cl0 = base[0];
32: Class c0 = cl0.getClass();
33: v.push(cl0);
34: for (int i = 1; i < base.length; i++) {
35: ClassLoader cl = base[i];
36: Class c = cl.getClass();
37: if (c != c0) {
38: if (c.isInstance(cl0)) {
39: v.pop();
40: v.push(cl);
41: } else if (!c0.isInstance(cl)) {
42: v.push(cl);
43: }
44: }
45: cl0 = cl;
46: c0 = c;
47: }
48: ClassLoader[] ret = new ClassLoader[v.size()];
49: v.copyInto(ret);
50: return ret;
51: }
52:
53: protected Class findClass(final String name)
54: throws ClassNotFoundException {
55: if (loaders != null) {
56: for (int i = 0; i < loaders.length; i++) {
57: ClassLoader cl = loaders[i];
58: try {
59: return cl.loadClass(name);
60: } catch (ClassNotFoundException e) {
61: }
62: }
63: }
64: throw new ClassNotFoundException(name);
65: }
66: }
|