1 18 19 package org.apache.tools.ant.taskdefs.optional; 20 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.PrintWriter ; 26 import java.util.Enumeration ; 27 import java.util.Vector ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.DirectoryScanner; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.taskdefs.ExecTask; 32 import org.apache.tools.ant.taskdefs.Execute; 33 import org.apache.tools.ant.taskdefs.LogOutputStream; 34 import org.apache.tools.ant.taskdefs.MatchingTask; 35 import org.apache.tools.ant.taskdefs.StreamPumper; 36 import org.apache.tools.ant.taskdefs.condition.Os; 37 import org.apache.tools.ant.types.FileSet; 38 import org.apache.tools.ant.util.FileUtils; 39 40 41 45 46 public class Cab extends MatchingTask { 47 48 private File cabFile; 49 private File baseDir; 50 private Vector filesets = new Vector (); 51 private boolean doCompress = true; 52 private boolean doVerbose = false; 53 private String cmdOptions; 54 55 protected String archiveType = "cab"; 57 59 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 60 61 65 public void setCabfile(File cabFile) { 66 this.cabFile = cabFile; 67 } 68 69 73 public void setBasedir(File baseDir) { 74 this.baseDir = baseDir; 75 } 76 77 81 public void setCompress(boolean compress) { 82 doCompress = compress; 83 } 84 85 89 public void setVerbose(boolean verbose) { 90 doVerbose = verbose; 91 } 92 93 97 public void setOptions(String options) { 98 cmdOptions = options; 99 } 100 101 105 public void addFileset(FileSet set) { 106 if (filesets.size() > 0) { 107 throw new BuildException("Only one nested fileset allowed"); 108 } 109 filesets.addElement(set); 110 } 111 112 117 121 protected void checkConfiguration() throws BuildException { 122 if (baseDir == null && filesets.size() == 0) { 123 throw new BuildException("basedir attribute or one " 124 + "nested fileset is required!", 125 getLocation()); 126 } 127 if (baseDir != null && !baseDir.exists()) { 128 throw new BuildException("basedir does not exist!", getLocation()); 129 } 130 if (baseDir != null && filesets.size() > 0) { 131 throw new BuildException( 132 "Both basedir attribute and a nested fileset is not allowed"); 133 } 134 if (cabFile == null) { 135 throw new BuildException("cabfile attribute must be set!", 136 getLocation()); 137 } 138 } 139 140 146 protected ExecTask createExec() throws BuildException { 147 ExecTask exec = new ExecTask(this); 148 return exec; 149 } 150 151 156 protected boolean isUpToDate(Vector files) { 157 boolean upToDate = true; 158 for (int i = 0; i < files.size() && upToDate; i++) { 159 String file = files.elementAt(i).toString(); 160 if (FILE_UTILS.resolveFile(baseDir, file).lastModified() 161 > cabFile.lastModified()) { 162 upToDate = false; 163 } 164 } 165 return upToDate; 166 } 167 168 178 protected File createListFile(Vector files) 179 throws IOException { 180 File listFile = FILE_UTILS.createTempFile("ant", "", null); 181 listFile.deleteOnExit(); 182 183 PrintWriter writer = new PrintWriter (new FileOutputStream (listFile)); 184 185 int size = files.size(); 186 for (int i = 0; i < size; i++) { 187 writer.println('\"' + files.elementAt(i).toString() + '\"'); 188 } 189 writer.close(); 190 191 return listFile; 192 } 193 194 199 protected void appendFiles(Vector files, DirectoryScanner ds) { 200 String [] dsfiles = ds.getIncludedFiles(); 201 202 for (int i = 0; i < dsfiles.length; i++) { 203 files.addElement(dsfiles[i]); 204 } 205 } 206 207 214 protected Vector getFileList() throws BuildException { 215 Vector files = new Vector (); 216 217 if (baseDir != null) { 218 appendFiles(files, super.getDirectoryScanner(baseDir)); 220 } else { 221 FileSet fs = (FileSet) filesets.elementAt(0); 222 baseDir = fs.getDir(); 223 appendFiles(files, fs.getDirectoryScanner(getProject())); 224 } 225 226 return files; 227 } 228 229 233 public void execute() throws BuildException { 234 235 checkConfiguration(); 236 237 Vector files = getFileList(); 238 239 if (isUpToDate(files)) { 241 return; 242 } 243 244 log("Building " + archiveType + ": " + cabFile.getAbsolutePath()); 245 246 if (!Os.isFamily("windows")) { 247 log("Using listcab/libcabinet", Project.MSG_VERBOSE); 248 249 StringBuffer sb = new StringBuffer (); 250 251 Enumeration fileEnum = files.elements(); 252 253 while (fileEnum.hasMoreElements()) { 254 sb.append(fileEnum.nextElement()).append("\n"); 255 } 256 sb.append("\n").append(cabFile.getAbsolutePath()).append("\n"); 257 258 try { 259 Process p = Execute.launch(getProject(), 260 new String [] {"listcab"}, null, 261 baseDir != null ? baseDir 262 : getProject().getBaseDir(), 263 true); 264 OutputStream out = p.getOutputStream(); 265 266 LogOutputStream outLog = new LogOutputStream(this, Project.MSG_VERBOSE); 270 LogOutputStream errLog = new LogOutputStream(this, Project.MSG_ERR); 271 StreamPumper outPump = new StreamPumper(p.getInputStream(), outLog); 272 StreamPumper errPump = new StreamPumper(p.getErrorStream(), errLog); 273 274 (new Thread (outPump)).start(); 276 (new Thread (errPump)).start(); 277 278 out.write(sb.toString().getBytes()); 279 out.flush(); 280 out.close(); 281 282 int result = -99; 284 try { 285 result = p.waitFor(); 287 288 outPump.waitFor(); 290 outLog.close(); 291 errPump.waitFor(); 292 errLog.close(); 293 } catch (InterruptedException ie) { 294 log("Thread interrupted: " + ie); 295 } 296 297 if (Execute.isFailure(result)) { 299 log("Error executing listcab; error code: " + result); 300 } 301 } catch (IOException ex) { 302 String msg = "Problem creating " + cabFile + " " + ex.getMessage(); 303 throw new BuildException(msg, getLocation()); 304 } 305 } else { 306 try { 307 File listFile = createListFile(files); 308 ExecTask exec = createExec(); 309 File outFile = null; 310 311 exec.setFailonerror(true); 313 exec.setDir(baseDir); 314 315 if (!doVerbose) { 316 outFile = FILE_UTILS.createTempFile("ant", "", null); 317 outFile.deleteOnExit(); 318 exec.setOutput(outFile); 319 } 320 321 exec.setExecutable("cabarc"); 322 exec.createArg().setValue("-r"); 323 exec.createArg().setValue("-p"); 324 325 if (!doCompress) { 326 exec.createArg().setValue("-m"); 327 exec.createArg().setValue("none"); 328 } 329 330 if (cmdOptions != null) { 331 exec.createArg().setLine(cmdOptions); 332 } 333 334 exec.createArg().setValue("n"); 335 exec.createArg().setFile(cabFile); 336 exec.createArg().setValue("@" + listFile.getAbsolutePath()); 337 338 exec.execute(); 339 340 if (outFile != null) { 341 outFile.delete(); 342 } 343 344 listFile.delete(); 345 } catch (IOException ioe) { 346 String msg = "Problem creating " + cabFile + " " + ioe.getMessage(); 347 throw new BuildException(msg, getLocation()); 348 } 349 } 350 } 351 } 352 | Popular Tags |