FileDocCategorySizeDatePackage
Cab.javaAPI DocApache Ant 1.7012107Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.optional

Cab

public class Cab extends org.apache.tools.ant.taskdefs.MatchingTask
Create a CAB archive.

Fields Summary
private File
cabFile
private File
baseDir
private Vector
filesets
private boolean
doCompress
private boolean
doVerbose
private String
cmdOptions
protected String
archiveType
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
Constructors Summary
Methods Summary
public voidaddFileset(org.apache.tools.ant.types.FileSet set)
Adds a set of files to archive.

param
set a set of files to archive.

        if (filesets.size() > 0) {
            throw new BuildException("Only one nested fileset allowed");
        }
        filesets.addElement(set);
    
protected voidappendFiles(java.util.Vector files, org.apache.tools.ant.DirectoryScanner ds)
Append all files found by a directory scanner to a vector.

param
files the vector to append the files to.
param
ds the scanner to get the files from.

        String[] dsfiles = ds.getIncludedFiles();

        for (int i = 0; i < dsfiles.length; i++) {
            files.addElement(dsfiles[i]);
        }
    
protected voidcheckConfiguration()
Check if the attributes and nested elements are correct.

throws
BuildException on error.

        if (baseDir == null && filesets.size() == 0) {
            throw new BuildException("basedir attribute or one "
                                     + "nested fileset is required!",
                                     getLocation());
        }
        if (baseDir != null && !baseDir.exists()) {
            throw new BuildException("basedir does not exist!", getLocation());
        }
        if (baseDir != null && filesets.size() > 0) {
            throw new BuildException(
                "Both basedir attribute and a nested fileset is not allowed");
        }
        if (cabFile == null) {
            throw new BuildException("cabfile attribute must be set!",
                                     getLocation());
        }
    
protected org.apache.tools.ant.taskdefs.ExecTaskcreateExec()
Create a new exec delegate. The delegate task is populated so that it appears in the logs to be the same task as this one.

return
the delegate.
throws
BuildException on error.

        ExecTask exec = new ExecTask(this);
        return exec;
    
protected java.io.FilecreateListFile(java.util.Vector files)
Creates a list file. This temporary file contains a list of all files to be included in the cab, one file per line.

This method expects to only be called on Windows and thus quotes the file names.

param
files the list of files to use.
return
the list file created.
throws
IOException if there is an error.

        File listFile = FILE_UTILS.createTempFile("ant", "", null);
        listFile.deleteOnExit();

        PrintWriter writer = new PrintWriter(new FileOutputStream(listFile));

        int size = files.size();
        for (int i = 0; i < size; i++) {
            writer.println('\"" + files.elementAt(i).toString() + '\"");
        }
        writer.close();

        return listFile;
    
public voidexecute()
execute this task.

