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:org.owasp.goatdroid.gui.emulator.EmulatorWorker.java

static public String startAndroidSDKManager()
        throws FileNotFoundException, IOException, CorruptConfigException {

    HashMap<String, String> settings = Config.readConfig();
    String emulator = "";
    if (getOperatingSystem().startsWith("windows"))
        if (Emulator.doesToolExist("", "AVD Manager.exe"))
            emulator = settings.get("sdk_path") + getSlash() + "AVD Manager.exe";
        else/*www.j a va  2  s .  co m*/
            emulator = settings.get("sdk_path") + getSlash() + "tools" + getSlash() + "android";
    else
        emulator = settings.get("sdk_path") + getSlash() + "tools" + getSlash() + "android";
    CommandLine cmdLine = new CommandLine(emulator);

    DefaultExecutor executor = new DefaultExecutor();
    try {
        executor.execute(cmdLine);
        return Constants.SUCCESS;
    } catch (ExecuteException e) {
        return Constants.UNEXPECTED_ERROR;
    } catch (IOException e) {
        return Constants.UNEXPECTED_ERROR;
    }
}

From source file:org.owasp.goatdroid.gui.emulator.EmulatorWorker.java

static public String pushAppOntoDevice(String appPath) {

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    CommandLine cmdLine = new CommandLine(sdkPath + getSlash() + "platform-tools" + getSlash() + "adb");
    cmdLine.addArgument("install");
    cmdLine.addArgument(appPath, false);
    DefaultExecutor executor = new DefaultExecutor();
    try {/*from w  ww .  j  a  v a 2  s .  co  m*/
        executor.setStreamHandler(psh);
        executor.execute(cmdLine);
        return stdout.toString();
    } catch (ExecuteException e) {
        return Constants.UNEXPECTED_ERROR;
    } catch (IOException e) {
        return Constants.UNEXPECTED_ERROR;
    }
}

From source file:org.owasp.goatdroid.gui.emulator.EmulatorWorker.java

static public String pushAppOntoDevice(String appPath, String deviceSerial) {

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    CommandLine cmdLine = new CommandLine(sdkPath + getSlash() + "platform-tools" + getSlash() + "adb");
    Map<String, String> map = new HashMap<String, String>();
    map.put("deviceSerial", deviceSerial);
    cmdLine.addArgument("-s", false);
    cmdLine.addArgument("${deviceSerial}", false);
    cmdLine.addArgument("install", false);
    cmdLine.addArgument(appPath, false);
    cmdLine.setSubstitutionMap(map);/*from w  w w  .jav  a  2s . c  o  m*/
    DefaultExecutor executor = new DefaultExecutor();
    try {
        executor.setStreamHandler(psh);
        executor.execute(cmdLine);
        return stdout.toString();
    } catch (ExecuteException e) {
        return Constants.UNEXPECTED_ERROR;
    } catch (IOException e) {
        return Constants.UNEXPECTED_ERROR;
    }
}

From source file:org.rbr8.script_runner.util.BatchScriptRunner.java

public void processFile(File file) throws IOException {

    CommandLine cmdLine = new CommandLine(executable);
    cmdLine.addArguments(arguments);/* ww  w. j  a v a  2s .  c  o m*/

    Map<String, Object> map = new HashMap<>();
    map.put(SubstitutionHelper.FILE, file);
    cmdLine.setSubstitutionMap(map);

    DefaultExecutor executor = new DefaultExecutor();
    //        executor.setExitValue(1);

    //        NotifierLogOutputStream outputLog = new NotifierLogOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(logOutputStream);
    executor.setStreamHandler(psh);

    //        ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    //        executor.setWatchdog(watchdog);
    int exitValue = executor.execute(cmdLine);
}

From source file:org.rhq.server.control.ControlCommand.java

protected void killPid(String pid) throws IOException {
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(getBinDir());
    PumpStreamHandler streamHandler = new PumpStreamHandler(createNullOutputStream(), createNullOutputStream());
    executor.setStreamHandler(streamHandler);
    org.apache.commons.exec.CommandLine commandLine;

    commandLine = new org.apache.commons.exec.CommandLine("kill").addArgument(pid);
    executor.execute(commandLine);/*  ww  w .ja  va2 s. c  om*/
}

From source file:org.rhq.server.control.ControlCommand.java

