KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > clazzy > CompositeClassLoader


1 package org.sapia.clazzy;
2
3 import java.io.File JavaDoc;
4 import java.net.MalformedURLException JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collections JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.ResourceBundle JavaDoc;
10
11 import org.sapia.clazzy.loader.FileSystemLoader;
12 import org.sapia.clazzy.loader.JarLoader;
13 import org.sapia.clazzy.loader.Loader;
14
15 /**
16  * This classloader delegates class lookups the <code>Loader</code> instances
17  * that it encapsulates. Children are added through the <code>addLoader()</code>
18  * or <code>addFile</code> method.
19  * <p>
20  * The <code>CompositeClassLoaderBuilder</code> can conveniently be used to create
21  * an instance of this class.
22  *
23  * @see org.sapia.clazzy.CompositeClassLoaderBuilder
24  *
25  * @author Yanick Duchesne
26  *
27  * <dl>
28  * <dt><b>Copyright: </b>
29  * <dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open
30  * Source Software </a>. All Rights Reserved.</dd>
31  * </dt>
32  * <dt><b>License: </b>
33  * <dd>Read the license.txt file of the jar or visit the <a
34  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
35  * OSS web site</dd>
36  * </dt>
37  * </dl>
38  */

39 public class CompositeClassLoader extends BaseClassLoader implements Consts{
40
41   private List JavaDoc _loaders = Collections.synchronizedList(new ArrayList JavaDoc());
42   private LoaderSelector _selector;
43   private ResourceBundle JavaDoc _bundle;
44
45   public CompositeClassLoader(ClassLoader JavaDoc parent, LoaderSelector selector) {
46     super(parent);
47     _selector = selector;
48   }
49
50   public CompositeClassLoader(LoaderSelector selector) {
51     _selector = selector;
52   }
53   
54   /**
55    * @return the URLs to which this instance "points".
56    */

57   public URL JavaDoc[] getURLs(){
58     List JavaDoc urls = new ArrayList JavaDoc();
59     Loader loader;
60     for(int i = 0; i < _loaders.size(); i++){
61       loader = (Loader)_loaders.get(i);
62       if(loader instanceof URLEnabled){
63         try{
64           urls.add(((URLEnabled)loader).getURL());
65         }catch(MalformedURLException JavaDoc e){
66         }
67       }
68     }
69     return (URL JavaDoc[])urls.toArray(new URL JavaDoc[urls.size()]);
70   }
71
72   /**
73    * @see java.lang.ClassLoader#findClass(java.lang.String)
74    */

75   public Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
76     Class JavaDoc clazz = null;
77     Loader loader;
78     byte[] classBytes = null;
79     String JavaDoc resourceName = name.replace('.', '/')+".class";
80     synchronized(_loaders) {
81       for(int i = 0; i < _loaders.size(); i++) {
82         loader = (Loader) _loaders.get(i);
83         if(_selector.acceptsClass(name, loader)) {
84           classBytes = loader.loadBytes(resourceName);
85           if(classBytes != null){
86             break;
87           }
88         }
89       }
90     }
91     if(classBytes == null) {
92       throw new ClassNotFoundException JavaDoc(name);
93     }
94     String JavaDoc pckg = Utils.getPackageNameFor(name);
95     if(pckg != null && getPackage(pckg) == null){
96       definePackage(pckg,
97                     PACKAGE_SPEC_TITLE,
98                     PACKAGE_SPEC_VERSION,
99                     PACKAGE_SPEC_VENDOR,
100                     PACKAGE_IMPL_TITLE,
101                     PACKAGE_IMPL_VERSION,
102                     PACKAGE_IMPL_VENDOR,
103                     null);
104     }
105     return super.defineClass(name, classBytes, 0, classBytes.length);
106   }
107   
108   /**
109    * @see java.lang.ClassLoader#findResource(java.lang.String)
110    */

111   protected URL JavaDoc findResource(String JavaDoc name) {
112     Loader loader;
113     URL JavaDoc toReturn = null;
114     synchronized(_loaders) {
115       for(int i = 0; i < _loaders.size(); i++) {
116         loader = (Loader) _loaders.get(i);
117         if(_selector.acceptsResource(name, loader)) {
118           toReturn = loader.getURL(name);
119           if(toReturn != null){
120             break;
121           }
122         }
123       }
124     }
125     return toReturn;
126   }
127   
128   /**
129    * @param loader
130    * the <code>Loader</code> to add to this instance.
131    */

132   public void addLoader(Loader loader) {
133     _loaders.add(loader);
134   }
135   
136   /**
137    * Adds the file object corresponding to a directory of classes or
138    * a jar to this instance.
139    * <p>
140    * Internally, the method creates either a <code>FileSystemLoader</code>
141    * (if the given file object corresponds to a directory) or a <code>JarLoader</code>.
142    * @param file a <code>File</code>
143    */

144   public void addPath(File JavaDoc file){
145     if(!file.exists()){
146       return;
147     }
148     if(file.isDirectory()){
149       addLoader(new FileSystemLoader(file));
150     }
151     else{
152       addLoader(new JarLoader(file));
153     }
154   }
155   
156   /**
157    * Releases all resources that this instance.
158    */

159   public void close(){
160     Loader loader;
161     for(int i = 0; i < _loaders.size(); i++){
162       loader = (Loader)_loaders.get(i);
163       try{
164         loader.close();
165       }catch(Exception JavaDoc e){}
166     }
167   }
168   
169 }
170
Popular Tags