1 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 ; 32 import java.io.IOException ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.util.ArrayList ; 36 import java.util.Enumeration ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.jar.JarEntry ; 40 import java.util.jar.JarFile ; 41 import java.util.zip.ZipEntry ; 42 43 import org.objectweb.easybeans.api.EZBArchive; 44 import org.objectweb.easybeans.api.EZBArchiveException; 45 import org.objectweb.easybeans.util.url.URLUtilsException; 46 50 public class EZBJarArchiveImpl implements EZBArchive { 51 52 55 private JarFile jarFile = null; 56 57 60 private File file = null; 61 62 66 protected EZBJarArchiveImpl(final File file) { 67 this.file = file; 68 69 } 70 71 76 private void init() throws EZBArchiveException { 77 try { 78 jarFile = new JarFile (file); 79 } catch (IOException e) { 80 throw new EZBArchiveException("Invalid file", e); 81 } 82 } 83 84 88 public String getName() { 89 return file.getPath(); 90 } 91 92 97 public boolean close() { 98 if (jarFile == null) { 100 return true; 101 } 102 try { 103 jarFile.close(); 104 } catch (IOException e) { 105 return false; 106 } 107 return true; 108 } 109 110 116 public URL getResource(final String resourceName) throws EZBArchiveException { 117 URL url = null; 118 init(); 120 try { 121 JarEntry jarEntry = jarFile.getJarEntry(resourceName); 122 if (jarEntry != null) { 123 try { 124 url = new URL("jar:" + fileToURL(file) + "!/" + resourceName); 125 } catch (MalformedURLException e) { 126 throw new EZBArchiveException("Invalid url", e); 127 } 128 } 129 } finally { 130 close(); 131 } 132 return url; 133 } 134 135 139 public Iterator <URL> getResources() throws EZBArchiveException { 140 List <URL> listResources = new ArrayList <URL>(); 141 init(); 142 try { 143 Enumeration <? extends ZipEntry > en = jarFile.entries(); 144 while (en.hasMoreElements()) { 145 ZipEntry zipEntry = en.nextElement(); 146 String name = zipEntry.getName(); 147 try { 148 listResources.add(new URL("jar:" + fileToURL(file) + "!/" + name)); 149 } catch (MalformedURLException e) { 150 throw new EZBArchiveException("Invalid url", e); 151 } 152 } 153 } finally { 154 close(); 155 } 156 return listResources.iterator(); 157 } 158 159 164 public Iterator <URL> getResources(final String resourceName) throws EZBArchiveException { 165 List <URL> listResources = new ArrayList <URL>(); 166 URL url = getResource(resourceName); 167 if (url != null) { 168 listResources.add(url); 169 } 170 171 return listResources.iterator(); 172 } 173 174 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 191 @Override 192 public boolean equals(final Object 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 204 @Override 205 public int hashCode() { 206 return file.hashCode(); 207 } 208 209 212 @Override 213 public String toString() { 214 return getName(); 215 } 216 217 } 218 | Popular Tags |