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 outAndErr) 

Source Link

Document

Construct a new PumpStreamHandler.

Usage

From source file:nl.tudelft.graphalytics.graphlab.GraphLabPlatform.java

/**
 * Execute the python script belonging to a given AlgorithmType with the given graph location and extra arguments
 * and return the Process created by the Java Runtime.
 * @param job The GraphLab job to execute
 * @return The exit code of the python subprocess
 * @throws IOException When an I/O error occurs
 *//* w w w . j ava2s.  com*/
private int executePythonJob(GraphLabJob job) throws IOException {
    LOG.entry(job);

    if (job == null) {
        LOG.warn("GraphLab job set to execute is null, skipping execution.");
        return LOG.exit(-1);
    }

    // Extract the script resource file
    File scriptFile = extractFile(job.getPythonFile());
    if (scriptFile == null) {
        return LOG.exit(-1);
    }

    // Construct the commandline execution pattern starting with the python executable
    CommandLine commandLine = new CommandLine("python2");

    // Add the arguments that are the same for all jobs
    commandLine.addArgument(scriptFile.getAbsolutePath());
    commandLine.addArgument("--target");
    commandLine.addArgument(TARGET);
    if (USE_HADOOP) {
        commandLine.addArgument("--virtual-cores");
        commandLine.addArgument(VIRTUAL_CORES, false);
        commandLine.addArgument("--heap-size");
        commandLine.addArgument(HEAP_SIZE, false);
    }

    // Add the save_graph_result parameter is true (default false, but can be set to true for automated testing)
    if (saveGraphResult) {
        commandLine.addArgument("--save-result");
    }

    // Let the job format it's arguments and add it to the commandline
    commandLine.addArguments(job.formatParametersAsStrings(), false);

    // Set the executor of the command, if desired this can be changed to a custom implementation
    DefaultExecutor executor = new DefaultExecutor();

    // Set the OutputStream to enable printing the output of the algorithm
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStream));

    int result;
    try {
        // Execute the actual command and store the return code
        result = executor.execute(commandLine);
        // Print the command output
        System.out.println(outputStream.toString());
    } catch (ExecuteException e) {
        // Catch the exception thrown when the process exits with result != 0
        System.out.println(outputStream.toString());
        LOG.catching(Level.ERROR, e);
        return LOG.exit(e.getExitValue());
    }
    return LOG.exit(result);
}

From source file:org.apache.bigtop.itest.hive.HiveHelper.java

public static Map<String, String> execCommand(CommandLine commandline, Map<String, String> envVars) {

    System.out.println("Executing command:");
    System.out.println(commandline.toString());
    Map<String, String> env = null;
    Map<String, String> entry = new HashMap<String, String>();
    try {/*ww w.j a  v a  2  s .  com*/
        env = EnvironmentUtils.getProcEnvironment();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to get process environment: " + e1.getMessage());
        e1.printStackTrace();
    }
    if (envVars != null) {
        for (String key : envVars.keySet()) {
            env.put(key, envVars.get(key));
        }
    }

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 10000);
    Executor executor = new DefaultExecutor();
    executor.setExitValue(1);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(streamHandler);
    try {
        executor.execute(commandline, env, resultHandler);
    } catch (ExecuteException e) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString() + e.getMessage());
        e.printStackTrace();
        return entry;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString() + e.getMessage());
        e.printStackTrace();
        return entry;
    }

    try {
        resultHandler.waitFor();
        /*System.out.println("Command output: "+outputStream.toString());*/
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString());
        return entry;
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        /*System.out.println("Command output: "+outputStream.toString());*/
        LOG.debug("exitValue: " + String.valueOf(resultHandler.getExitValue()));
        LOG.debug("outputStream: " + outputStream.toString());
        entry.put("exitValue", String.valueOf(resultHandler.getExitValue()));
        entry.put("outputStream", outputStream.toString());
        e.printStackTrace();
        return entry;
    }
}

From source file:org.apache.karaf.decanter.collector.system.SystemCollector.java

@Override
public void run() {
    if (properties != null) {
        String karafName = System.getProperty("karaf.name");
        String hostAddress = null;
        String hostName = null;/*from  w w  w .  j  a v  a 2s  . com*/
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
            hostName = InetAddress.getLocalHost().getHostName();
        } catch (Exception e) {
            // nothing to do
        }
        Enumeration<String> keys = properties.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            try {
                if (key.startsWith("command.")) {
                    HashMap<String, Object> data = new HashMap<>();
                    String command = (String) properties.get(key);
                    LOGGER.debug("Executing {} ({})", command, key);
                    CommandLine cmdLine = CommandLine.parse(command);
                    DefaultExecutor executor = new DefaultExecutor();
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
                    executor.setStreamHandler(streamHandler);
                    data.put("timestamp", System.currentTimeMillis());
                    data.put("type", "system");
                    data.put("karafName", karafName);
                    data.put("hostAddress", hostAddress);
                    data.put("hostName", hostName);
                    executor.execute(cmdLine);
                    outputStream.flush();
                    String output = outputStream.toString();
                    if (output.endsWith("\n")) {
                        output = output.substring(0, output.length() - 1);
                    }
                    // try to convert to number
                    try {
                        if (output.contains(".")) {
                            Double value = Double.parseDouble(output);
                            data.put(key, value);
                        } else {
                            Integer value = Integer.parseInt(output);
                            data.put(key, value);
                        }
                    } catch (NumberFormatException e) {
                        data.put(key, outputStream.toString());
                    }
                    streamHandler.stop();
                    Event event = new Event("decanter/collect/system/" + key.replace(".", "_"), data);
                    eventAdmin.postEvent(event);
                    try {
                        outputStream.close();
                    } catch (Exception e) {
                        // nothing to do
                    }
                }
            } catch (Exception e) {
                LOGGER.warn("Command {} execution failed", key, e);
            }
        }
    }
}

