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

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

Introduction

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

Prototype

public DefaultExecuteResultHandler() 

Source Link

Document

Constructor.

Usage

From source file:com.stratio.ingestion.utils.IngestionUtils.java

public static DefaultExecuteResultHandler executeBash(String command) throws IOException {
    CommandLine cmdLine = CommandLine.parse("bash");
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(command, false);

    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStreamAgentStart = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStreamAgentStart));
    DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
    executor.execute(cmdLine, handler);/*www  .j  a v a  2 s  .  com*/

    return handler;
}

From source file:com.boulmier.machinelearning.jobexecutor.job.Job.java

public void start() throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final DefaultExecutor exec = new DefaultExecutor();
    final ExecuteWatchdog wd = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    final PumpStreamHandler output = new PumpStreamHandler(out);
    final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();

    exec.setWatchdog(wd);// w ww.jav a2s.com
    exec.setStreamHandler(output);
    exec.execute(cl, handler);
    JobExecutor.logger.info("Running job " + jobid);

    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                handler.waitFor();
                Computer.ComputeProperties properties = Computer.ComputeProperties.buildFromRequest(req);
                new SenderComputer(new StorageComputer(out.toString(), properties)).compute();
                JobExecutor.logger.info("Job complete " + jobid);
            } catch (InterruptedException ex) {
                exec.getWatchdog().destroyProcess();
                JobExecutor.logger.error(
                        "Job (" + jobid + ") has been destroyed due to internal error " + ex.getMessage());
            }
        }

    }).start();

}

From source file:io.selendroid.io.ShellCommand.java

public static void execAsync(String display, CommandLine commandline) throws ShellCommandException {
    log.info("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(new PritingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {//  w  w w .ja v a2 s . c o m
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        throw new ShellCommandException("An error occured while executing shell command: " + commandline, e);
    }
}

From source file:com.kotcrab.vis.editor.util.ApplicationUtils.java

public static void startNewInstance() {
    try {/* w w  w  .j  a  va  2 s. c  om*/
        CommandLine cmdLine = CommandLine.parse(getRestartCommand());
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(null, null, null));
        executor.execute(cmdLine, new DefaultExecuteResultHandler());
    } catch (Exception e) {
        Log.exception(e);
    }
}

From source file:io.selendroid.standalone.io.ShellCommand.java

public static void execAsync(String display, CommandLine commandline) throws ShellCommandException {
    log.info("executing async command: " + commandline);
    DefaultExecutor exec = new DefaultExecutor();

    ExecuteResultHandler handler = new DefaultExecuteResultHandler();
    PumpStreamHandler streamHandler = new PumpStreamHandler(new PrintingLogOutputStream());
    exec.setStreamHandler(streamHandler);
    try {/*from   w  w  w .j av  a 2 s . c  om*/
        if (display == null || display.isEmpty()) {
            exec.execute(commandline, handler);
        } else {
            Map env = EnvironmentUtils.getProcEnvironment();
            EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display);

            exec.execute(commandline, env, handler);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error executing command: " + commandline, e);
        throw new ShellCommandException("Error executing shell command: " + commandline, e);
    }
}

From source file:com.tribuneqa.utilities.FrameworkUtilities.java

public static void appiumStop() throws IOException {
    // Add different arguments In command line which requires to stop appium server. 
    CommandLine command = new CommandLine("cmd");
    command.addArgument("/c");
    command.addArgument("taskkill");
    command.addArgument("/F");
    command.addArgument("/IM");
    command.addArgument("node.exe");

    // Execute command line arguments to stop appium server. 
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(1);/*w ww  .  j a v a2 s  . com*/
    executor.execute(command, resultHandler);
}

From source file:net.minecraft.client.MineExec.java

@Override
protected Integer doInBackground() throws IOException, InterruptedException {
    Executor exe = new DefaultExecutor();
    CommandLine mineExec = CommandLine.parse(mineCmd);

    PipedOutputStream stdout = new PipedOutputStream();
    PipedOutputStream stderr = new PipedOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr);
    exe.setStreamHandler(streamHandler);
    MinecraftApplet configCrusher = new MinecraftApplet();
    try {/*from  w  w  w . jav a2  s.  c o  m*/
        File target = new File(VersionInfo.getTechnicFolder());
        BufferedInputStream biserr = new BufferedInputStream(new PipedInputStream(stderr));
        ExecuteResultHandler rh = new DefaultExecuteResultHandler();
        exe.execute(mineExec, rh);
        BufferedReader reader = new BufferedReader(new InputStreamReader(biserr));
        configCrusher.minecraftLoaded();
        String line = reader.readLine();
        logWindow.append(line + "\n");
        int dupLen = 0;
        int dupMax = 25;
        while (line != null) {
            String line2 = reader.readLine();
            if ((line2.contains("Aether") || line2.contains("aether") && watchCfg
                    && new File(target + File.separator + VersionInfo.getModpackName(), "MenuAPI.properties")
                            .exists())) {
                configCrusher.prepareConfigs();
            }
            if ((Boolean) nbDebug[2]) {
                if (!line.equals(line2) || dupLen >= dupMax) {
                    if (dupLen > 0) {
                        logWindow.append(line + "(" + dupLen + ")\n");
                    } else {
                        if (!line.equals(line2)) {
                            logWindow.append(line2 + "\n");
                        }
                    }
                    dupLen = 0;
                } else {
                    dupLen++;
                }
                line = line2;
            } else {
                logWindow.append(line2 + "\n");
                line = line2;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if ((Integer) (nbDebug[1]) <= 1) {
        configCrusher.remExtras();
        configCrusher.minecraftClosed(password);
    }
    return 1;
}

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 w  ww.  j  a  va 2 s  . c  o 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:com.tribuneqa.utilities.FrameworkUtilities.java

public void appiumStart() throws IOException, InterruptedException {

    // Start command prompt In background.
    CommandLine command = new CommandLine("cmd");

    // Add different arguments In command line which requires to start appium server.
    command.addArgument("/c");

    // Add different arguments In command line which requires to start appium server. 
    command.addArgument("appium");
    command.addArgument("-a");
    command.addArgument("10.20.121.69");
    command.addArgument("-p");
    command.addArgument("8001");
    command.addArgument("-U");
    command.addArgument("4d0081724d5741c7");

    // Execute command line arguments to start appium server. 
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(1);//w w w  .j  a  v a2  s . c  o  m
    executor.execute(command, resultHandler);

    Thread.sleep(15000);

}

From source file:com.github.genium_framework.appium.support.command.CommandManager.java

/**
 * Execute a command on the operating system using Apache Commons Exec. This
 * function runs asynchronously and dumps both stderr and stdout streams to
 * a temp file.// w  w w.  ja v  a  2 s  .co  m
 *
 * @param commandLine The command to be executed.
 * @param outputStreamHandler An output stream to dump the process stderr
 * and stdout to it.
 */
public static void executeCommandUsingApacheExec(CommandLine commandLine, OutputStream outputStreamHandler) {
    try {
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStreamHandler);

        Executor process = new DefaultExecutor();
        process.setExitValue(0);
        process.setStreamHandler(streamHandler);
        process.execute(commandLine, resultHandler);
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "An exception was thrown.", ex);
    }
}