Example usage for org.apache.commons.exec PumpStreamHandler PumpStreamHandler

List of usage examples for org.apache.commons.exec PumpStreamHandler PumpStreamHandler

Introduction

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

Prototype

public PumpStreamHandler(final OutputStream out, final OutputStream err) 

Source Link

Document

Construct a new PumpStreamHandler.

Usage

From source file:com.vmware.bdd.usermgmt.job.ChangeLocalAccountStateExecutor.java

private void changeLocalAccountState(String argument) {
    //String chefCmd = "sudo /opt/serengeti/sbin/set-password L";
    String sudoCmd = CommonUtil.getCustomizedSudoCmd();
    CommandLine cmdLine = new CommandLine(sudoCmd).addArgument(SET_PASSWORD_COMMAND).addArgument(argument);

    DefaultExecutor executor = new DefaultExecutor();

    executor.setStreamHandler(new PumpStreamHandler(new ExecOutputLogger(LOGGER, false), //output logger
            new ExecOutputLogger(LOGGER, true)) //error logger
    );//from   w  ww.  j a va  2 s  . com

    executor.setWatchdog(new ExecuteWatchdog(1000l * TIMEOUT));

    try {
        int exitVal = executor.execute(cmdLine);
        if (exitVal != 0) {
            throw new UserMgmtExecException("CHANGE_LOCAL_ACCOUNT_STATE_FAIL", null);
        }
    } catch (IOException e) {
        throw new UserMgmtExecException("CHANGE_LOCAL_ACCOUNT_STATE_FAIL", e);
    }
}

From source file:hoot.services.command.CommandRunnerImpl.java

@Override
public CommandResult exec(String command) throws IOException {
    logger.debug("Executing the following command: {}", command);

    try (OutputStream stdout = new ByteArrayOutputStream(); OutputStream stderr = new ByteArrayOutputStream()) {

        CommandLine cmdLine = CommandLine.parse(command);

        ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(stdout, stderr);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(executeStreamHandler);

        int exitValue;
        try {/*from w  w w.  j a v a2 s .  c  o m*/
            exitValue = executor.execute(cmdLine);
        } catch (Exception e) {
            exitValue = -1;
            logger.warn("Error executing: {}", cmdLine, e);
        }

        CommandResult commandResult = new CommandResult(cmdLine.toString(), exitValue, stdout.toString(),
                stderr.toString());

        this.stdout = stdout.toString();

        logger.debug("Finished executing: {}", commandResult);

        return commandResult;
    }
}

From source file:de.torstenwalter.maven.plugins.ImpdpMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    CommandLine commandLine = new CommandLine(impdp);
    addCommonArguments(commandLine);//from   www .  ja  va  2s  .c o m

    if (StringUtils.isNotEmpty(remap_tablespace)) {
        commandLine.addArgument("REMAP_TABLESPACE=" + remap_tablespace);
    }

    if (StringUtils.isNotEmpty(remap_schema)) {
        commandLine.addArgument("REMAP_SCHEMA=" + remap_schema);
    }

    if (StringUtils.isNotEmpty(table_exists_action)) {
        commandLine.addArgument("TABLE_EXISTS_ACTION=" + table_exists_action);
    }

    getLog().info("Executing command line: " + obfuscateCredentials(commandLine.toString(), getCredentials()));

    Executor exec = new DefaultExecutor();
    exec.setStreamHandler(new PumpStreamHandler(System.out, System.err));
    try {
        exec.execute(commandLine);
    } catch (ExecuteException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    }
}

From source file:com.zxy.commons.exec.CmdExecutor.java

/**
 * //ww w.jav a  2  s.c o  m
 * 
 * @param workHome workHome
 * @param command command
 * @return ???
 * @throws InterruptedException InterruptedException
 * @throws IOException IOException
 */
public static ExecutorResult exec(String workHome, String command) throws InterruptedException, IOException {
    CommandLine cmdLine = CommandLine.parse(PRE_CMD + command);
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(new File(workHome));

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
    executor.setStreamHandler(streamHandler);

    int code = executor.execute(cmdLine, EnvironmentUtils.getProcEnvironment());
    String successMsg = outputStream.toString(ENCODING);
    String errorMsg = errorStream.toString(ENCODING);
    return new ExecutorResult(code, successMsg, errorMsg);
}

From source file:io.covert.binary.analysis.ExecutorThread.java

public ExecutorThread(String prog, String[] args, Map<String, Object> substitutionMap, int[] exitCodes,
        long timeoutMS, File workingDirectory) throws ExecuteException, IOException {
    cmdLine = new CommandLine(prog);
    cmdLine.addArguments(args);/*from www  .j  a v  a  2s .  co  m*/
    cmdLine.setSubstitutionMap(substitutionMap);

    resultHandler = new DefaultExecuteResultHandler();

    watchdog = new ExecuteWatchdog(60 * 1000);
    executor = new DefaultExecutor();

    stdOut = new ByteArrayOutputStream();
    stdErr = new ByteArrayOutputStream();

    streamHandler = new PumpStreamHandler(stdOut, stdErr);
    executor.setStreamHandler(streamHandler);
    executor.setWorkingDirectory(workingDirectory);
    executor.setExitValues(exitCodes);
    executor.setWatchdog(watchdog);
}

