KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.clazzy;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.util.ResourceBundle JavaDoc;
9
10 import org.sapia.clazzy.loader.JarLoader;
11
12 /**
13  * This class overrides the <code>BaseClassLoader</code> class and search
14  * classes in a given jar file. It is provided as an alternative to the
15  * implementation that comes with the JDK, which locks the underlying
16  * jar files (in fact keeping the files open).
17  * <p>
18  * Under some OS (WIN!"$D%?), this prevents the jar files from being deleted
19  * (for example, in the case of hot-deployments).
20  * <p>
21  * An instance of this class works around this limitation by internally using
22  * a <code>JarLoader</code> instance, which opens the jar file at instantiation
23  * time, and allows closing the said jar file when it is not needed anymore.
24  * <p>
25  * Client applications should call <code>close()</code> on an instance of this class to ensure
26  * that the underlying <code>JarLoader</code> is disposed of cleanly (i.e.: that the jar file
27  * it holds is closed).
28  *
29  * @author Yanick Duchesne
30  *
31  * @see org.sapia.clazzy.loader.JarLoader
32  * @see org.sapia.clazzy.ClazzyURLConnection
33  * @see org.sapia.clazzy.ClazzyURLStreamHandlerFactory
34  *
35  * <dl>
36  * <dt><b>Copyright: </b>
37  * <dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open
38  * Source Software </a>. All Rights Reserved.</dd>
39  * </dt>
40  * <dt><b>License: </b>
41  * <dd>Read the license.txt file of the jar or visit the <a
42  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
43  * OSS web site</dd>
44  * </dt>
45  * </dl>
46  */

47 public class JarClassLoader extends BaseClassLoader implements Consts{
48
49   private JarLoader _jar;
50   
51   ResourceBundle JavaDoc bundle;
52
53   public JarClassLoader(File JavaDoc jar) {
54     _jar = new JarLoader(jar);
55   }
56
57   public JarClassLoader(ClassLoader JavaDoc parent, File JavaDoc jar) {
58     super(parent);
59     _jar = new JarLoader(jar);
60   }
61
62   /**
63    * @return the <code>File</code> corresponding to the archive in which this
64    * instance looks up.
65    */

66   public File JavaDoc getFile() {
67     return _jar.getJarFile();
68   }
69
70   /**
71    * @see java.lang.ClassLoader#findClass(java.lang.String)
72    */

73   protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
74     if(!_jar.getJarFile().exists()) {
75       throw new ClassNotFoundException JavaDoc(name);
76     }
77     
78     String JavaDoc resourceName = name.replace('.', '/') + ".class";
79     byte[] classBytes = _jar.loadBytes(resourceName);
80     if(classBytes == null){
81       throw new ClassNotFoundException JavaDoc(name);
82     }
83     String JavaDoc pckg = Utils.getPackageNameFor(name);
84     if(pckg != null && getPackage(pckg) == null){
85       definePackage(pckg,
86                     PACKAGE_IMPL_TITLE,
87                     PACKAGE_IMPL_VERSION,
88                     PACKAGE_IMPL_VENDOR,
89                     PACKAGE_IMPL_TITLE,
90                     PACKAGE_IMPL_VERSION,
91                     PACKAGE_IMPL_VENDOR,
92                     null);
93     }
94     return defineClass(name, classBytes, 0, classBytes.length);
95   }
96   
97   /**
98    * @param name the name of the resource whose stream should be returned.
99    * @return an <code>InputStream</code>.
100    */

101   public InputStream JavaDoc findResourceAsStream(String JavaDoc name){
102     if(!_jar.getJarFile().exists()) {
103       return null;
104     }
105     byte[] toReturn = _jar.loadBytes(name);
106     if(toReturn == null){
107       return null;
108     }
109     return new ByteArrayInputStream JavaDoc(toReturn);
110   }
111   
112   /**
113    * @see java.lang.ClassLoader#findResource(java.lang.String)
114    */

115   protected URL JavaDoc findResource(String JavaDoc name) {
116     try{
117       return new URL JavaDoc(ClazzyURLStreamHandlerFactory.PROTOCOL, "", _jar.getJarFile().getAbsolutePath()+"?"+name);
118     }catch(MalformedURLException JavaDoc e){
119       return null;
120     }
121   }
122   
123   /**
124    * @see JarLoader#close()
125    */

126   public void close(){
127     _jar.close();
128   }
129   
130 }
131
Popular Tags