01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.mx.loading;
23:
24: import java.net.URLStreamHandlerFactory;
25:
26: import org.jboss.util.loading.DelegatingClassLoader;
27:
28: /**
29: * A delegating classloader that first peeks in the loader
30: * repository's cache.
31: *
32: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
33: * @version $Revision: 57200 $
34: */
35: public class LoaderRepositoryClassLoader extends DelegatingClassLoader {
36: /** The loader repository */
37: protected LoaderRepository repository;
38:
39: /**
40: * Constructor
41: *
42: * @param parent the parent classloader, cannot be null.
43: * @param repository the loader repository, cannot be null.
44: */
45: public LoaderRepositoryClassLoader(ClassLoader parent,
46: LoaderRepository repository) {
47: super (parent);
48: if (repository == null)
49: throw new IllegalArgumentException("No repository");
50: this .repository = repository;
51: }
52:
53: /**
54: * Constructor
55: *
56: * @param parent, the parent classloader, cannot be null.
57: * @param factory the url stream factory.
58: */
59: public LoaderRepositoryClassLoader(ClassLoader parent,
60: LoaderRepository repository, URLStreamHandlerFactory factory) {
61: super (parent);
62: if (repository == null)
63: throw new IllegalArgumentException("No repository");
64: this .repository = repository;
65: }
66:
67: /**
68: * Load a class, first peek in the loader repository cache then
69: * ask the parent.
70: *
71: * @param className the class name to load
72: * @param resolve whether to link the class
73: * @return the loaded class
74: * @throws ClassNotFoundException when the class could not be found
75: */
76: protected Class loadClass(String className, boolean resolve)
77: throws ClassNotFoundException {
78: Class clazz = repository.getCachedClass(className);
79: if (clazz != null) {
80: if (resolve)
81: resolveClass(clazz);
82: return clazz;
83: }
84:
85: // Delegate
86: return super.loadClass(className, resolve);
87: }
88: }
|