From source file:org.apache.karaf.decanter.kibana6.KibanaController.java

public KibanaController(File workingDirectory) {
    this.workingDirectory = workingDirectory;
    this.workingDirectory.mkdirs();
    this.executor = new DaemonExecutor();
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(new LogOutputStream() {
        @Override//from w  ww .j  a v a2  s . c  o  m
        protected void processLine(String line, int logLevel) {
            KIBANA_LOGGER.info(line);
        }
    });
    executor.setStreamHandler(pumpStreamHandler);
    executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
    executeResultHandler = new DefaultExecuteResultHandler();
}

From source file:org.apache.stratos.cartridge.agent.test.JavaCartridgeAgentTest.java

/**
 * Execute shell command//from   w w w .j  ava 2s.  c o  m
 *
 * @param commandText
 */
private ByteArrayOutputStreamLocal executeCommand(final String commandText, File workingDir) {
    final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        if (workingDir != null) {
            exec.setWorkingDirectory(workingDir);
        }
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setStreamHandler(streamHandler);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
        exec.setWatchdog(watchdog);
        log.info("Executing command: " + commandText
                + (workingDir == null ? "" : " at " + workingDir.getCanonicalPath()));
        exec.execute(commandline, new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                log.info(commandText + " process completed");
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                log.error(commandText + " process failed", e);
            }
        });
        executorList.put(commandText, exec);
        return outputStream;
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.integration.tests.SampleApplicationsTest.java

/**
 * Execute shell command/*from w  w  w .  jav  a 2 s .co m*/
 * @param commandText
 */
private void executeCommand(String commandText) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setStreamHandler(streamHandler);
        exec.execute(commandline);
        log.info(outputStream.toString());
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.integration.tests.SampleApplicationTests.java

private void executeCommand(String commandText) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {/*ww  w . ja v a2s .c  om*/
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setStreamHandler(streamHandler);
        exec.execute(commandline);
        log.info(outputStream.toString());
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.python.cartridge.agent.integration.tests.PythonAgentIntegrationTest.java

/**
 * Execute shell command//from w  w w.j  a v  a2 s.  c o  m
 *
 * @param commandText Command string to be executed
 */
protected ByteArrayOutputStreamLocal executeCommand(final String commandText, int timeout) {
    final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setWorkingDirectory(new File(PythonAgentIntegrationTest.class.getResource(PATH_SEP).getPath()
                + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME));
        exec.setStreamHandler(streamHandler);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        exec.setWatchdog(watchdog);
        exec.execute(commandline, new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                log.info(commandText + " process completed");
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                log.error(commandText + " process failed", e);
            }
        });
        executorList.put(commandText, exec);
        return outputStream;
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.python.cartridge.agent.test.PythonAgentTestManager.java

/**
 * Execute shell command//w w w . j  av a 2  s. co  m
 *
 * @param commandText
 */
protected ByteArrayOutputStreamLocal executeCommand(final String commandText) {
    final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setWorkingDirectory(new File(PythonAgentTestManager.class.getResource(PATH_SEP).getPath()
                + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME));
        exec.setStreamHandler(streamHandler);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
        exec.setWatchdog(watchdog);
        exec.execute(commandline, new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                log.info(commandText + " process completed");
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                log.error(commandText + " process failed", e);
            }
        });
        executorList.put(commandText, exec);
        return outputStream;
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}

From source file:org.apache.stratos.python.cartridge.agent.test.PythonCartridgeAgentTest.java

/**
 * Execute shell command/*w  ww .  j a  v  a2  s. c  om*/
 *
 * @param commandText
 */
private ByteArrayOutputStreamLocal executeCommand(final String commandText) {
    final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal();
    try {
        CommandLine commandline = CommandLine.parse(commandText);
        DefaultExecutor exec = new DefaultExecutor();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        exec.setStreamHandler(streamHandler);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT);
        exec.setWatchdog(watchdog);
        exec.execute(commandline, new ExecuteResultHandler() {
            @Override
            public void onProcessComplete(int i) {
                log.info(commandText + " process completed");
            }

            @Override
            public void onProcessFailed(ExecuteException e) {
                log.error(commandText + " process failed", e);
            }
        });
        executorList.put(commandText, exec);
        return outputStream;
    } catch (Exception e) {
        log.error(outputStream.toString(), e);
        throw new RuntimeException(e);
    }
}