KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > archive > EZBJarArchiveImpl


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EZBJarArchiveImpl.java 1026 2006-08-04 14:54:10Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container.archive;
27
28 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL;
29 import static org.objectweb.easybeans.util.url.URLUtils.fileToURL2;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.jar.JarEntry JavaDoc;
40 import java.util.jar.JarFile JavaDoc;
41 import java.util.zip.ZipEntry JavaDoc;
42
43 import org.objectweb.easybeans.api.EZBArchive;
44 import org.objectweb.easybeans.api.EZBArchiveException;
45 import org.objectweb.easybeans.util.url.URLUtilsException;
46 /**
47  * Creates wrapper around jar file.
48  * @author Florent Benoit
49  */

50 public class EZBJarArchiveImpl implements EZBArchive {
51
52     /**
53      * Internal resource used as archive.
54      */

55     private JarFile JavaDoc jarFile = null;
56
57     /**
58      * Internal resource used as archive.
59      */

60     private File JavaDoc file = null;
61
62     /**
63      * Creates new instance of an EZBArchive for a jar file.
64      * @param file the given jar file.
65      */

66     protected EZBJarArchiveImpl(final File JavaDoc file) {
67         this.file = file;
68
69     }
70
71     /**
72      * Init an archive and create the associated jar file object. Close() method
73      * needs to be called !.
74      * @throws EZBArchiveException if init fails
75      */

76     private void init() throws EZBArchiveException {
77         try {
78             jarFile = new JarFile JavaDoc(file);
79         } catch (IOException JavaDoc e) {
80             throw new EZBArchiveException("Invalid file", e);
81         }
82     }
83
84     /**
85      * @return a description of this archive. This name could be used in logger
86      * info.
87      */

88     public String JavaDoc getName() {
89         return file.getPath();
90     }
91
92     /**
93      * Close the underlying Resource.
94      * @return Returns <code>true</code> if the close was succesful,
95      * <code>false</code> otherwise.
96      */

97     public boolean close() {
98         // Never accessed.
99
if (jarFile == null) {
100             return true;
101         }
102         try {
103             jarFile.close();
104         } catch (IOException JavaDoc e) {
105             return false;
106         }
107         return true;
108     }
109
110     /**
111      * @param resourceName The resource name to be looked up.
112      * @return Returns the resource URL if the resource has been found. null
113      * otherwise.
114      * @throws EZBArchiveException if method fails.
115      */

116     public URL getResource(final String JavaDoc resourceName) throws EZBArchiveException {
117         URL url = null;
118         // Open jar file.
119
init();
120         try {
121             JarEntry JavaDoc jarEntry = jarFile.getJarEntry(resourceName);
122             if (jarEntry != null) {
123                 try {
124                     url = new URL("jar:" + fileToURL(file) + "!/" + resourceName);
125                 } catch (MalformedURLException JavaDoc e) {
126                     throw new EZBArchiveException("Invalid url", e);
127                 }
128             }
129         } finally {
130             close();
131         }
132         return url;
133     }
134
135     /**
136      * @return Returns an Iterator of Resource's URL.
137      * @throws EZBArchiveException if method fails.
138      */

139     public Iterator JavaDoc<URL> getResources() throws EZBArchiveException {
140         List JavaDoc<URL> listResources = new ArrayList JavaDoc<URL>();
141         init();
142         try {
143             Enumeration JavaDoc<? extends ZipEntry JavaDoc> en = jarFile.entries();
144             while (en.hasMoreElements()) {
145                 ZipEntry JavaDoc zipEntry = en.nextElement();
146                 String JavaDoc name = zipEntry.getName();
147                 try {
148                     listResources.add(new URL("jar:" + fileToURL(file) + "!/" + name));
149                 } catch (MalformedURLException JavaDoc e) {
150                     throw new EZBArchiveException("Invalid url", e);
151                 }
152             }
153         } finally {
154             close();
155         }
156         return listResources.iterator();
157     }
158
159     /**
160      * @param resourceName The resource name to be looked up.
161      * @return Returns an Iterator of matching resources.
162      * @throws EZBArchiveException if method fails.
163      */

164     public Iterator JavaDoc<URL> getResources(final String JavaDoc resourceName) throws EZBArchiveException {
165         List JavaDoc<URL> listResources = new ArrayList JavaDoc<URL>();
166         URL url = getResource(resourceName);
167         if (url != null) {
168             listResources.add(url);
169         }
170
171         return listResources.iterator();
172     }
173
174     /**
175      * @return Returns the resource URL.
176      * @throws EZBArchiveException if method fails.
177      */

178     public URL getURL() throws EZBArchiveException {
179         try {
180             return fileToURL2(file);
181         } catch (URLUtilsException e) {
182             throw new EZBArchiveException("Invalid url", e);
183         }
184     }
185
186     /**
187      * Is that the given object is equals to our instance.
188      * @param o the object to compare.
189      * @return true if equals, else false.
190      */

191     @Override JavaDoc
192     public boolean equals(final Object JavaDoc o) {
193         if (!(o instanceof EZBJarArchiveImpl)) {
194             return false;
195         }
196         EZBJarArchiveImpl other = (EZBJarArchiveImpl) o;
197         return this.file.equals(other.file);
198     }
199
200     /**
201      * Gets hashcode for this object.
202      * @return hash code.
203      */

204     @Override JavaDoc
205     public int hashCode() {
206         return file.hashCode();
207     }
208
209     /**
210      * @return string representation
211      */

212     @Override JavaDoc
213     public String JavaDoc toString() {
214         return getName();
215     }
216
217 }
218
Popular Tags