1 36 package org.ungoverned.oscar.installer.artifact; 37 38 import java.io.*; 39 import java.util.Map ; 40 import java.util.jar.JarEntry ; 41 import java.util.jar.JarInputStream ; 42 43 import org.ungoverned.oscar.installer.Install; 44 import org.ungoverned.oscar.installer.Status; 45 import org.ungoverned.oscar.installer.StringProperty; 46 47 public abstract class AbstractJarArtifact extends AbstractArtifact 48 { 49 public AbstractJarArtifact(StringProperty sourceName) 50 { 51 this(sourceName, null); 52 } 53 54 public AbstractJarArtifact(StringProperty sourceName, StringProperty destDir) 55 { 56 this(sourceName, destDir, false); 57 } 58 59 public AbstractJarArtifact( 60 StringProperty sourceName, StringProperty destDir, boolean localize) 61 { 62 super(sourceName, destDir, localize); 63 } 64 65 public boolean process(Status status, Map propMap) 66 { 67 try 68 { 69 InputStream is = getInputStream(status); 70 71 if (is == null) 72 { 73 return true; 74 } 75 76 JarInputStream jis = new JarInputStream (is); 77 status.setText("Extracting..."); 78 unjar(jis, propMap); 79 jis.close(); 80 } 81 catch (Exception ex) 82 { 83 System.err.println(this); 84 System.err.println(ex); 85 return false; 86 } 87 88 return true; 89 } 90 91 protected void unjar(JarInputStream jis, Map propMap) 92 throws IOException 93 { 94 String installDir = 95 ((StringProperty) propMap.get(Install.INSTALL_DIR)).getStringValue(); 96 97 for (JarEntry je = jis.getNextJarEntry(); 99 je != null; 100 je = jis.getNextJarEntry()) 101 { 102 if (je.getName().startsWith("/")) 103 { 104 throw new IOException("JAR resource cannot contain absolute paths."); 105 } 106 107 File target = 108 new File(installDir, getDestinationDirectory().getStringValue()); 109 target = new File(target, je.getName()); 110 111 if (je.isDirectory()) 113 { 114 if (!target.exists()) 115 { 116 if (!target.mkdirs()) 117 { 118 throw new IOException("Unable to create target directory: " 119 + target); 120 } 121 } 122 continue; 124 } 125 126 int lastIndex = je.getName().lastIndexOf('/'); 127 String name = (lastIndex >= 0) ? 128 je.getName().substring(lastIndex + 1) : je.getName(); 129 String destination = (lastIndex >= 0) ? 130 je.getName().substring(0, lastIndex) : ""; 131 132 destination = destination.replace('/', File.separatorChar); 134 135 if (localize()) 136 { 137 copyAndLocalize(jis, installDir, name, destination, propMap); 138 } 139 else 140 { 141 copy(jis, installDir, name, destination); 142 } 143 } 144 } 145 } | Popular Tags |