1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jci.stores;
18
19 import org.apache.commons.jci.utils.ConversionUtils;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23
24
25
26
27
28
29 public final class ResourceStoreClassLoader extends ClassLoader {
30
31 private final Log log = LogFactory.getLog(ResourceStoreClassLoader.class);
32
33 private final ResourceStore[] stores;
34
35 public ResourceStoreClassLoader( final ClassLoader pParent, final ResourceStore[] pStores ) {
36 super(pParent);
37 stores = pStores;
38 }
39
40 private Class fastFindClass(final String name) {
41
42 if (stores != null) {
43 for (int i = 0; i < stores.length; i++) {
44 final ResourceStore store = stores[i];
45 final byte[] clazzBytes = store.read(ConversionUtils.convertClassToResourcePath(name));
46 if (clazzBytes != null) {
47 log.debug(getId() + " found class: " + name + " (" + clazzBytes.length + " bytes)");
48 return defineClass(name, clazzBytes, 0, clazzBytes.length);
49 }
50 }
51 }
52
53 return null;
54 }
55
56 protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
57
58 Class clazz = findLoadedClass(name);
59
60 if (clazz == null) {
61 clazz = fastFindClass(name);
62
63 if (clazz == null) {
64
65 final ClassLoader parent = getParent();
66 if (parent != null) {
67 clazz = parent.loadClass(name);
68
69 } else {
70 throw new ClassNotFoundException(name);
71 }
72
73 } else {
74 log.debug(getId() + " loaded from store: " + name);
75 }
76 }
77
78 if (resolve) {
79 resolveClass(clazz);
80 }
81
82 return clazz;
83 }
84
85 protected Class findClass( final String name ) throws ClassNotFoundException {
86 final Class clazz = fastFindClass(name);
87 if (clazz == null) {
88 throw new ClassNotFoundException(name);
89 }
90 return clazz;
91 }
92
93 private String getId() {
94 return "" + this + "[" + this.getClass().getClassLoader() + "]";
95 }
96 }