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

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

Introduction

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

Prototype

public DefaultExecutor() 

Source Link

Document

Default constructor creating a default PumpStreamHandler and sets the working directory of the subprocess to the current working directory.

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   ww w .j  ava  2  s  .co  m*/

    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:com.stratio.explorer.shell.ShellInterpreter.java

@Override
public InterpreterResult interpret(String cmd) {
    logger.info("Run shell command '" + cmd + "'");
    long start = System.currentTimeMillis();
    CommandLine cmdLine = CommandLine.parse("bash");
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(cmd, false);/*from   w  w w  .j  a  v  a2 s. c  om*/
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStream));

    executor.setWatchdog(new ExecuteWatchdog(CMD_TIMEOUT));
    try {
        int exitValue = executor.execute(cmdLine);
        return new InterpreterResult(InterpreterResult.Code.SUCCESS, outputStream.toString());
    } catch (ExecuteException e) {
        logger.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    } catch (IOException e) {
        logger.error("Can not run " + cmd, e);
        return new InterpreterResult(Code.ERROR, e.getMessage());
    }
}

From source file:com.dangdang.ddframe.job.executor.type.ScriptJobExecutor.java

private void executeScript(final ShardingContext shardingContext, final String scriptCommandLine) {
    CommandLine commandLine = CommandLine.parse(scriptCommandLine);
    commandLine.addArgument(GsonFactory.getGson().toJson(shardingContext), false);
    try {/* w w  w . jav  a  2  s  .c o  m*/
        new DefaultExecutor().execute(commandLine);
    } catch (final IOException ex) {
        throw new JobConfigurationException("Execute script failure.", ex);
    }
}

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);// ww  w. j  ava 2s  .  c om
    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:com.arhs.mojo.pack200.AbstractPluginMojo.java

/**
 * Constructor./*from w ww  .  j ava  2  s. com*/
 *
 * @param command Command name.
 */
protected AbstractPluginMojo(String command) {
    this(CommandLine.parse(command), new DefaultExecutor());
}

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

public void execute() throws MojoExecutionException, MojoFailureException {
    CommandLine commandLine = new CommandLine(impdp);
    addCommonArguments(commandLine);/* ww w .  j a  v a 2  s.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.k42b3.sacmis.ExecutorAbstract.java

public void run() {
    try {//w ww  . ja v a2s.  c  o m
        // clear text
        this.textArea.setText("");

        CommandLine commandLine = CommandLine.parse(this.getExecutable() + " " + this.cmd);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);

        // create executor
        DefaultExecutor executor = new DefaultExecutor();

        executor.setStreamHandler(new PumpStreamHandler(new TextAreaOutputStream(textArea)));
        executor.setWatchdog(watchdog);
        executor.execute(commandLine);
    } catch (FoundNoExecutableException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Information", JOptionPane.ERROR_MESSAGE);
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}

From source file:com.k42b3.aletheia.filter.response.Application.java

public void exec(Response response) throws Exception {
    if (response instanceof com.k42b3.aletheia.protocol.http.Response) {
        com.k42b3.aletheia.protocol.http.Response httpResponse = (com.k42b3.aletheia.protocol.http.Response) response;

        String contentType = httpResponse.getHeader("Content-Type");

        if (Aletheia.getInstance().getConfig().getApplications().containsKey(contentType)) {
            String path = Aletheia.getInstance().getConfig().getApplications().get(contentType);
            String url = Aletheia.getInstance().getActiveUrl().getText();
            String cmd = path.replace("${url}", url);

            try {
                logger.info("Call " + cmd);

                CommandLine commandLine = CommandLine.parse(cmd);
                DefaultExecutor executor = new DefaultExecutor();

                executor.execute(commandLine);
            } catch (Exception e) {
                Aletheia.handleException(e);
            }//w w  w  .j av a2  s  .  c om
        }
    }
}

From source file:io.gatling.mojo.GatlingJavaMainCallerByFork.java

@Override
public boolean run(boolean displayCmd, boolean throwFailure) throws Exception {
    List<String> cmd = buildCommand();
    displayCmd(displayCmd, cmd);/* www  . j  ava 2 s.  c  om*/
    Executor exec = new DefaultExecutor();

    // err and out are redirected to out
    exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));

    exec.setProcessDestroyer(new ShutdownHookProcessDestroyer());

    CommandLine cl = new CommandLine(cmd.get(0));
    for (int i = 1; i < cmd.size(); i++) {
        cl.addArgument(cmd.get(i), false);
    }
    try {
        int exitValue = exec.execute(cl);
        if (exitValue != 0) {
            if (throwFailure) {
                throw new MojoFailureException("command line returned non-zero value:" + exitValue);
            }
            return false;
        }
        return true;
    } catch (ExecuteException exc) {
        if (throwFailure) {
            throw exc;
        }
        return false;
    }
}

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  ww . jav a2 s  . c om
            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;
    }
}