protected boolean isUnixPidRunning(String pid) {

    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(getBinDir());
    PumpStreamHandler streamHandler = new PumpStreamHandler(createNullOutputStream(), createNullOutputStream());
    executor.setStreamHandler(streamHandler);
    org.apache.commons.exec.CommandLine commandLine;
    commandLine = new org.apache.commons.exec.CommandLine("kill").addArgument("-0").addArgument(pid);

    boolean isRunning = true; // assume it is running
    try {//from   ww w  .ja  v a 2  s . co m
        int code = executor.execute(commandLine);
        if (code != 0) {
            isRunning = false;
        }
    } catch (ExecuteException ee) {
        log.debug("kill -0 for pid [" + pid + "] threw exception with exit value [" + ee.getExitValue() + "]");
        if (ee.getExitValue() == 1) {
            // return code 1 means process does not exist
            isRunning = false;
        }
    } catch (IOException e) {
        log.error("Checking for running process failed. Will assume it is running. Error: " + e.getMessage());
    }

    log.debug("unix pid [" + pid + "] " + ((isRunning) ? "is" : "is NOT") + " running");
    return isRunning;
}

From source file:org.rhq.storage.installer.StorageInstaller.java

private String exec(File workingDir, org.apache.commons.exec.CommandLine cmdLine) throws Exception {
    Executor executor = new DefaultExecutor();
    org.apache.commons.io.output.ByteArrayOutputStream buffer = new org.apache.commons.io.output.ByteArrayOutputStream();
    NullOutputStream nullOs = new NullOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(nullOs, buffer);
    executor.setWorkingDirectory(workingDir);
    executor.setStreamHandler(streamHandler);
    String result = "";

    try {//from   w w w.  j  a v  a 2  s  . c o m
        exec(executor, cmdLine);
        result = buffer.toString();

    } finally {
        try {
            buffer.close();
            nullOs.close();
        } catch (Exception e) {
            // best effort
        }
    }

    return result;
}

From source file:org.sakuli.actions.environment.CommandLineUtil.java

static public CommandLineResult runCommand(String command, boolean throwException) throws SakuliException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    CommandLineResult result = new CommandLineResult();
    try {// ww w.  ja v a2 s.co m
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(outputStream, error));
        int exitCode = executor.execute(CommandLine.parse(command));
        result.setExitCode(exitCode);
        result.setOutput(error.toString() + outputStream.toString());
    } catch (Exception e) {
        if (throwException) {
            throw new SakuliException(e,
                    String.format("Error during execution of command '%s': %s", command, error.toString()));
        }
        result.setExitCode(resolveExitCode(e.getMessage()));
        result.setOutput(e.getMessage());
    }
    return result;
}

From source file:org.sikuli.natives.CommandExecutorHelper.java

public static CommandExecutorResult execute(String commandString, int expectedExitValue) throws Exception {
    ByteArrayOutputStream error = new ByteArrayOutputStream();
    ByteArrayOutputStream stout = new ByteArrayOutputStream();
    CommandLine cmd = CommandLine.parse(commandString);
    try {/*from w  w w.  j  ava 2  s .com*/
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(expectedExitValue);
        executor.setStreamHandler(new PumpStreamHandler(stout, error));
        //if exit value != expectedExitValue => Exception
        int exitValue = executor.execute(cmd);
        return new CommandExecutorResult(exitValue, stout.toString(), error.toString());

    } catch (Exception e) {
        int exitValue = -1;
        if (e instanceof ExecuteException) {
            exitValue = ((ExecuteException) e).getExitValue();
        }
        throw new CommandExecutorException("error in command " + cmd.toString(),
                new CommandExecutorResult(exitValue, stout.toString(), error.toString()));
    }
}

From source file:org.sikuli.natives.LinuxUtil.java

@Override
public void checkLibAvailability() {
    List<CommandLine> commands = Arrays.asList(CommandLine.parse("wmctrl -m"),
            CommandLine.parse("xdotool version"), CommandLine.parse("killall --version"));
    String msg = "";
    for (CommandLine cmd : commands) {
        try {/*from   w w  w.  j  a v  a2s .c o m*/
            DefaultExecutor executor = new DefaultExecutor();
            executor.setExitValue(0);
            //suppress system output
            executor.setStreamHandler(new PumpStreamHandler(null));
            executor.execute(cmd);
        } catch (IOException e) {
            String executable = cmd.toStrings()[0];
            if (executable.equals("wmctrl")) {
                wmctrlAvail = false;
            }
            if (executable.equals("xdotool")) {
                xdoToolAvail = false;
            }
            msg += "command '" + executable + "' is not executable\n";
        }
    }
    if (!msg.isEmpty()) {
        msg += "Please check the availability - some features might not work without!";
        Debug.error(msg);
    }
}