1 17 package org.apache.geronimo.system.repository; 18 19 import java.io.InputStream ; 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.FileOutputStream ; 23 import java.io.OutputStream ; 24 import java.util.zip.ZipInputStream ; 25 import java.util.zip.ZipEntry ; 26 27 import org.apache.geronimo.kernel.repository.ArtifactTypeHandler; 28 import org.apache.geronimo.kernel.repository.Artifact; 29 import org.apache.geronimo.kernel.repository.FileWriteMonitor; 30 import org.apache.geronimo.kernel.config.IOUtil; 31 32 35 public class UnpackArtifactTypeHandler implements ArtifactTypeHandler { 36 private final static int TRANSFER_NOTIFICATION_SIZE = 10240; private final static int TRANSFER_BUF_SIZE = 10240; 39 public void install(InputStream source, int size, Artifact artifact, FileWriteMonitor monitor, File target) throws IOException { 40 File parent = target.getParentFile(); 42 if (!parent.exists() && !parent.mkdirs()) { 43 throw new RuntimeException ("Unable to create directory " + parent.getAbsolutePath()); 44 } 45 46 if (monitor != null) { 48 monitor.writeStarted(artifact.toString(), size); 49 } 50 51 int total = 0; 52 ZipInputStream in = new ZipInputStream (source); 53 try { 54 55 int threshold = UnpackArtifactTypeHandler.TRANSFER_NOTIFICATION_SIZE; 56 byte[] buffer = new byte[TRANSFER_BUF_SIZE]; 57 58 for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) { 59 File file = new File (target, entry.getName()); 60 if (entry.isDirectory()) { 61 file.mkdirs(); 62 } else { 63 if (!entry.getName().equals("META-INF/startup-jar")) { 64 file.getParentFile().mkdirs(); 65 OutputStream out = new FileOutputStream (file); 66 try { 67 int count; 68 while ((count = in.read(buffer)) > 0) { 69 out.write(buffer, 0, count); 70 if (monitor != null) { 71 total += count; 72 if (total > threshold) { 73 threshold += UnpackArtifactTypeHandler.TRANSFER_NOTIFICATION_SIZE; 74 monitor.writeProgress(total); 75 } 76 } 77 } 78 } finally { 79 IOUtil.flush(out); 80 out.close(); 81 } 82 in.closeEntry(); 83 } 84 } 85 } 86 } catch (IOException e) { 87 IOUtil.recursiveDelete(target); 88 throw e; 89 } finally { 90 in.close(); 91 if (monitor != null) { 92 monitor.writeComplete(total); 93 } 94 } 95 } 96 } 97 | Popular Tags |