Example usage for org.apache.commons.exec CommandLine parse

List of usage examples for org.apache.commons.exec CommandLine parse

Introduction

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

Prototype

public static CommandLine parse(final String line) 

Source Link

Document

Create a command line from a string.

Usage

From source file:org.cloudifysource.dsl.context.utils.VolumeUtils.java

private static void executeCommandLine(final String commandLine, final long timeout)
        throws LocalStorageOperationException, TimeoutException {

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);//from  w ww  .  j av a 2  s .com
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    ProcessOutputStream outAndErr = new ProcessOutputStream();
    try {
        PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr);
        executor.setStreamHandler(streamHandler);
        logger.info("Executing commandLine : '" + commandLine + "'");
        executor.execute(CommandLine.parse(commandLine));
        logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput());
    } catch (final Exception e) {
        if (watchdog.killedProcess()) {
            throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'");
        }
        throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine
                + ". Process output was : " + outAndErr.getOutput(), e);
    }
}

From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java

private static void executeCommandLine(final String commandLine, final long timeout)
        throws LocalStorageOperationException, TimeoutException {

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);/*from w w w.  j a v  a 2s  . c om*/
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    ProcessOutputStream outAndErr = new ProcessOutputStream();
    try {
        PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr);
        executor.setStreamHandler(streamHandler);
        logger.info("Executing commandLine : '" + commandLine + "'");
        executor.execute(CommandLine.parse(commandLine));
        logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput());
    } catch (final Exception e) {
        if (watchdog.killedProcess()) {
            throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'");
        }

        throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine
                + ". Process output was : " + outAndErr.getOutput(), e);
    }
}

From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java

private static String executeSilentCommandLineReturnOutput(final String commandLine, final long timeout)
        throws LocalStorageOperationException, TimeoutException {

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);// ww  w .ja v  a2 s  .c  o m
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(watchdog);
    ProcessOutputStream outAndErr = new ProcessOutputStream();
    try {
        PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr);
        executor.setStreamHandler(streamHandler);
        executor.execute(CommandLine.parse(commandLine));
    } catch (final Exception e) {
        if (watchdog.killedProcess()) {
            throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'");
        }

        throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine
                + ". Process output was : " + outAndErr.getOutput(), e);
    }

    return outAndErr.getOutput();
}

From source file:org.dataconservancy.dcs.access.server.util.VivoUtil.java

public static String cmdExec(String query, String sparqlEndpoint) {
    String cmd = "curl -s -S -X POST --data-binary \"" + query + "\" " + sparqlEndpoint;
    CommandLine cmdLine = CommandLine.parse(cmd);
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);

    executor.setStreamHandler(psh);/*from w w w  . j av  a 2s  . c  o  m*/

    int exitValue;
    try {
        exitValue = executor.execute(cmdLine);
    } catch (ExecuteException e) {
        //logger.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        //.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
    }

    //String str = "";
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(stdout.toByteArray())));
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    String output_line = null;
    String xml = "";
    try {
        int flag = 0;

        while ((output_line = br.readLine()) != null) {
            if (output_line.contains("--:--")) {
                if (output_line.contains("<?xml version"))
                    output_line = output_line.substring(output_line.indexOf("<?xml version"));
                else
                    continue;
            }
            xml += output_line;

        }

    } catch (IOException e) {
        //logger.log(Level.SEVERE, e.getMessage());
        e.printStackTrace();
    }
    return xml;
}

From source file:org.dataconservancy.dcs.id.impl.DataciteIdService.java

ByteArrayOutputStream executeCommand(String command) throws IOException {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);

    executor.setStreamHandler(psh);/* w  w  w .j a v a2s  .  c o  m*/

    int exitValue = executor.execute(cmdLine);
    return stdout;
}

From source file:org.eclipse.kura.linux.net.modem.SupportedUsbModems.java

/**
 * Execute command an return splitted lines
 *
 * @param command/*from ww  w.j a  v a2s.c  o m*/
 *            the command to execute
 * @return the lines output by the command
 * @throws IOException
 *             if executing the commands fails
 */
private static List<String> execute(final String command) throws ExecuteException, IOException {
    final DefaultExecutor executor = new DefaultExecutor();

    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(out, NullOutputStream.NULL_OUTPUT_STREAM));

    int rc = executor.execute(CommandLine.parse(command));

    s_logger.debug("Called {} - rc = {}", command, rc);

    return IOUtils.readLines(new ByteArrayInputStream(out.toByteArray()));
}