throws
BuildException on error.


        checkConfiguration();

        Vector files = getFileList();

        // quick exit if the target is up to date
        if (isUpToDate(files)) {
            return;
        }

        log("Building " + archiveType + ": " + cabFile.getAbsolutePath());

        if (!Os.isFamily("windows")) {
            log("Using listcab/libcabinet", Project.MSG_VERBOSE);

            StringBuffer sb = new StringBuffer();

            Enumeration fileEnum = files.elements();

            while (fileEnum.hasMoreElements()) {
                sb.append(fileEnum.nextElement()).append("\n");
            }
            sb.append("\n").append(cabFile.getAbsolutePath()).append("\n");

            try {
                Process p = Execute.launch(getProject(),
                                           new String[] {"listcab"}, null,
                                           baseDir != null ? baseDir
                                                   : getProject().getBaseDir(),
                                           true);
                OutputStream out = p.getOutputStream();

                // Create the stream pumpers to forward listcab's stdout and stderr to the log
                // note: listcab is an interactive program, and issues prompts for every new line.
                //       Therefore, make it show only with verbose logging turned on.
                LogOutputStream outLog = new LogOutputStream(this, Project.MSG_VERBOSE);
                LogOutputStream errLog = new LogOutputStream(this, Project.MSG_ERR);
                StreamPumper    outPump = new StreamPumper(p.getInputStream(), outLog);
                StreamPumper    errPump = new StreamPumper(p.getErrorStream(), errLog);

                // Pump streams asynchronously
                (new Thread(outPump)).start();
                (new Thread(errPump)).start();

                out.write(sb.toString().getBytes());
                out.flush();
                out.close();

                int result = -99; // A wild default for when the thread is interrupted

                try {
                    // Wait for the process to finish
                    result = p.waitFor();

                    // Wait for the end of output and error streams
                    outPump.waitFor();
                    outLog.close();
                    errPump.waitFor();
                    errLog.close();
                } catch (InterruptedException ie) {
                    log("Thread interrupted: " + ie);
                }

                // Informative summary message in case of errors
                if (Execute.isFailure(result)) {
                    log("Error executing listcab; error code: " + result);
                }
            } catch (IOException ex) {
                String msg = "Problem creating " + cabFile + " " + ex.getMessage();
                throw new BuildException(msg, getLocation());
            }
        } else {
            try {
                File listFile = createListFile(files);
                ExecTask exec = createExec();
                File outFile = null;

                // die if cabarc fails
                exec.setFailonerror(true);
                exec.setDir(baseDir);

                if (!doVerbose) {
                    outFile = FILE_UTILS.createTempFile("ant", "", null);
                    outFile.deleteOnExit();
                    exec.setOutput(outFile);
                }

                exec.setExecutable("cabarc");
                exec.createArg().setValue("-r");
                exec.createArg().setValue("-p");

                if (!doCompress) {
                    exec.createArg().setValue("-m");
                    exec.createArg().setValue("none");
                }

                if (cmdOptions != null) {
                    exec.createArg().setLine(cmdOptions);
                }

                exec.createArg().setValue("n");
                exec.createArg().setFile(cabFile);
                exec.createArg().setValue("@" + listFile.getAbsolutePath());

                exec.execute();

                if (outFile != null) {
                    outFile.delete();
                }

                listFile.delete();
            } catch (IOException ioe) {
                String msg = "Problem creating " + cabFile + " " + ioe.getMessage();
                throw new BuildException(msg, getLocation());
            }
        }
    
protected java.util.VectorgetFileList()
Get the complete list of files to be included in the cab. Filenames are gathered from the fileset if it has been added, otherwise from the traditional include parameters.

return
the list of files.
throws
BuildException if there is an error.

        Vector files = new Vector();

        if (baseDir != null) {
            // get files from old methods - includes and nested include
            appendFiles(files, super.getDirectoryScanner(baseDir));
        } else {
            FileSet fs = (FileSet) filesets.elementAt(0);
            baseDir = fs.getDir();
            appendFiles(files, fs.getDirectoryScanner(getProject()));
        }

        return files;
    
protected booleanisUpToDate(java.util.Vector files)
Check to see if the target is up to date with respect to input files.

param
files the list of files to check.
return
true if the cab file is newer than its dependents.

        boolean upToDate = true;
        for (int i = 0; i < files.size() && upToDate; i++) {
            String file = files.elementAt(i).toString();
            if (FILE_UTILS.resolveFile(baseDir, file).lastModified()
                    > cabFile.lastModified()) {
                upToDate = false;
            }
        }
        return upToDate;
    
public voidsetBasedir(java.io.File baseDir)
Base directory to look in for files to CAB.

param
baseDir base directory for files to cab.

        this.baseDir = baseDir;
    
public voidsetCabfile(java.io.File cabFile)
The name/location of where to create the .cab file.

param
cabFile the location of the cab file.


                          
        
        this.cabFile = cabFile;
    
public voidsetCompress(boolean compress)
If true, compress the files otherwise only store them.

param
compress a boolean value.

        doCompress = compress;
    
public voidsetOptions(java.lang.String options)
Sets additional cabarc options that are not supported directly.

param
options cabarc command line options.

        cmdOptions = options;
    
public voidsetVerbose(boolean verbose)
If true, display cabarc output.

param
verbose a boolean value.

        doVerbose = verbose;