KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tools > BootJarHandler


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.tools;
5
6 import org.apache.commons.io.FileUtils;
7
8 import java.io.File JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.OutputStream JavaDoc;
14 import java.util.jar.JarEntry JavaDoc;
15 import java.util.jar.JarInputStream JavaDoc;
16 import java.util.jar.JarOutputStream JavaDoc;
17 import java.util.jar.Manifest JavaDoc;
18
19 public class BootJarHandler {
20   private final boolean write_out_temp_file;
21   private final File JavaDoc outputFile;
22   private final File JavaDoc tempOutputFile;
23   private final String JavaDoc tempOutputFileAbsPath;
24   private final String JavaDoc outputFileAbsPath;
25
26   public BootJarHandler(boolean write_out_temp_file, File JavaDoc 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 JavaDoc e) {
36         throw new RuntimeException JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc getCreationErrorMessage() {
74     if (write_out_temp_file) { return "ERROR creating temp boot jar"; }
75     return "ERROR creating boot jar";
76   }
77
78   public String JavaDoc 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 JavaDoc jarIn = new JarInputStream JavaDoc(new FileInputStream JavaDoc(tempOutputFile.getAbsolutePath()));
94       Manifest JavaDoc manifest = jarIn.getManifest();
95       if (manifest == null) {
96         manifest = new Manifest JavaDoc();
97       }
98
99       File JavaDoc tempFile = File.createTempFile("tc-bootjar", null);
100       tempFile.deleteOnExit();
101
102       JarOutputStream JavaDoc jarOut = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(tempFile.getAbsolutePath()), manifest);
103       byte[] buffer = new byte[4096];
104       JarEntry JavaDoc 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 JavaDoc 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 JavaDoc msg) {
130     System.out.println(msg);
131   }
132
133   private void copyFile(File JavaDoc src, File JavaDoc dest) throws IOException JavaDoc {
134     if (dest.isDirectory()) {
135       dest = new File JavaDoc(dest, src.getName());
136     }
137
138     File JavaDoc tmplck = null;
139     InputStream JavaDoc in = null;
140     OutputStream JavaDoc out = null;
141     try {
142       // wait until it's okay to copy over the bootjar
143
tmplck = new File JavaDoc(dest.getParentFile(), "tc-bootjar.lck");
144       while (tmplck.exists()) {
145         try {
146           Thread.sleep(1000);
147         } catch (InterruptedException JavaDoc e) {
148           //throw e;
149
}
150       }
151       
152       // block everyone else from copying over their bootjar
153
tmplck.createNewFile();
154       tmplck.deleteOnExit();
155       
156       // copy our new bootjar file over into a temporary file
157
File JavaDoc tmpdest = File.createTempFile("tc-bootjar", null, dest.getParentFile());
158       out = new FileOutputStream JavaDoc(tmpdest);
159       in = new FileInputStream JavaDoc(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       // remove any existing bootjar...
175
if(dest.exists() && !dest.delete()) {
176         throw new IOException JavaDoc("Unable to delete '" + dest + "'");
177       }
178       
179       // ... and replace it with the one that we just copied over
180
if(!tmpdest.renameTo(dest)) {
181         throw new IOException JavaDoc("Unable to rename '" + tmpdest + "' to '" + dest + "'");
182       }
183     } finally {
184       // house keeping
185
if (in != null) in.close();
186       if (out != null) out.close();
187
188       // now signal that it's okay for the other clients to copy over their bootjar
189
tmplck.delete();
190     }
191   }
192 }
193
Popular Tags