1 4 package uk.ac.roe.antigen.ant; 5 6 import java.io.BufferedInputStream ; 7 import java.io.File ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.io.BufferedOutputStream ; 13 import java.net.JarURLConnection ; 14 import java.net.URL ; 15 import java.util.Enumeration ; 16 import java.util.jar.JarEntry ; 17 import java.util.jar.JarFile ; 18 import java.util.logging.Logger ; 19 20 import uk.ac.roe.antigen.utils.CopyableFile; 21 22 23 30 public class BuildGrabber { 31 34 private static final Logger logger = Logger.getLogger(BuildGrabber.class.getName()); 35 36 37 private CopyableFile tmpDir; 38 39 public static void main(String [] args) throws Exception { 40 BuildGrabber grabber = new BuildGrabber(); 41 try { 42 CopyableFile tmp = grabber.tmpDir; 43 logger.fine("Temporary directory: " + tmp); 44 URL jarUrl = new URL ( 46 "file:///C:/Documents and Settings/jdt/.maven/repository/org.astrogrid/wars/astrogrid-portal-0.7-b015p.war"); 47 File copiedDir = grabber.grab("/foo"); 49 logger.fine("Copied dir was " + copiedDir); 50 Thread.sleep(30000); 51 } finally { 52 System.out.println("Deleted? " + grabber.deleteTmp()); 53 } 54 } 55 56 63 public CopyableFile grab(File dir) throws IOException { 64 new CopyableFile(dir).copyTo(tmpDir); 65 return tmpDir; 66 } 67 68 76 public CopyableFile grab(URL url) throws IOException { 77 String simpleUrl = url.toExternalForm(); 78 logger.fine("Grabbing from " + simpleUrl); 79 JarURLConnection jarUrl = (JarURLConnection ) new URL ("jar:" + simpleUrl 80 + "!/").openConnection(); 81 logger.fine("Opening connection to " + jarUrl); 82 JarFile jarFile = jarUrl.getJarFile(); 83 return grab(jarFile); 84 } 85 86 95 public CopyableFile grab(JarFile jar) throws IOException { 96 Enumeration entries = jar.entries(); 97 while (entries.hasMoreElements()) { 98 JarEntry entry = (JarEntry ) entries.nextElement(); 99 logger.fine("Entry " + entry); 100 101 File copiedFileEntry = new File (tmpDir, entry.getName()); 102 logger.fine("Copying to " + copiedFileEntry.getAbsolutePath()); 103 104 if (entry.isDirectory()) { 105 copiedFileEntry.mkdirs(); 106 } else { 107 InputStream is = new BufferedInputStream (jar 108 .getInputStream(entry)); 109 copiedFileEntry.createNewFile(); 110 FileOutputStream os = new FileOutputStream (copiedFileEntry); 111 112 copyStreams(is, os); 113 } 114 115 } 116 return tmpDir; 117 } 118 119 private void copyStreams(InputStream in, OutputStream out) 120 throws IOException { 121 int data = -1; 122 while ((data = in.read()) != -1) { 123 out.write(data); 124 } 125 out.close(); 126 127 in.close(); 128 } 129 130 138 public CopyableFile grab(String path) throws IOException { 139 if (path.startsWith("/")) { 143 path = path.substring(1); 144 } 145 File tmpJarFile = File.createTempFile("build", ".jar"); 146 tmpJarFile.deleteOnExit(); 147 JarFile installSource = new JarFile (System.getProperty("java.class.path")); 148 logger.fine("path: "+path); 149 File antBuildJar = unzipFile(path,installSource, tmpDir, false); 150 158 JarFile jar = new JarFile (antBuildJar); 159 unzipFile(null,jar,tmpDir,true); 160 179 return tmpDir; 180 181 } 182 183 190 private File unzipFile(String fullPathFileName, JarFile srcJar, File destDir, boolean useFullPath) { 191 int BUFFER = 16384; 192 BufferedOutputStream dest = null; 193 BufferedInputStream is = null; 194 JarEntry entry; 195 String newFullPathFileName = null; 196 Enumeration e = srcJar.entries(); 197 try { 198 while(e.hasMoreElements()) { 199 entry = (JarEntry ) e.nextElement(); 200 if (fullPathFileName != null && !(entry.getName().equals(fullPathFileName) )) { 201 continue; 202 } 203 204 logger.fine("Extracting: " +entry); 205 is = new BufferedInputStream (srcJar.getInputStream(entry)); 206 int count; 207 byte data[] = new byte[BUFFER]; 208 int lastIndexOfSlashPlusOne = entry.getName().lastIndexOf('/')+1; 209 String path = null; 210 String fileName = null; 211 if (lastIndexOfSlashPlusOne == 0) { 212 path = ""; 213 } else { 214 path = entry.getName().substring(0,lastIndexOfSlashPlusOne); 215 String sep = File.separator; 216 if (sep.equals("\\")) { 217 sep = "\\\\"; 218 } 219 path = path.replaceAll("/",sep); 220 } 221 fileName = entry.getName().substring(lastIndexOfSlashPlusOne); 222 String newEntryName = destDir.getAbsolutePath()+File.separator+path+fileName; 223 if (!useFullPath) { 224 newEntryName = destDir.getAbsolutePath()+File.separator+fileName; 225 } 226 if (entry.isDirectory()) { 227 File entryFile = new File (newEntryName); 228 if (!entryFile.exists()) { 229 entryFile.mkdirs(); 230 } 231 continue; 232 } 233 newFullPathFileName = newEntryName; 234 235 FileOutputStream fos = new FileOutputStream (newFullPathFileName); 236 dest = new 237 BufferedOutputStream (fos, BUFFER); 238 while ((count = is.read(data, 0, BUFFER)) != -1) { 239 dest.write(data, 0, count); 240 } 241 dest.flush(); 242 dest.close(); 243 is.close(); 244 if (fullPathFileName != null) { 245 return new File (newFullPathFileName); 246 } 247 } 248 } catch(Exception ex) { 249 logger.warning(ex.getMessage()); 250 ex.printStackTrace(); 251 } 252 253 return null; 254 } 255 256 262 public boolean deleteTmp() { 263 264 return tmpDir.recursivelyDelete(); 265 } 266 267 273 public BuildGrabber() throws IOException { 274 String systemTemp = System.getProperty("java.io.tmpdir"); 275 logger.fine("System Temp Dir " + systemTemp); 276 277 tmpDir = new CopyableFile(File.createTempFile("antigen", "", new File ( 278 systemTemp))); 279 logger.info("Build Dir " + tmpDir); 280 tmpDir.delete(); 282 tmpDir.mkdirs(); 283 tmpDir.deleteOnExit(); 284 } 285 292 public BuildGrabber(File dir) throws IOException { 293 tmpDir = new CopyableFile(dir); 294 logger.info("Build Dir " + tmpDir); 295 } 296 } | Popular Tags |