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

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

Introduction

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

Prototype

public int execute(final CommandLine command) throws ExecuteException, IOException 

Source Link

Usage

From source file:com.sohu.dc.jobkeeper.ActiveMasterManager.java

private void startPrepare() {
    String shellPath = ActiveMasterManager.class.getClassLoader().getResource("").getPath().replace("classes/",
            "");//  w w w.  j  a  v  a 2  s .  c  o  m
    DefaultExecutor executor = new DefaultExecutor();
    CommandLine cmd = new CommandLine(shellPath + "prepare.sh");
    try {
        executor.execute(cmd);
    } catch (ExecuteException e) {
        LOG.debug("prepare.sh exec faild \n" + e);
    } catch (IOException e) {
        LOG.debug("prepare.sh exec faild \n" + e);
    }

}

From source file:com.github.rwhogg.git_vcr.review.ProcessBasedReviewTool.java

/**
 * Runs the command string specified by commandLine, then returns the output.
 * Note that any command-line arguments from the configuration are automatically added.
 * @param commands The rest of the command line, excluding any arguments
 * @return The output of the command/*from  w  w w.j av  a 2s . c o  m*/
 */
protected List<String> runProcess(String commands) throws ExecuteException, IOException {
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    executor.setExitValues(null);
    String arguments = configuration.getString("args", "");
    executor.execute(CommandLine.parse(getExecutableName() + " " + arguments + " " + commands));
    return Arrays.asList(outputStream.toString("UTF-8").split("\n"));
}

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);/* w w  w  .ja  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.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
    );/*w  w  w  .j a va 2s.c om*/

    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.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.j a v 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.blackducksoftware.tools.scmconnector.core.CommandLineExecutor.java

@Override
public CommandResults executeCommandForOutput(Logger log, CommandLine command, File targetDir)
        throws Exception {
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);//from w  w w. ja v  a2  s. co  m
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(psh);

    int exitStatus = 1;
    executor.setWorkingDirectory(targetDir);
    exitStatus = executor.execute(command);
    String outputString = outputStream.toString();

    // This log msg would reveal password
    // log.info("Command: " + command.toString() + " executed in: "
    // + targetDir.toString() + "; output: " + outputString);

    return new CommandResults(exitStatus, outputString);
}

From source file:com.k42b3.aletheia.filter.request.Process.java

public void exec(Request request) {
    String cmd = getConfig().getProperty("cmd");

    try {//w  w w. java 2 s .c  o  m
        logger.info("Execute: " + cmd);

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

        this.baos = new ByteArrayOutputStream();
        this.baosErr = new ByteArrayOutputStream();
        this.bais = new ByteArrayInputStream(request.getContent().getBytes());

        executor.setStreamHandler(new PumpStreamHandler(this.baos, this.baosErr, this.bais));
        executor.setWatchdog(watchdog);
        executor.execute(commandLine);

        logger.info("Output: " + this.baos.toString());

        request.setContent(this.baos.toString());
    } catch (Exception e) {
        logger.warning(e.getMessage());
    }
}

From source file:com.vmware.bdd.service.impl.NodeLdapUserMgmtConfService.java

private void transferFile(String srcFilePath, String ip, String targetFilePath) {
    CommandLine cmdLine = new CommandLine("scp").addArgument(srcFilePath)
            .addArgument(ip + ":" + targetFilePath);

    DefaultExecutor executor = new DefaultExecutor();

    executor.setStreamHandler(new PumpStreamHandler(new ExecOutputLogger(LOGGER, false), //output logger
            new ExecOutputLogger(LOGGER, true)) //error logger
    );/*from ww  w .  java 2s. c om*/

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

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

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
    );//from   w  w  w.  j  a va2  s .  c om

    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);
    }
}

From source file:net.openbyte.gui.CreateProjectFrame.java

public void doOperations() {
    createProjectFolder();/*from  w w  w.  j a  va2 s  . com*/
    monitor.setProgress(20);
    monitor.setNote("Creating solution for project...");
    createSolutionFile();
    monitor.setNote("Creating library solution for project...");
    createLibrarySolution();
    monitor.setProgress(60);
    monitor.setNote("Cloning API...");
    cloneAPI();
    monitor.setProgress(90);
    monitor.setNote("Decompiling Minecraft...");
    if (this.api == ModificationAPI.MCP) {
        if (System.getProperty("os.name").startsWith("Windows")) {
            try {
                Runtime.getRuntime().exec("cmd /c decompile.bat", null, projectFolder);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            try {
                CommandLine commandLine = CommandLine.parse("python "
                        + new File(new File(projectFolder, "runtime"), "decompile.py").getAbsolutePath()
                        + " $@");
                File runtimePythonDirectory = new File(projectFolder, "runtime");
                File decompilePython = new File(runtimePythonDirectory, "decompile.py");
                CommandLine authPy = CommandLine.parse("chmod 755 " + decompilePython.getAbsolutePath());
                DefaultExecutor executor = new DefaultExecutor();
                executor.setWorkingDirectory(projectFolder);
                executor.execute(authPy);
                executor.execute(commandLine);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    if (this.api == ModificationAPI.MINECRAFT_FORGE) {
        GradleConnector.newConnector().forProjectDirectory(projectFolder).connect().newBuild()
                .setJvmArguments("-XX:-UseGCOverheadLimit").forTasks("setupDecompWorkspace").run();
    }
    monitor.close();
    WelcomeFrame welcomeFrame = new WelcomeFrame();
    welcomeFrame.setVisible(true);
}