01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package com.opensymphony.webwork.util.classloader.stores;
17:
18: import org.apache.commons.logging.Log;
19: import org.apache.commons.logging.LogFactory;
20:
21: /**
22: * @author tcurdt
23: */
24: public final class ResourceStoreClassLoader extends ClassLoader {
25:
26: private final static Log log = LogFactory
27: .getLog(ResourceStoreClassLoader.class);
28:
29: private final ResourceStore store;
30:
31: public ResourceStoreClassLoader(final ClassLoader pParent,
32: final ResourceStore pStore) {
33: super (pParent);
34: store = pStore;
35: }
36:
37: private Class fastFindClass(final String name) {
38: final byte[] clazzBytes = store.read(name);
39:
40: if (clazzBytes != null) {
41: log.debug("found class " + name + " (" + clazzBytes.length
42: + " bytes)");
43: return defineClass(name, clazzBytes, 0, clazzBytes.length);
44: }
45:
46: return null;
47: }
48:
49: protected synchronized Class loadClass(String name, boolean resolve)
50: throws ClassNotFoundException {
51: Class clazz = findLoadedClass(name);
52:
53: if (clazz == null) {
54: clazz = fastFindClass(name);
55:
56: if (clazz == null) {
57:
58: final ClassLoader parent = getParent();
59: if (parent != null) {
60: clazz = parent.loadClass(name);
61: log.debug("loaded from parent: " + name);
62: } else {
63: throw new ClassNotFoundException(name);
64: }
65:
66: } else {
67: log.debug("loaded from store: " + name);
68: }
69: }
70:
71: if (resolve) {
72: resolveClass(clazz);
73: }
74:
75: return clazz;
76: }
77:
78: protected Class findClass(final String name)
79: throws ClassNotFoundException {
80: final Class clazz = fastFindClass(name);
81: if (clazz == null) {
82: throw new ClassNotFoundException(name);
83: }
84: return clazz;
85: }
86: }
|