KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > moduleloader > JarResourceSource


1 /*
2  * ModuleLoader - A generic, policy-driven class loader.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.moduleloader;
37
38 import java.io.*;
39 import java.util.jar.JarFile JavaDoc;
40 import java.util.zip.ZipEntry JavaDoc;
41
42 /**
43  * <p>
44  * This class implements a <tt>ResourceSource</tt> for retrieving resources
45  * from a JAR file. The approach used by this implementation is to defer
46  * opening the JAR file until a request for a resource is made.
47  * </p>
48  * @see org.ungoverned.moduleloader.ResourceSource
49 **/

50 public class JarResourceSource implements ResourceSource
51 {
52     private static final int BUFSIZE = 4096;
53
54     private File JavaDoc m_file = null;
55     private JarFile JavaDoc m_jarFile = null;
56     private boolean m_opened = false;
57
58     /**
59      * <p>
60      * Constructs an instance using the specified file name as the source
61      * of the JAR file.
62      * </p>
63      * @param fileName the name of the JAR file to be used as the source.
64     **/

65     public JarResourceSource(String JavaDoc fileName)
66     {
67         m_file = new File JavaDoc(fileName);
68     }
69
70     /**
71      * <p>
72      * Constructs an instance using the specified file as the source
73      * of the JAR file.
74      * </p>
75      * @param file the JAR file to be used as the source.
76     **/

77     public JarResourceSource(File JavaDoc file)
78     {
79         m_file = file;
80     }
81
82     /**
83      * <p>
84      * Closes the JAR file if it has not already been closed.
85      * <p>
86     **/

87     protected void finalize()
88     {
89         if (m_jarFile != null)
90         {
91             try {
92                 m_jarFile.close();
93             } catch (IOException ex) {
94                 // Not much we can do, so ignore it.
95
}
96         }
97     }
98
99     /**
100      * <p>
101      * This method initializes the resource source. Since opening
102      * the JAR file is deferred until a request for a resource is
103      * actually made, this method really only sets a flag indicating
104      * that the resource source has been initialized.
105      * <p>
106     **/

107     public void open()
108     {
109         m_opened = true;
110     }
111
112     /**
113      * <p>
114      * This method deinitializes the resource source by closing
115      * the associated JAR file if it is open.
116      * <p>
117     **/

118     public synchronized void close()
119     {
120         try {
121             if (m_jarFile != null)
122             {
123                 m_jarFile.close();
124             }
125         } catch (Exception JavaDoc ex) {
126             System.err.println("JarResourceSource: " + ex);
127         }
128
129         m_jarFile = null;
130         m_opened = false;
131     }
132
133     // JavaDoc comments are copied from ResourceSource.
134
public synchronized boolean hasResource(String JavaDoc name) throws IllegalStateException JavaDoc
135     {
136         if (!m_opened)
137         {
138             throw new IllegalStateException JavaDoc("JarResourceSource is not open");
139         }
140
141         // Open JAR file if not already opened.
142
if (m_jarFile == null)
143         {
144             try {
145                 openJarFile();
146             } catch (IOException ex) {
147                 System.err.println("JarResourceSource: " + ex);
148                 return false;
149             }
150         }
151
152         try {
153             ZipEntry JavaDoc ze = m_jarFile.getEntry(name);
154             return ze != null;
155         } catch (Exception JavaDoc ex) {
156             return false;
157         } finally {
158         }
159     }
160
161     // JavaDoc comments are copied from ResourceSource.
162
public synchronized byte[] getBytes(String JavaDoc name) throws IllegalStateException JavaDoc
163     {
164         if (!m_opened)
165         {
166             throw new IllegalStateException JavaDoc("JarResourceSource is not open");
167         }
168
169         // Open JAR file if not already opened.
170
if (m_jarFile == null)
171         {
172             try {
173                 openJarFile();
174             } catch (IOException ex) {
175                 System.err.println("JarResourceSource: " + ex);
176                 return null;
177             }
178         }
179
180         // Get the embedded resource.
181
InputStream is = null;
182         ByteArrayOutputStream baos = null;
183
184         try {
185             ZipEntry JavaDoc ze = m_jarFile.getEntry(name);
186             if (ze == null)
187             {
188                 return null;
189             }
190             is = m_jarFile.getInputStream(ze);
191             if (is == null)
192             {
193                 return null;
194             }
195             baos = new ByteArrayOutputStream(BUFSIZE);
196             byte[] buf = new byte[BUFSIZE];
197             int n = 0;
198             while ((n = is.read(buf, 0, buf.length)) >= 0)
199             {
200                 baos.write(buf, 0, n);
201             }
202             return baos.toByteArray();
203
204         } catch (Exception JavaDoc ex) {
205             return null;
206         } finally {
207             try {
208                 if (baos != null) baos.close();
209             } catch (Exception JavaDoc ex) {
210             }
211             try {
212                 if (is != null) is.close();
213             } catch (Exception JavaDoc ex) {
214             }
215         }
216     }
217
218     private void openJarFile() throws IOException
219     {
220         if (m_jarFile == null)
221         {
222             m_jarFile = new JarFile JavaDoc(m_file);
223         }
224     }
225
226     public String JavaDoc toString()
227     {
228         return "JAR " + m_file.getPath();
229     }
230 }
Popular Tags