1 4 package com.tc.object.tools; 5 6 import org.apache.commons.io.FileUtils; 7 8 import java.io.File ; 9 import java.io.FileInputStream ; 10 import java.io.FileOutputStream ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.io.OutputStream ; 14 import java.util.jar.JarEntry ; 15 import java.util.jar.JarInputStream ; 16 import java.util.jar.JarOutputStream ; 17 import java.util.jar.Manifest ; 18 19 public class BootJarHandler { 20 private final boolean write_out_temp_file; 21 private final File outputFile; 22 private final File tempOutputFile; 23 private final String tempOutputFileAbsPath; 24 private final String outputFileAbsPath; 25 26 public BootJarHandler(boolean write_out_temp_file, File outputFile) { 27 this.write_out_temp_file = write_out_temp_file; 28 this.outputFile = outputFile; 29 outputFileAbsPath = this.outputFile.getAbsolutePath(); 30 if (this.write_out_temp_file) { 31 try { 32 tempOutputFile = File.createTempFile("tc-bootjar", null); 33 tempOutputFileAbsPath = tempOutputFile.getAbsolutePath(); 34 tempOutputFile.deleteOnExit(); 35 } catch (IOException e) { 36 throw new RuntimeException (e); 37 } 38 } else { 39 tempOutputFile = null; 40 tempOutputFileAbsPath = ""; 41 } 42 } 43 44 public void validateDirectoryExists() throws BootJarHandlerException { 45 try { 46 FileUtils.forceMkdir(outputFile.getAbsoluteFile().getParentFile()); 47 } catch (IOException ioe) { 48 throw new BootJarHandlerException("Failed to create path:" + outputFile.getParentFile().getAbsolutePath(), ioe); 49 } 50 51 if (write_out_temp_file) { 52 try { 53 FileUtils.forceMkdir(tempOutputFile.getAbsoluteFile().getParentFile()); 54 } catch (IOException ioe) { 55 throw new BootJarHandlerException("Failed to create path:" + tempOutputFile.getParentFile().getAbsolutePath(), 56 ioe); 57 } 58 } 59 } 60 61 public void announceCreationStart() { 62 announce("Creating boot JAR at '" + outputFileAbsPath + "..."); 63 } 64 65 public BootJar getBootJar() throws UnsupportedVMException { 66 if (write_out_temp_file) { 67 return BootJar.getBootJarForWriting(this.tempOutputFile); 68 } else { 69 return BootJar.getBootJarForWriting(this.outputFile); 70 } 71 } 72 73 public String getCreationErrorMessage() { 74 if (write_out_temp_file) { return "ERROR creating temp boot jar"; } 75 return "ERROR creating boot jar"; 76 } 77 78 public String getCloseErrorMessage() { 79 if (write_out_temp_file) { return "Failed to create temp jar file:" + tempOutputFileAbsPath; } 80 return "Failed to create jar file:" + outputFileAbsPath; 81 } 82 83 public void announceCreationEnd() throws BootJarHandlerException { 84 if (write_out_temp_file) { 85 createFinalBootJar(); 86 } 87 announce("Successfully created boot JAR file at '" + outputFileAbsPath + "'."); 88 } 89 90 private void createFinalBootJar() throws BootJarHandlerException { 91 announce("Creating boot JAR at '" + outputFileAbsPath + "..."); 92 try { 93 JarInputStream jarIn = new JarInputStream (new FileInputStream (tempOutputFile.getAbsolutePath())); 94 Manifest manifest = jarIn.getManifest(); 95 if (manifest == null) { 96 manifest = new Manifest (); 97 } 98 99 File tempFile = File.createTempFile("tc-bootjar", null); 100 tempFile.deleteOnExit(); 101 102 JarOutputStream jarOut = new JarOutputStream (new FileOutputStream (tempFile.getAbsolutePath()), manifest); 103 byte[] buffer = new byte[4096]; 104 JarEntry entry; 105 while ((entry = jarIn.getNextJarEntry()) != null) { 106 if ("META-INF/MANIFEST.MF".equals(entry.getName())) { 107 continue; 108 } 109 jarOut.putNextEntry(entry); 110 int read; 111 while ((read = jarIn.read(buffer)) != -1) { 112 jarOut.write(buffer, 0, read); 113 } 114 jarOut.closeEntry(); 115 } 116 jarOut.flush(); 117 jarOut.close(); 118 jarIn.close(); 119 120 copyFile(tempFile, outputFile); 121 } catch (Exception e) { 122 throw new BootJarHandlerException("ERROR creating boot jar", e); 123 } 124 if (!tempOutputFile.delete()) { 125 announce("Warning: Unsuccessful deletion of temp boot JAR file at '" + tempOutputFileAbsPath + "'."); 126 } 127 } 128 129 private void announce(String msg) { 130 System.out.println(msg); 131 } 132 133 private void copyFile(File src, File dest) throws IOException { 134 if (dest.isDirectory()) { 135 dest = new File (dest, src.getName()); 136 } 137 138 File tmplck = null; 139 InputStream in = null; 140 OutputStream out = null; 141 try { 142 tmplck = new File (dest.getParentFile(), "tc-bootjar.lck"); 144 while (tmplck.exists()) { 145 try { 146 Thread.sleep(1000); 147 } catch (InterruptedException e) { 148 } 150 } 151 152 tmplck.createNewFile(); 154 tmplck.deleteOnExit(); 155 156 File tmpdest = File.createTempFile("tc-bootjar", null, dest.getParentFile()); 158 out = new FileOutputStream (tmpdest); 159 in = new FileInputStream (src); 160 161 byte[] buffer = new byte[4096]; 162 int bytesRead; 163 164 while ((bytesRead = in.read(buffer)) >= 0) { 165 out.write(buffer, 0, bytesRead); 166 } 167 168 in.close(); 169 in = null; 170 171 out.close(); 172 out = null; 173 174 if(dest.exists() && !dest.delete()) { 176 throw new IOException ("Unable to delete '" + dest + "'"); 177 } 178 179 if(!tmpdest.renameTo(dest)) { 181 throw new IOException ("Unable to rename '" + tmpdest + "' to '" + dest + "'"); 182 } 183 } finally { 184 if (in != null) in.close(); 186 if (out != null) out.close(); 187 188 tmplck.delete(); 190 } 191 } 192 } 193 | Popular Tags |