1 23 24 package com.sun.enterprise.deployment.deploy.shared; 25 26 import java.io.*; 27 import java.util.*; 28 import java.util.jar.*; 29 import java.util.zip.*; 30 import java.util.logging.Level ; 31 import java.net.URI ; 32 33 import com.sun.enterprise.deployment.util.DOLUtils; 34 import com.sun.enterprise.deployment.deploy.shared.Archive; 35 import com.sun.enterprise.util.io.FileUtils; 36 import com.sun.enterprise.util.i18n.StringManager; 37 38 44 public class InputJarArchive extends AbstractArchive { 45 46 47 protected JarFile jarFile=null; 49 50 protected JarInputStream jarIS=null; 54 55 private String archiveUri; 57 58 private InputJarArchive parentArchive=null; 60 61 private StringManager localStrings = StringManager.getManager(getClass()); 62 63 64 67 public String getArchiveUri() { 68 return archiveUri; 69 } 70 71 75 public long getArchiveSize() throws NullPointerException , SecurityException { 76 if(getArchiveUri() == null) { 77 return -1; 78 } 79 File tmpFile = new File(getArchiveUri()); 80 return(tmpFile.length()); 81 } 82 83 87 public OutputStream addEntry(String name) throws IOException { 88 throw new UnsupportedOperationException ("Cannot write to an JAR archive open for reading"); 89 } 90 91 94 public void close() throws IOException { 95 if (jarFile!=null) { 96 jarFile.close(); 97 jarFile=null; 98 } 99 if (jarIS!=null) { 100 jarIS.close(); 101 jarIS=null; 102 } 103 } 104 105 110 public void create(String path) throws IOException { 111 throw new UnsupportedOperationException ("Cannot write to an JAR archive open for reading"); 112 } 113 114 118 public Enumeration entries() { 119 Vector entries = new Vector(); 120 121 if (parentArchive!=null) { 122 try { 123 jarIS = new JarInputStream(parentArchive.jarFile.getInputStream(parentArchive.jarFile.getJarEntry(archiveUri))); 126 JarEntry ze; 127 do { 128 ze = jarIS.getNextJarEntry(); 129 if (ze!=null && !ze.isDirectory()) { 130 entries.add(ze.getName()); 131 } 132 } while (ze!=null); 133 jarIS.close(); 134 jarIS = null; 135 } catch(IOException ioe) { 136 return null; 137 } 138 } else { 139 try { 140 if (jarFile==null) { 141 getJarFile(archiveUri); 142 } 143 } catch(IOException ioe) { 144 return entries.elements(); 145 } 146 if (jarFile==null) { 147 return entries.elements(); 148 } 149 for (Enumeration e = jarFile.entries();e.hasMoreElements();) { 150 ZipEntry ze = (ZipEntry) e.nextElement(); 151 if (!ze.isDirectory() && !ze.getName().equals(JarFile.MANIFEST_NAME)) { 152 entries.add(ze.getName()); 153 } 154 } 155 } 156 return entries.elements(); 157 } 158 159 164 public Enumeration entries(Enumeration embeddedArchives) { 165 return entries(); 167 } 168 169 174 public InputStream getEntry(String entryName) throws IOException { 175 if (jarFile!=null) { 176 ZipEntry ze = jarFile.getEntry(entryName); 177 if (ze!=null) { 178 return new BufferedInputStream(jarFile.getInputStream(ze)); 179 } else { 180 return null; 181 } 182 } else 183 if ((parentArchive != null) && (parentArchive.jarFile != null)) { 184 JarEntry je; 185 if (jarIS!=null) { 187 jarIS.close(); 188 } 189 190 JarEntry archiveJarEntry = (archiveUri != null)? parentArchive.jarFile.getJarEntry(archiveUri) : null; 193 if (archiveJarEntry == null) { 194 return null; 195 } 196 jarIS = new JarInputStream(parentArchive.jarFile.getInputStream(archiveJarEntry)); 197 do { 198 je = jarIS.getNextJarEntry(); 199 } while (je!=null && !je.getName().equals(entryName)); 200 if (je!=null) { 201 return new BufferedInputStream(jarIS); 202 } else { 203 return null; 204 } 205 } else { 206 return null; 207 } 208 } 209 210 213 public void open(String path) throws IOException { 214 archiveUri = path; 215 jarFile = getJarFile(path); 216 } 217 218 221 protected JarFile getJarFile(String path) throws IOException { 222 jarFile = null; 223 try { 224 File file = new File(path); 225 if (file.exists()) { 226 jarFile = new JarFile(file); 227 } 228 } catch(IOException e) { 229 DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.fileOpenFailure", 230 new Object []{path}); 231 String additionalInfo = localStrings.getString( 234 "enterprise.deployment.invalid_zip_file", path); 235 IOException ioe = new IOException(e.getMessage() + " -- " + additionalInfo); 236 ioe.initCause(e); 237 throw ioe; 238 } 239 return jarFile; 240 } 241 242 243 246 public Manifest getManifest() throws IOException { 247 if (jarFile!=null) { 248 return jarFile.getManifest(); 249 } 250 if (parentArchive!=null) { 251 if (jarIS!=null) { 253 jarIS.close(); 254 } 255 if (jarIS==null) { 258 jarIS = new JarInputStream(parentArchive.jarFile.getInputStream(parentArchive.jarFile.getJarEntry(archiveUri))); 259 } 260 Manifest m = jarIS.getManifest(); 261 if (m==null) { 262 java.io.InputStream is = getEntry(java.util.jar.JarFile.MANIFEST_NAME); 263 if (is!=null) { 264 m = new Manifest(); 265 m.read(is); 266 is.close(); 267 } 268 } 269 return m; 270 } 271 return null; 272 } 273 274 278 public boolean exists() { 279 return jarFile!=null; 280 } 281 282 285 public boolean delete() { 286 if (jarFile==null) { 287 return false; 288 } 289 try { 290 jarFile.close(); 291 jarFile = null; 292 } catch (IOException ioe) { 293 return false; 294 } 295 return FileUtils.deleteFile(new File(archiveUri)); 296 } 297 298 301 public boolean renameTo(String name) { 302 if (jarFile==null) { 303 return false; 304 } 305 try { 306 jarFile.close(); 307 jarFile = null; 308 } catch (IOException ioe) { 309 return false; 310 } 311 return FileUtils.renameFile(new File(archiveUri), new File(name)); 312 } 313 314 318 public AbstractArchive getEmbeddedArchive(String name) throws IOException { 319 if (jarFile!=null) { 320 InputJarArchive ija = new InputJarArchive(); 322 JarEntry je = jarFile.getJarEntry(name); 323 if (je!=null) { 324 JarInputStream jis = new JarInputStream(new BufferedInputStream(jarFile.getInputStream(je))); 325 ija.archiveUri = name; 326 ija.jarIS = jis; 327 ija.parentArchive = this; 328 return ija; 329 } 330 } 331 return null; 332 } 333 334 338 public void closeEntry(AbstractArchive os) throws IOException { 339 throw new UnsupportedOperationException ("Cannot write to an JAR archive open for reading"); 340 } 341 342 public void closeEntry() { 343 throw new UnsupportedOperationException ("Cannot write to an JAR archive open for reading"); 344 } 345 346 public URI getURI() { 347 try { 348 return ArchiveFactory.prepareArchiveURI(getArchiveUri()); 349 } catch(java.net.URISyntaxException e) { 350 return null; 351 } catch (UnsupportedEncodingException uee) { 352 return null; 353 } catch (IOException ioe) { 354 return null; 355 } 356 } 357 358 public OutputStream putNextEntry(String name) throws java.io.IOException { 359 throw new UnsupportedOperationException ("Cannot write to an JAR archive open for reading"); 360 } 361 362 } 363 | Popular Tags |