1 | /* |
2 | * Created on Aug 31, 2004 |
3 | * |
4 | * TODO To change the template for this generated file go to |
5 | * Window - Preferences - Java - Code Style - Code Templates |
6 | */ |
7 | package biz.xsoftware.buildtemplate; |
8 | |
9 | import java.io.File; |
10 | import java.io.FileInputStream; |
11 | import java.io.FileOutputStream; |
12 | import java.io.IOException; |
13 | import java.io.InputStream; |
14 | import java.util.ArrayList; |
15 | import java.util.Enumeration; |
16 | import java.util.Iterator; |
17 | import java.util.List; |
18 | import java.util.jar.JarEntry; |
19 | import java.util.jar.JarFile; |
20 | import java.util.logging.Logger; |
21 | |
22 | /** |
23 | * Fix some things |
24 | * 1. Use Shutdown hook to compress template if someone pressed ctrl-C |
25 | * 1. Test the ManifestInfo with TestManifestInfo.java |
26 | * 2. Fix mockobject and all projects who want to include code coverage in the deliver |
27 | * or do we....maybe we automatically post code coverage, but I do like it in the |
28 | * distribution. |
29 | * 7. IMPORTANT: Must log all files in buildtemplate.version file because right now we only delete |
30 | * files by using the jar file itself which may be incorrect as files may have been |
31 | * removed from it leaving files on the file system. |
32 | * NOTE: files in staging or *.java files should not be moved/renamed as the files |
33 | * would be left on the users view. |
34 | * 3. Don't want to extract permanent files over and over unless |
35 | * a. file is actually different! otherwise interacts poorly with CVS clients |
36 | * 4. merging properties files(files with extension .properties.default) |
37 | * a. don't write ".default" files on every upgrade, very annoying. |
38 | * I turned it off for now |
39 | * b. In fact, never write .default files except on first time, after |
40 | * always merge or don't write |
41 | * 5. listing files that need to be checked in as deleted/added/changed based on |
42 | * number 3 and 4 above |
43 | * 6. fill in BuildTemplate.testExtract to fail if extract will not work |
44 | * This one is very low priority and not that important as it will rarely rarely happen. |
45 | */ |
46 | public class BuildTemplate { |
47 | |
48 | private final static Logger log = Logger.getLogger(BuildTemplate.class.getName()); |
49 | |
50 | private static final String DEFAULT = ".default"; |
51 | private static final String PERMANENT = ".permanent"; |
52 | |
53 | private JarFile zip; |
54 | private File baseDir; |
55 | |
56 | public BuildTemplate(File f, File baseDir) throws IOException { |
57 | this.zip = new JarFile(f); |
58 | this.baseDir = baseDir; |
59 | } |
60 | |
61 | /** |
62 | * |
63 | */ |
64 | public FileBean[] extract() throws IOException { |
65 | log.fine("extracting buildtemplate"); |
66 | List extractedFiles = new ArrayList(); |
67 | Enumeration enum = zip.entries(); |
68 | while(enum.hasMoreElements()) { |
69 | JarEntry entry = (JarEntry)enum.nextElement(); |
70 | FileBean file = extractFileOrDirectory(zip, entry); |
71 | if(file != null) |
72 | extractedFiles.add(file); |
73 | } |
74 | FileBean[] retVal = new FileBean[0]; |
75 | return (FileBean[])extractedFiles.toArray(retVal); |
76 | } |
77 | |
78 | /** |
79 | * |
80 | * @param dir |
81 | * @param file |
82 | * @param entry |
83 | * @return Filename if it is a permanently extracted file, else return null for |
84 | * temporarily extracted files. |
85 | * @throws IOException |
86 | */ |
87 | private FileBean extractFileOrDirectory(JarFile file, JarEntry entry) throws IOException { |
88 | |
89 | File outFile = new File(baseDir, entry.getName()); |
90 | if(entry.isDirectory()) { |
91 | if(!outFile.exists()) |
92 | outFile.mkdirs(); |
93 | else if(!outFile.isDirectory()) { |
94 | String s = "File="+outFile.getCanonicalPath()+" is a file and needs to be a directory\n" |
95 | +"Please rename the file temporarily so the template can extract fully\n" |
96 | +"The stuff that is already extracted will not be backed out of"; |
97 | log.severe(s); |
98 | System.exit(1); |
99 | } |
100 | return null; |
101 | } else if(entry.getName().endsWith(DEFAULT)) { |
102 | return extractToBeMergedFile(file, entry, outFile); |
103 | } else if(entry.getName().endsWith(PERMANENT)) { |
104 | int len = entry.getName().length() - PERMANENT.length(); |
105 | String name = cutoffEndString(entry.getName(), PERMANENT); |
106 | return extractPermanentFile(file, entry, name); |
107 | } else |
108 | extractOverrideFile(file, entry, outFile); |
109 | return null; |
110 | } |
111 | |
112 | private String cutoffEndString(String target, String suffix) { |
113 | int len = target.length() - suffix.length(); |
114 | return target.substring(0, len); |
115 | } |
116 | |
117 | private FileBean extractToBeMergedFile(JarFile file, JarEntry entry, File outFile) throws IOException { |
118 | log.fine("extracting user defined file="+entry.getName()); |
119 | String name = cutoffEndString(entry.getName(), DEFAULT); |
120 | File tempFile = new File(baseDir, name); |
121 | if(!tempFile.exists()) { |
122 | extractOverrideFile(file, entry, tempFile); |
123 | } else { |
124 | //otherwise, file needs to be merged, load in both and see what new properties are |
125 | //needed. |
126 | log.finer("File="+name+" already exists."); |
127 | log.finer("Creating "+entry.getName()+" instead. Please merge"); |
128 | //Actually, I don't think we ever want this..... |
129 | //instead, we actually want to find out if it is a properties file |
130 | //If it is, we add the new properties to that file from the file |
131 | //we have...we need to make sure though we tack on our properties at the |
132 | //end and don't screw with the way they have the file formatted |
133 | //extractOverrideFile(dir, file, entry, new File(dir, entry.getName())); |
134 | return null; |
135 | } |
136 | return new FileBean(name, tempFile.lastModified()); |
137 | } |
138 | |
139 | private FileBean extractPermanentFile(JarFile file, JarEntry entry, String outFileName) throws IOException { |
140 | File outFile = new File(baseDir, outFileName); |
141 | |
142 | boolean filesAreTheSame = false; |
143 | if(outFile.exists()) { |
144 | InputStream in1 = file.getInputStream(entry); |
145 | InputStream in2 = new FileInputStream(outFile); |
146 | filesAreTheSame = Util.inputsAreTheSame(outFile.length(), in1, in2); |
147 | in1.close(); |
148 | in2.close(); |
149 | } |
150 | |
151 | if(!filesAreTheSame) { |
152 | log.info("File changed, upgrading="+outFile.getName()); |
153 | extractOverrideFile(file, entry, outFile); |
154 | return new FileBean(outFileName, outFile.lastModified()); |
155 | } |
156 | return null; |
157 | } |
158 | |
159 | private void extractOverrideFile(JarFile file, JarEntry entry, File outFile) throws IOException { |
160 | if(!outFile.exists()) |
161 | outFile.createNewFile(); |
162 | else if(!outFile.isFile()) { |
163 | String s = "File="+outFile.getCanonicalPath()+" is a directory and needs to be a file" |
164 | +"Please rename the directory temporarily so the template can extract fully" |
165 | +"The stuff that is already extracted will not be backed out of"; |
166 | log.severe(s); |
167 | System.exit(1); |
168 | } |
169 | |
170 | log.finest("writing file="+outFile.getCanonicalPath()); |
171 | InputStream in = file.getInputStream(entry); |
172 | FileOutputStream out = new FileOutputStream(outFile); |
173 | Util.copyInToOut(in, out); |
174 | in.close(); |
175 | out.close(); |
176 | } |
177 | |
178 | /** |
179 | * @throws IOException |
180 | * |
181 | */ |
182 | public void compress() throws IOException { |
183 | log.info("recompressing template back to jar file"); |
184 | Enumeration enum = zip.entries(); |
185 | //need to reverse all the elements |
186 | List list = new ArrayList(); |
187 | while(enum.hasMoreElements()) { |
188 | list.add(0, enum.nextElement()); |
189 | } |
190 | |
191 | Iterator iter = list.iterator(); |
192 | while(iter.hasNext()) { |
193 | JarEntry entry = (JarEntry)iter.next(); |
194 | deleteFile(baseDir, entry); |
195 | } |
196 | zip.close(); |
197 | } |
198 | |
199 | private void deleteFile(File baseDir, JarEntry entry) throws IOException { |
200 | File f = new File(baseDir, entry.getName()); |
201 | if(f.getAbsolutePath().endsWith("javasrc")) |
202 | return; |
203 | boolean result = f.delete(); |
204 | f.deleteOnExit(); //deleteOnExit is for directories as the delete on |
205 | //directories fails when files are in them that |
206 | //are deleted after the directory. |
207 | |
208 | log.finest("deleting file="+entry.getName()+" is="+result); |
209 | } |
210 | |
211 | /** |
212 | * Goes through and makes sure extraction will succeed. If will not, |
213 | * warns the user of the problem and exits without building the |
214 | * system. |
215 | */ |
216 | public void testExtraction() { |
217 | |
218 | } |
219 | |
220 | |
221 | } |