From source file:org.eclipse.php.composer.core.launch.execution.ScriptExecutor.java

public void execute(String cmd) throws ExecuteException, IOException, InterruptedException {
    execute(CommandLine.parse(cmd));
}

From source file:org.eclipse.smarthome.io.net.exec.ExecUtil.java

/**
 * <p>//from w w w .  j a  v a 2  s.  co  m
 * Executes <code>commandLine</code>. Sometimes (especially observed on
 * MacOS) the commandLine isn't executed properly. In that cases another
 * exec-method is to be used. To accomplish this please use the special
 * delimiter '<code>@@</code>'. If <code>commandLine</code> contains this
 * delimiter it is split into a String[] array and the special exec-method
 * is used.
 * </p>
 * <p>
 * A possible {@link IOException} gets logged but no further processing is
 * done.
 * </p>
 * 
 * @param commandLine
 *            the command line to execute
 * @param timeout
 *            timeout for execution in milliseconds
 * @return response data from executed command line
 */
public static String executeCommandLineAndWaitResponse(String commandLine, int timeout) {
    String retval = null;

    CommandLine cmdLine = null;

    if (commandLine.contains(CMD_LINE_DELIMITER)) {
        String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER);
        cmdLine = new CommandLine(cmdArray[0]);

        for (int i = 1; i < cmdArray.length; i++) {
            cmdLine.addArgument(cmdArray[i], false);
        }
    } else {
        cmdLine = CommandLine.parse(commandLine);
    }

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    Executor executor = new DefaultExecutor();

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout);

    executor.setExitValue(1);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(watchdog);

    try {
        executor.execute(cmdLine, resultHandler);
        logger.debug("executed commandLine '{}'", commandLine);
    } catch (ExecuteException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    } catch (IOException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    }

    // some time later the result handler callback was invoked so we
    // can safely request the exit code
    try {
        resultHandler.waitFor();
        int exitCode = resultHandler.getExitValue();
        retval = StringUtils.chomp(stdout.toString());
        logger.debug("exit code '{}', result '{}'", exitCode, retval);

    } catch (InterruptedException e) {
        logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e);
    }

    return retval;
}

From source file:org.estatio.webapp.services.other.EstatioOtherServices.java

@Programmatic
String execute(String command) {

    int exitValue = 1;
    CommandLine commandLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);//  w w  w.  j  av  a 2 s  . c  o  m
    ExecuteStreamHandler handler = executor.getStreamHandler();

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout);
    executor.setStreamHandler(psh);

    try {
        handler.setProcessOutputStream(System.in);
    } catch (IOException e) {

    }

    try {
        exitValue = executor.execute(commandLine);
    } catch (ExecuteException e) {
        return e.getMessage();
    } catch (IOException e) {
        return e.getMessage();
    }
    return stdout.toString();
}

From source file:org.evosuite.utils.ProcessLauncher.java

public int launchNewProcess(File baseDir, String cmdString, int timeout)
        throws IOException, ProcessTimeoutException {

    DefaultExecutor executor = new DefaultExecutor();
    ExecuteWatchdog timeoutWatchdog = new ExecuteWatchdog(timeout);
    executor.setWatchdog(timeoutWatchdog);

    PumpStreamHandler streamHandler = new PumpStreamHandler(this.outAndErr, this.outAndErr, this.input);
    executor.setStreamHandler(streamHandler);
    if (baseDir != null) {
        executor.setWorkingDirectory(baseDir);
    }/* www  .  j  a  v  a2  s  .c o  m*/

    int exitValue;
    try {
        logger.debug("About to execute command " + cmdString);
        exitValue = executor.execute(CommandLine.parse(cmdString));
        if (executor.isFailure(exitValue) && timeoutWatchdog.killedProcess()) {
            // it was killed on purpose by the watchdog
            logger.debug("A timeout occured while executing a process");
            logger.debug("The command is " + cmdString);
            throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString);
        }

        return exitValue;
    } catch (ExecuteException e) {
        if (timeoutWatchdog.killedProcess()) {
            logger.debug("A timeout occured while executing a process");
            logger.debug("The command is " + cmdString);
            throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString);
        } else {
            throw e;
        }

    }

}