From source file:io.vertx.config.vault.utils.VaultProcess.java

private void init() {
    String line = executable.getAbsolutePath() + " init -key-shares=1 -key-threshold=1 " + CA_CERT_ARG;
    System.out.println(">> " + line);
    CommandLine parse = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    PumpStreamHandler pump = new PumpStreamHandler(new VaultOutputStream().addExtractor(l -> {
        if (l.contains("Unseal Key 1:")) {
            unseal = l.replace("Unseal Key 1: ", "").trim();
        } else if (l.contains("Initial Root Token:")) {
            token = l.replace("Initial Root Token: ", "").trim();
        }/*  w  w w  . j av a2s . c o  m*/
    }), System.err);

    ExecuteWatchdog watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    executor.setWatchdog(watchDog);
    executor.setStreamHandler(pump);
    try {
        executor.execute(parse);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    //    await().until(() -> token != null);
    //    await().until(() -> unseal != null);

    System.out.println("Vault Server initialized (but sealed)");
    System.out.println("Root token: " + token);
    System.out.println("Unseal key: " + unseal);
}

From source file:de.pawlidi.openaletheia.utils.exec.ProcessExecutor.java

/**
 * Creates executor with system watchdog for given output stream.
 * //from  ww w.  j  a v  a2 s  . c  o  m
 * @param outputStream
 * @return
 */
private static Executor createExecutor(OutputStream outputStream) {

    // create process watchdog with timeout 60000 milliseconds
    ExecuteWatchdog watchdog = new ExecuteWatchdog(WATCHDOG_TIMEOUT);

    // set watchdog and stream handler
    Executor executor = new DefaultExecutor();
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(new PumpStreamHandler(outputStream, outputStream));
    return executor;
}

From source file:com.netflix.genie.GenieCoreTestApplication.java

/**
 * Get an {@link Executor} to use for executing processes from tasks.
 *
 * @return The executor to use/*from  ww  w . ja  va 2  s .  c om*/
 */
@Bean
public Executor processExecutor() {
    final Executor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(null, null));
    return executor;
}

From source file:hoot.services.nativeinterfaces.CommandRunnerImpl.java

@Override
public CommandResult exec(String[] command) throws IOException {
    logger.debug("Executing the following command: {}", Arrays.toString(command));

    try (OutputStream stdout = new ByteArrayOutputStream(); OutputStream stderr = new ByteArrayOutputStream()) {

        this.stdout = stdout;
        this.stderr = stderr;

        CommandLine cmdLine = new CommandLine(command[0]);
        for (int i = 1; i < command.length; i++) {
            cmdLine.addArgument(command[i], false);
        }// w  w w  .  jav  a 2  s.  c o m

        ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(stdout, stderr);
        DefaultExecutor executor = new DefaultExecutor();
        this.watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
        executor.setWatchdog(this.watchDog);
        executor.setStreamHandler(executeStreamHandler);

        int exitValue;
        try {
            exitValue = executor.execute(cmdLine);

            if (executor.isFailure(exitValue) && this.watchDog.killedProcess()) {
                // it was killed on purpose by the watchdog
                logger.info("Process for '{}' command was killed!", cmdLine);
            }
        } catch (Exception e) {
            exitValue = -1;
            logger.warn("Error executing: {}", cmdLine, e);
        }

        CommandResult commandResult = new CommandResult(cmdLine.toString(), exitValue, stdout.toString(),
                stderr.toString());

        logger.debug("Finished executing: {}", commandResult);

        return commandResult;
    }
}

From source file:com.vmware.bdd.usermgmt.job.CfgUserMgmtOnMgmtVMExecutor.java

private void execCommand(CommandLine cmdLine) {
    DefaultExecutor executor = new DefaultExecutor();

    executor.setStreamHandler(new PumpStreamHandler(new ExecOutputLogger(LOGGER, false), //output logger
            new ExecOutputLogger(LOGGER, true)) //error logger
    );/*  ww  w.j av a  2 s. c  o m*/

    executor.setWatchdog(new ExecuteWatchdog(1000l * TIMEOUT));

    try {
        int exitVal = executor.execute(cmdLine);
        if (exitVal != 0) {
            throw new UserMgmtExecException("CFG_LDAP_FAIL", null);
        }
    } catch (IOException e) {
        throw new UserMgmtExecException("CFG_LDAP_FAIL", e);
    }
}