KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > classman > runtime > JoinClassLoader


1 /*
2  * Copyright (C) The Spice Group. All rights reserved.
3  *
4  * This software is published under the terms of the Spice
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.loom.classman.runtime;
9
10 import java.io.IOException JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.security.SecureClassLoader JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 /**
17  * The <tt>JoinClassLoader</tt> is a {@link ClassLoader} that joins a list of
18  * ClassLoaders together. The JoinClassLoader has a list of ClassLoaders
19  * specified from which resources and classes can be loaded from. The
20  * JoinClassLoader will attempt to load the Class or resources from each
21  * ClassLoader in succession.
22  *
23  * <p>Note that it is recomended that each ClassLoader be made up of distinct,
24  * non-overlapping sets of resources or else ClassCastExceptions may result
25  * along with other undersired behaviour. </p>
26  *
27  * @author Peter Donald
28  * @version $Revision: 1.1 $ $Date: 2004/04/19 22:19:26 $
29  */

30 public class JoinClassLoader
31     extends SecureClassLoader JavaDoc
32 {
33     /**
34      * The list of classLoaders to search through each time a class or resource
35      * is requested.
36      */

37     private final ClassLoader JavaDoc[] m_classLoaders;
38
39     /**
40      * Construct a join ClassLoader that defines parent ClassLoader and list of
41      * ClassLoaders to search for classes or resources when requested.
42      *
43      * @param parent the parent classloader
44      * @param classLoaders the classloaders to search
45      */

46     public JoinClassLoader( final ClassLoader JavaDoc[] classLoaders,
47                             final ClassLoader JavaDoc parent )
48     {
49         super( parent );
50         if( null == classLoaders )
51         {
52             throw new NullPointerException JavaDoc( "classLoaders" );
53         }
54         for( int i = 0; i < classLoaders.length; i++ )
55         {
56             if( null == classLoaders[ i ] )
57             {
58                 throw new NullPointerException JavaDoc( "classLoaders[" + i + "]" );
59             }
60         }
61         m_classLoaders = classLoaders;
62     }
63
64     /**
65      * Overide findClass to find a class by searching all the ClassLoaders for
66      * class.
67      *
68      * @param name the name of class
69      * @return the Class instance
70      * @throws ClassNotFoundException if unable to find class
71      * @see ClassLoader#findClass
72      */

73     protected Class JavaDoc findClass( final String JavaDoc name )
74         throws ClassNotFoundException JavaDoc
75     {
76         for( int i = 0; i < m_classLoaders.length; i++ )
77         {
78             try
79             {
80                 return m_classLoaders[ i ].loadClass( name );
81             }
82             catch( final ClassNotFoundException JavaDoc cnfe )
83             {
84                 //Not in that classloader
85
}
86         }
87
88         return super.findClass( name );
89     }
90
91     /**
92      * Overide findResources to retrieve all the resources from all classloaders
93      * with a particular name.
94      *
95      * @param name the resources to search for
96      * @return an enumeration of all resources with specified name
97      * @throws IOException if unable to find resources
98      * @see ClassLoader#findResources
99      */

100     protected Enumeration JavaDoc findResources( final String JavaDoc name )
101         throws IOException JavaDoc
102     {
103         final Vector JavaDoc result = new Vector JavaDoc();
104
105         for( int i = 0; i < m_classLoaders.length; i++ )
106         {
107             try
108             {
109                 final Enumeration JavaDoc resources =
110                     m_classLoaders[ i ].getResources( name );
111                 addAll( result, resources );
112             }
113             catch( final IOException JavaDoc ioe )
114             {
115                 //Not in that classloader
116
}
117         }
118         final Enumeration JavaDoc resources = super.findResources( name );
119         addAll( result, resources );
120         return result.elements();
121     }
122
123     /**
124      * Add all resources specified into result vector.
125      *
126      * @param result the result vector
127      * @param resources the enumeration of resources
128      */

129     private void addAll( final Vector JavaDoc result,
130                          final Enumeration JavaDoc resources )
131     {
132         while( resources.hasMoreElements() )
133         {
134             result.add( resources.nextElement() );
135         }
136     }
137
138     /**
139      * Overide findResource to search through all classloaders.
140      *
141      * @param name the resources to search for
142      * @return the resource URL
143      * @see ClassLoader#findResource
144      */

145     protected URL JavaDoc findResource( final String JavaDoc name )
146     {
147         for( int i = 0; i < m_classLoaders.length; i++ )
148         {
149             final URL JavaDoc resource = m_classLoaders[ i ].getResource( name );
150             if( null != resource )
151             {
152                 return resource;
153             }
154         }
155         return super.findResource( name );
156     }
157 }
158
Popular Tags