KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > loading > LoaderRepositoryClassLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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 JavaDoc;
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: 37459 $
34  */

35 public class LoaderRepositoryClassLoader
36    extends DelegatingClassLoader
37 {
38    /** The loader repository */
39    protected LoaderRepository repository;
40
41    /**
42     * Constructor
43     *
44     * @param parent the parent classloader, cannot be null.
45     * @param repository the loader repository, cannot be null.
46     */

47    public LoaderRepositoryClassLoader(ClassLoader JavaDoc parent, LoaderRepository repository)
48    {
49       super(parent);
50       if (repository == null)
51          throw new IllegalArgumentException JavaDoc("No repository");
52       this.repository = repository;
53    }
54
55    /**
56     * Constructor
57     *
58     * @param parent, the parent classloader, cannot be null.
59     * @param factory the url stream factory.
60     */

61    public LoaderRepositoryClassLoader(ClassLoader JavaDoc parent, LoaderRepository repository, URLStreamHandlerFactory JavaDoc factory)
62    {
63       super(parent);
64       if (repository == null)
65          throw new IllegalArgumentException JavaDoc("No repository");
66       this.repository = repository;
67    }
68
69    /**
70     * Load a class, first peek in the loader repository cache then
71     * ask the parent.
72     *
73     * @param className the class name to load
74     * @param resolve whether to link the class
75     * @return the loaded class
76     * @throws ClassNotFoundException when the class could not be found
77     */

78    protected Class JavaDoc loadClass(String JavaDoc className, boolean resolve)
79       throws ClassNotFoundException JavaDoc
80    {
81       Class JavaDoc clazz = repository.getCachedClass(className);
82       if (clazz != null)
83       {
84          if (resolve)
85             resolveClass(clazz);
86          return clazz;
87       }
88
89       // Delegate
90
return super.loadClass(className, resolve);
91    }
92 }
93
Popular Tags