KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > clazzy > loader > JarLoader


1 package org.sapia.clazzy.loader;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.net.MalformedURLException JavaDoc;
7 import java.net.URL JavaDoc;
8 import java.util.jar.JarFile JavaDoc;
9 import java.util.zip.ZipEntry JavaDoc;
10
11 import org.sapia.clazzy.ClazzyURLStreamHandlerFactory;
12 import org.sapia.clazzy.URLEnabled;
13 import org.sapia.clazzy.Utils;
14
15 /**
16  * Implements the <code>Loader</code> interface over a jar file. The underlying jar file is opened at
17  * instantiation time. An instance of this class should be disposed of appropriately (through the
18  * <code>close()</code> method), so that the underlying jar file is properly closed.
19  *
20  * @author Yanick Duchesne
21  *
22  * <dl>
23  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 public class JarLoader implements Loader, URLEnabled{
29   
30   private File JavaDoc _jar;
31   private JarFile JavaDoc _file;
32
33   public JarLoader(File JavaDoc jar) {
34     _jar = jar;
35     try{
36       _file = new JarFile JavaDoc(jar);
37     } catch (IOException JavaDoc e){
38       //noop;
39
}
40   }
41   
42   public File JavaDoc getJarFile(){
43     return _jar;
44   }
45   
46   /**
47    * @return the <code>URL</code> of the <code>File</code> to which this
48    * instance corresponds.
49    *
50    * @throws MalformedURLException
51    */

52   public URL JavaDoc getURL() throws MalformedURLException JavaDoc {
53     return _jar.toURL();
54   }
55   
56   /**
57    * @see org.sapia.clazzy.loader.Loader#getURL(java.lang.String)
58    */

59   public URL JavaDoc getURL(String JavaDoc resourceName) {
60     if(_file == null){
61       return null;
62     }
63     ZipEntry JavaDoc entry = _file.getEntry(resourceName);
64     if(entry == null){
65       return null;
66     }
67     try{
68       return new URL JavaDoc(ClazzyURLStreamHandlerFactory.PROTOCOL, "", _jar.getAbsolutePath()+"?"+resourceName);
69     }catch(MalformedURLException JavaDoc e){
70       return null;
71     }
72   }
73   
74   /**
75    * @see org.sapia.clazzy.loader.Loader#loadBytes(java.lang.String)
76    */

77   public byte[] loadBytes(String JavaDoc resourceName) {
78     if(_file == null){
79       return null;
80     }
81     InputStream JavaDoc entryStream = null;
82     try {
83       ZipEntry JavaDoc entry = _file.getEntry(resourceName);
84       if(entry == null) {
85         return null;
86       }
87       entryStream = _file.getInputStream(entry);
88       return Utils.streamToBytes(entryStream);
89     } catch (IOException JavaDoc e){
90       return null;
91     } finally {
92       if(entryStream != null) {
93         try {
94           entryStream.close();
95         } catch(IOException JavaDoc e) {
96         }
97       }
98     }
99   }
100
101   /**
102    * @see org.sapia.clazzy.loader.Loader#close()
103    */

104   public void close(){
105     if(_file != null){
106       try{
107         _file.close();
108       }catch(IOException JavaDoc e){
109         //noop
110
}
111     }
112   }
113
114 }
115
Popular Tags