Search in sources :

Example 6 with TeeOutputStream

use of org.apache.tools.ant.util.TeeOutputStream in project ant by apache.

the class SSHExec method executeCommand.

private void executeCommand(final Session session, final String cmd, final StringBuilder sb) throws BuildException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final ByteArrayOutputStream errout = new ByteArrayOutputStream();
    final OutputStream teeErr = suppressSystemErr ? errout : new TeeOutputStream(errout, KeepAliveOutputStream.wrapSystemErr());
    final OutputStream tee = suppressSystemOut ? out : new TeeOutputStream(out, KeepAliveOutputStream.wrapSystemOut());
    InputStream istream = null;
    if (inputFile != null) {
        try {
            istream = Files.newInputStream(inputFile.toPath());
        } catch (final IOException e) {
            // because we checked the existence before, this one
            // shouldn't happen What if the file exists, but there
            // are no read permissions?
            log("Failed to read " + inputFile + " because of: " + e.getMessage(), Project.MSG_WARN);
        }
    }
    if (inputProperty != null) {
        final String inputData = getProject().getProperty(inputProperty);
        if (inputData != null) {
            istream = new ByteArrayInputStream(inputData.getBytes());
        }
    }
    if (inputString != null) {
        istream = new ByteArrayInputStream(inputString.getBytes());
    }
    if (useSystemIn) {
        istream = KeepAliveInputStream.wrapSystemIn();
    }
    try {
        final ChannelExec channel;
        session.setTimeout((int) maxwait);
        /* execute the command */
        channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand(cmd);
        channel.setOutputStream(tee);
        channel.setExtOutputStream(tee);
        channel.setErrStream(teeErr);
        if (istream != null) {
            channel.setInputStream(istream);
        }
        channel.setPty(usePty);
        channel.connect();
        // wait for it to finish
        thread = new Thread() {

            @Override
            public void run() {
                while (!channel.isClosed()) {
                    if (thread == null) {
                        return;
                    }
                    try {
                        sleep(RETRY_INTERVAL);
                    } catch (final Exception e) {
                    // ignored
                    }
                }
            }
        };
        thread.start();
        thread.join(maxwait);
        if (thread.isAlive()) {
            // ran out of time
            thread = null;
            if (getFailonerror()) {
                throw new BuildException(TIMEOUT_MESSAGE);
            }
            log(TIMEOUT_MESSAGE, Project.MSG_ERR);
        } else {
            // stdout to outputFile
            if (outputFile != null) {
                writeToFile(out.toString(), append, outputFile);
            }
            // set errorProperty
            if (errorProperty != null) {
                getProject().setNewProperty(errorProperty, errout.toString());
            }
            // stderr to errorFile
            if (errorFile != null) {
                writeToFile(errout.toString(), appenderr, errorFile);
            }
            // this is the wrong test if the remote OS is OpenVMS,
            // but there doesn't seem to be a way to detect it.
            final int ec = channel.getExitStatus();
            // set resultproperty
            if (resultProperty != null) {
                getProject().setNewProperty(resultProperty, Integer.toString(ec));
            }
            if (ec != 0) {
                final String msg = "Remote command failed with exit status " + ec;
                if (getFailonerror()) {
                    throw new BuildException(msg);
                }
                log(msg, Project.MSG_ERR);
            }
        }
    } catch (final BuildException e) {
        throw e;
    } catch (final JSchException e) {
        if (e.getMessage().contains("session is down")) {
            if (getFailonerror()) {
                throw new BuildException(TIMEOUT_MESSAGE, e);
            }
            log(TIMEOUT_MESSAGE, Project.MSG_ERR);
        } else {
            if (getFailonerror()) {
                throw new BuildException(e);
            }
            log("Caught exception: " + e.getMessage(), Project.MSG_ERR);
        }
    } catch (final Exception e) {
        if (getFailonerror()) {
            throw new BuildException(e);
        }
        log("Caught exception: " + e.getMessage(), Project.MSG_ERR);
    } finally {
        sb.append(out.toString());
        FileUtils.close(istream);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) TeeOutputStream(org.apache.tools.ant.util.TeeOutputStream) KeepAliveInputStream(org.apache.tools.ant.util.KeepAliveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeepAliveOutputStream(org.apache.tools.ant.util.KeepAliveOutputStream) TeeOutputStream(org.apache.tools.ant.util.TeeOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ChannelExec(com.jcraft.jsch.ChannelExec) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) JSchException(com.jcraft.jsch.JSchException) ByteArrayInputStream(java.io.ByteArrayInputStream) BuildException(org.apache.tools.ant.BuildException)

Aggregations

TeeOutputStream (org.apache.tools.ant.util.TeeOutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 OutputStream (java.io.OutputStream)5 KeepAliveOutputStream (org.apache.tools.ant.util.KeepAliveOutputStream)5 IOException (java.io.IOException)4 PipedOutputStream (java.io.PipedOutputStream)4 BuildException (org.apache.tools.ant.BuildException)4 LazyFileOutputStream (org.apache.tools.ant.util.LazyFileOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ChannelExec (com.jcraft.jsch.ChannelExec)1 JSchException (com.jcraft.jsch.JSchException)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 ChainReaderHelper (org.apache.tools.ant.filters.util.ChainReaderHelper)1 Execute (org.apache.tools.ant.taskdefs.Execute)1 LogOutputStream (org.apache.tools.ant.taskdefs.LogOutputStream)1 PumpStreamHandler (org.apache.tools.ant.taskdefs.PumpStreamHandler)1