Example usage for org.apache.commons.exec Executor setStreamHandler

List of usage examples for org.apache.commons.exec Executor setStreamHandler

Introduction

In this page you can find the example usage for org.apache.commons.exec Executor setStreamHandler.

Prototype

void setStreamHandler(ExecuteStreamHandler streamHandler);

Source Link

Document

Set a custom the StreamHandler used for providing input and retrieving the output.

Usage

From source file:org.apache.bigtop.itest.hive.HiveHelper.java

public static Map<String, String> execCommand(CommandLine commandline, Map<String, String> envVars) {

    System.out.println("Executing command:");
    System.out.println(commandline.toString());
    Map<String, String> env = null;
    Map<String, String> entry = new HashMap<String, String>();
    try {// w  ww . j  a  va2  s .  c  o  m
        env = EnvironmentUtils.getProcEnvironment();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to get process environment: " + e1.getMessage());
        e1.printStackTrace();
    }
    if (envVars != null) {
        for (String key : envVars.keySet()) {
            env.put(key, envVars.get(key));
        }
    }

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 10000);
    Executor executor = new DefaultExecutor();
    executor.setExitValue(1);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(streamHandler);
    try {
        executor.execute(commandline, env, resultHandler);
    } catch (ExecuteException e) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString() + e.getMessage());
        e.printStackTrace();
        return entry;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString() + e.getMessage());
        e.printStackTrace();
        return entry;
    }

    try {
        resultHandler.waitFor();
        /*System.out.println("Command output: "+outputStream.toString());*/
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString());
        return entry;
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        /*System.out.println("Command output: "+outputStream.toString());*/
        LOG.debug("exitValue: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString());
        e.printStackTrace();
        return entry;
    }
}

From source file:org.apache.maven.plugin.cxx.utils.ExecutorService.java

public static int executeCommandLine(Executor exec, CommandLine commandLine, Properties enviro,
        OutputStream out, OutputStream err, InputStream in) throws ExecuteException, IOException {
    exec.setExitValues(null); // let us decide of exit value
    exec.setStreamHandler(new PumpStreamHandler(out, err, in));
    return exec.execute(commandLine, enviro);
}

From source file:org.cloudifysource.dsl.context.utils.VolumeUtils.java

private static void executeCommandLine(final String commandLine, final long timeout)
        throws LocalStorageOperationException, TimeoutException {

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);/*  ww w.  ja va 2s .  c  o  m*/
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    ProcessOutputStream outAndErr = new ProcessOutputStream();
    try {
        PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr);
        executor.setStreamHandler(streamHandler);
        logger.info("Executing commandLine : '" + commandLine + "'");
        executor.execute(CommandLine.parse(commandLine));
        logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput());
    } catch (final Exception e) {
        if (watchdog.killedProcess()) {
            throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'");
        }
        throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine
                + ". Process output was : " + outAndErr.getOutput(), e);
    }
}

From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java

private static void executeCommandLine(final String commandLine, final long timeout)
        throws LocalStorageOperationException, TimeoutException {

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);/*  w  w  w .  j  a v  a 2 s.c o  m*/
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    ProcessOutputStream outAndErr = new ProcessOutputStream();
    try {
        PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr);
        executor.setStreamHandler(streamHandler);
        logger.info("Executing commandLine : '" + commandLine + "'");
        executor.execute(CommandLine.parse(commandLine));
        logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput());
    } catch (final Exception e) {
        if (watchdog.killedProcess()) {
            throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'");
        }

        throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine
                + ". Process output was : " + outAndErr.getOutput(), e);
    }
}

From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java

private static String executeSilentCommandLineReturnOutput(final String commandLine, final long timeout)
        throws LocalStorageOperationException, TimeoutException {

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);//from   w  ww .  j  a v  a  2s . co m
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    ProcessOutputStream outAndErr = new ProcessOutputStream();
    try {
        PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr);
        executor.setStreamHandler(streamHandler);
        executor.execute(CommandLine.parse(commandLine));
    } catch (final Exception e) {
        if (watchdog.killedProcess()) {
            throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'");
        }

        throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine
                + ". Process output was : " + outAndErr.getOutput(), e);
    }

    return outAndErr.getOutput();
}

