Search in sources :

Example 1 with StreamPumper

use of org.apache.tools.ant.taskdefs.StreamPumper in project ant by apache.

the class GreedyInputHandler method handleInput.

/**
 * Prompts and requests input.
 * @param request the request to handle
 * @throws BuildException if not possible to read from console,
 *         or if input is invalid.
 */
public void handleInput(InputRequest request) throws BuildException {
    String prompt = getPrompt(request);
    InputStream in = null;
    try {
        in = getInputStream();
        System.err.println(prompt);
        System.err.flush();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        StreamPumper p = new StreamPumper(in, baos);
        Thread t = new Thread(p);
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            try {
                t.join();
            } catch (InterruptedException e2) {
            // Ignore
            }
        }
        request.setInput(new String(baos.toByteArray()));
        if (!request.isInputValid()) {
            throw new BuildException("Received invalid console input");
        }
        if (p.getException() != null) {
            throw new BuildException("Failed to read input from console", p.getException());
        }
    } finally {
        FileUtils.close(in);
    }
}
Also used : InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StreamPumper(org.apache.tools.ant.taskdefs.StreamPumper) BuildException(org.apache.tools.ant.BuildException)

Example 2 with StreamPumper

use of org.apache.tools.ant.taskdefs.StreamPumper in project ant by apache.

the class Cab method execute.

/**
 * execute this task.
 * @throws BuildException on error.
 */
@Override
public void execute() throws BuildException {
    checkConfiguration();
    Vector<String> 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);
        StringBuilder sb = new StringBuilder();
        files.forEach(f -> sb.append(f).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();
            // A wild default for when the thread is interrupted
            int result = DEFAULT_RESULT;
            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) {
            throw new BuildException("Problem creating " + cabFile + " " + ex.getMessage(), 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, true, true);
                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) {
            throw new BuildException("Problem creating " + cabFile + " " + ioe.getMessage(), getLocation());
        }
    }
}
Also used : OutputStream(java.io.OutputStream) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) ExecTask(org.apache.tools.ant.taskdefs.ExecTask) IOException(java.io.IOException) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) StreamPumper(org.apache.tools.ant.taskdefs.StreamPumper) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Aggregations

BuildException (org.apache.tools.ant.BuildException)2 StreamPumper (org.apache.tools.ant.taskdefs.StreamPumper)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ExecTask (org.apache.tools.ant.taskdefs.ExecTask)1 LogOutputStream (org.apache.tools.ant.taskdefs.LogOutputStream)1