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:eu.forgestore.ws.util.Utils.java

public static int executeSystemCommand(String cmdStr) {

    CommandLine cmdLine = CommandLine.parse(cmdStr);
    final Executor executor = new DefaultExecutor();
    // create the executor and consider the exitValue '0' as success
    executor.setExitValue(0);//from   w  ww .  j ava2 s . co  m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(out);
    executor.setStreamHandler(streamHandler);

    int exitValue = -1;
    try {
        exitValue = executor.execute(cmdLine);

    } catch (ExecuteException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

    return exitValue;

}

From source file:latexstudio.editor.runtime.CommandLineExecutor.java

public static synchronized void executeGeneratePDF(CommandLineBuilder cmd) {
    String outputDirectory = "--output-directory=" + cmd.getOutputDirectory();
    String outputFormat = "--output-format=pdf";
    String job = cmd.getJobname() == null ? "" : "--jobname=" + cmd.getJobname().replaceAll(" ", "_");
    ByteArrayOutputStream outputStream = null;

    try {// w w w  .  j a va  2  s .  co m
        String[] command = new String[] { outputDirectory, outputFormat, job, cmd.getPathToSource() };

        CommandLine cmdLine = new CommandLine(ApplicationUtils.getPathToTEX(cmd.getLatexPath()));
        //For windows, we set handling quoting to true
        cmdLine.addArguments(command, ApplicationUtils.isWindows());

        outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        EXECUTOR.setStreamHandler(streamHandler);

        if (cmd.getWorkingFile() != null) {
            EXECUTOR.setWorkingDirectory(cmd.getWorkingFile().getParentFile().getAbsoluteFile());
        }

        EXECUTOR.execute(cmdLine);

        if (cmd.getLogger() != null) {
            cmd.getLogger().log(cmdLine.toString());
            cmd.getLogger().log(outputStream.toString());
        }

    } catch (IOException e) {
        if (cmd.getLogger() != null) {
            cmd.getLogger().log("The path to the pdflatex tool is incorrect or has not been set.");
        }
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

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

public static String exec(CommandLine commandline, long timeoutInMillies) throws ShellCommandException {
    log.info("executing command: " + commandline);
    PritingLogOutputStream outputStream = new PritingLogOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(new ExecuteWatchdog(timeoutInMillies));
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    try {//from w ww .  ja v  a 2  s . co  m
        exec.execute(commandline);
    } catch (Exception e) {
        throw new ShellCommandException("An error occured while executing shell command: " + commandline,
                new ShellCommandException(outputStream.getOutput()));
    }
    return (outputStream.getOutput());
}

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

public static String exec(CommandLine commandline, long timeoutInMillies) throws ShellCommandException {
    log.info("Executing shell command: " + commandline);
    PrintingLogOutputStream outputStream = new PrintingLogOutputStream();
    DefaultExecutor exec = new DefaultExecutor();
    exec.setWatchdog(new ExecuteWatchdog(timeoutInMillies));
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    exec.setStreamHandler(streamHandler);
    try {/*from  ww  w .j  a va 2  s  .  c o m*/
        exec.execute(commandline);
    } catch (Exception e) {
        log.log(Level.SEVERE, "Error executing command: " + commandline, e);
        if (e.getMessage().contains("device offline")) {
            throw new DeviceOfflineException(e);
        }
        throw new ShellCommandException("Error executing shell command: " + commandline,
                new ShellCommandException(outputStream.getOutput()));
    }
    String result = outputStream.getOutput().trim();
    log.info("Shell command output\n-->\n" + result + "\n<--");
    return result;
}

From source file:com.bptselenium.jenkins.BPTSeleniumJenkins.RunCommand.java

public static void runCommand(String command, TaskListener listener, Run<?, ?> build) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CommandLine cmdLine = CommandLine.parse(command);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    Executor executor = new DefaultExecutor();
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
    executor.setStreamHandler(streamHandler);
    executor.setExitValue(10);//w  ww  .  j av  a  2  s .  c  om
    executor.setWatchdog(watchdog);
    try {
        executor.execute(cmdLine);
    } catch (ExecuteException ee) {
        //getting a non-standard execution value, set build result to unstable
        Result result = Result.UNSTABLE;
        if (build.getResult() == null) {
            build.setResult(result);
        } else if (build.getResult().isBetterThan(result)) {
            build.setResult(result.combine(build.getResult()));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        listener.getLogger().println(outputStream.toString());
    }
}

From source file:fitnesse.maven.io.CommandShell.java

public String execute(File workingDir, String... commands) {
    CommandLine commandLine = CommandLine.parse(commands[0]);
    for (int i = 1; i < commands.length; i++) {
        commandLine.addArgument(commands[i]);
    }/*from  w  w  w. j a  v a 2 s  . co m*/
    DefaultExecutor executor = new DefaultExecutor();
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        executor.setStreamHandler(new PumpStreamHandler(baos));
        executor.setWorkingDirectory(workingDir);
        executor.execute(commandLine);
        return new String(baos.toByteArray());
    } catch (ExecuteException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.comcast.tvx.haproxy.HAProxyServiceController.java

@Override
public int reload() {
    if (!new File("/etc/init.d/haproxy").exists()) {
        logger.info("HaProxy is not installed");
        throw new IllegalArgumentException("HaProxy is not installed");
    }/* ww w  . j a  v a 2 s. c  om*/

    CommandLine cmdLine = new CommandLine("sudo");
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    cmdLine.addArgument("service");
    cmdLine.addArgument("haproxy");
    cmdLine.addArgument("reload");

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(new int[] { 0, 1 });

    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(psh);

    int exitValue;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (ExecuteException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    logger.info("output from running process: " + stdout.toString());
    logger.info("Exit value was: " + exitValue);
    return exitValue;

}

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);/*from  www  . j av a  2s.c  o m*/

    return handler;
}

From source file:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java

protected List<String> getPorts(String executable) {
    List<String> ports = new ArrayList<>();
    try {/*  ww w .j  a  v  a  2s  .c  o  m*/
        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-P");
        DefaultExecutor executor = new DefaultExecutor();
        //executor.setExitValues(new int[]{451, 301});

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            return ports;
        } finally {
            output = outputStream.toString();
        }

        /*
         if (exitValue == 301) {
         return ports;
         }
         */
        //            System.out.println("output: " + output);
        Scanner scanner = new Scanner(output);

        while (scanner.hasNextLine()) {
            ports.add(scanner.nextLine());
        }

        return ports;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        return null;
    }
}

From source file:eu.creatingfuture.propeller.webLoader.propeller.PropellerLoad.java

protected List<String> getPorts(String executable) {
    List<String> ports = new ArrayList<String>();
    try {//  w w  w  .j a  v a 2 s . c o  m
        CommandLine cmdLine = new CommandLine(executable);
        cmdLine.addArgument("-P");
        DefaultExecutor executor = new DefaultExecutor();
        //executor.setExitValues(new int[]{451, 301});

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            return ports;
        } finally {
            output = outputStream.toString();
        }

        /*
         if (exitValue == 301) {
         return ports;
         }
         */
        //            System.out.println("output: " + output);
        Scanner scanner = new Scanner(output);

        while (scanner.hasNextLine()) {
            ports.add(scanner.nextLine());
        }

        return ports;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        return null;
    }
}