From source file:org.codehaus.mojo.AbstractLaunchMojo.java

protected int executeCommandLine(Executor exec, CommandLine commandLine, Map enviro, OutputStream out,
        OutputStream err, InputStream in) throws ExecuteException, IOException {
    exec.setStreamHandler(new PumpStreamHandler(out, err, in));
    return exec.execute(commandLine, enviro);
}

From source file:org.codehaus.mojo.cassandra.CleanupCassandraMojo.java

/**
 * {@inheritDoc}//w  w  w.  ja  v a2s  .  c o  m
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping cassandra: cassandra.skip==true");
        return;
    }
    try {
        Map environment = createEnvironmentVars();
        CommandLine commandLine = newNodetoolCommandLine("cleanup");

        Executor exec = new DefaultExecutor();
        exec.setWorkingDirectory(cassandraDir);
        exec.setProcessDestroyer(new ShutdownHookProcessDestroyer());

        LogOutputStream stdout = new MavenLogOutputStream(getLog());
        LogOutputStream stderr = new MavenLogOutputStream(getLog());

        try {
            getLog().debug("Executing command line: " + commandLine);

            exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in));

            exec.execute(commandLine, environment);

            getLog().info("Cleanup triggered.");
        } catch (ExecuteException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        } catch (IOException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
}

From source file:org.codehaus.mojo.cassandra.CompactCassandraMojo.java

/**
 * {@inheritDoc}//from w  ww . j a v  a  2  s  .c  o m
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping cassandra: cassandra.skip==true");
        return;
    }
    try {
        Map environment = createEnvironmentVars();
        CommandLine commandLine = newNodetoolCommandLine("compact");

        Executor exec = new DefaultExecutor();
        exec.setWorkingDirectory(cassandraDir);
        exec.setProcessDestroyer(new ShutdownHookProcessDestroyer());

        LogOutputStream stdout = new MavenLogOutputStream(getLog());
        LogOutputStream stderr = new MavenLogOutputStream(getLog());

        try {
            getLog().debug("Executing command line: " + commandLine);

            exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in));

            exec.execute(commandLine, environment);

            getLog().info("Compact triggered.");
        } catch (ExecuteException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        } catch (IOException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
}

From source file:org.codehaus.mojo.cassandra.FlushCassandraMojo.java

/**
 * {@inheritDoc}//from  w  w  w  .  ja v  a 2  s  .c  om
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping cassandra: cassandra.skip==true");
        return;
    }
    try {
        Map environment = createEnvironmentVars();
        CommandLine commandLine = newNodetoolCommandLine("flush");

        Executor exec = new DefaultExecutor();
        exec.setWorkingDirectory(cassandraDir);
        exec.setProcessDestroyer(new ShutdownHookProcessDestroyer());

        LogOutputStream stdout = new MavenLogOutputStream(getLog());
        LogOutputStream stderr = new MavenLogOutputStream(getLog());

        try {
            getLog().debug("Executing command line: " + commandLine);

            exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in));

            exec.execute(commandLine, environment);

            getLog().info("Flush triggered.");
        } catch (ExecuteException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        } catch (IOException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
}

From source file:org.codehaus.mojo.cassandra.RepairCassandraMojo.java

/**
 * {@inheritDoc}//from  w  ww . j  a  va  2  s. c  om
 */
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping cassandra: cassandra.skip==true");
        return;
    }
    try {
        Map environment = createEnvironmentVars();
        CommandLine commandLine = newNodetoolCommandLine("repair");

        Executor exec = new DefaultExecutor();
        exec.setWorkingDirectory(cassandraDir);
        exec.setProcessDestroyer(new ShutdownHookProcessDestroyer());

        LogOutputStream stdout = new MavenLogOutputStream(getLog());
        LogOutputStream stderr = new MavenLogOutputStream(getLog());

        try {
            getLog().debug("Executing command line: " + commandLine);

            exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in));

            exec.execute(commandLine, environment);

            getLog().info("Repair triggered.");
        } catch (ExecuteException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        } catch (IOException e) {
            throw new MojoExecutionException("Command execution failed.", e);
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getLocalizedMessage(), e);
    }
}