1 22 package org.jboss.virtual.plugins.context.jar; 23 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.ObjectInputStream ; 30 import java.net.MalformedURLException ; 31 import java.net.URISyntaxException ; 32 import java.net.URL ; 33 import java.util.jar.JarEntry ; 34 import java.util.jar.JarFile ; 35 36 import org.jboss.virtual.spi.VFSContext; 37 import org.jboss.virtual.spi.VirtualFileHandler; 38 39 46 public class NestedJarHandler extends AbstractJarHandler 47 { 48 49 private static final long serialVersionUID = 1L; 50 51 52 private transient JarEntry entry; 53 54 55 private transient File temp; 56 57 66 private static JarFile createTempJar(File temp, JarFile parentJar, JarEntry entry) throws IOException 67 { 68 InputStream inputStream = parentJar.getInputStream(entry); 69 try 70 { 71 FileOutputStream outputStream = new FileOutputStream (temp); 72 try 73 { 74 byte[] buffer = new byte[8096]; 75 int read = inputStream.read(buffer); 76 while (read != -1) 77 { 78 outputStream.write(buffer, 0, read); 79 read = inputStream.read(buffer); 80 } 81 } 82 finally 83 { 84 outputStream.close(); 85 } 86 } 87 finally 88 { 89 try 90 { 91 inputStream.close(); 92 } 93 catch (IOException ignored) 94 { 95 } 96 } 97 98 return new JarFile (temp); 99 } 100 101 112 public NestedJarHandler(VFSContext context, VirtualFileHandler parent, 113 JarFile parentJar, JarEntry entry, URL url, String entryName) 114 throws IOException 115 { 116 super(context, parent, url, entryName); 117 118 119 try 120 { 121 temp = File.createTempFile("nestedjar", null); 122 temp.deleteOnExit(); 123 124 initJarFile(createTempJar(temp, parentJar, entry)); 125 } 126 catch (IOException original) 127 { 128 IOException e = new IOException ("Error opening jar file: " + url + " reason=" + original.getMessage()); 130 e.setStackTrace(original.getStackTrace()); 131 throw e; 132 } 133 134 this.entry = entry; 135 } 136 137 142 protected JarEntry getEntry() 143 { 144 checkClosed(); 145 return entry; 146 } 147 148 @Override 149 public long getLastModified() throws IOException 150 { 151 return getEntry().getTime(); 152 } 153 154 @Override 155 public long getSize() throws IOException 156 { 157 return getEntry().getSize(); 158 } 159 160 163 @Override 164 public InputStream openStream() throws IOException 165 { 166 FileInputStream fis = new FileInputStream (temp); 167 return fis; 168 } 169 170 @Override 171 public URL toURL() throws MalformedURLException , URISyntaxException 172 { 173 return new URL ("jar:" + temp.toURL() + "!/"); 174 } 175 176 183 private void readObject(ObjectInputStream in) 184 throws IOException , ClassNotFoundException 185 { 186 JarFile parentJar = super.getJar(); 187 entry = parentJar.getJarEntry(getName()); 189 temp = File.createTempFile("nestedjar", null); 190 temp.deleteOnExit(); 191 createTempJar(temp, parentJar, entry); 192 super.initJarFile(parentJar); 194 } 195 196 } 197 | Popular Tags |