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.kercoin.magrit.core.build.BuildTask.java

private void gnomeNotifySend(int exitCode, boolean success) {
    try {//  w w  w .j  ava 2  s.c  o  m
        String message = "";
        if (success) {
            message = String.format("notify-send \"Magrit\" \"Build successful\"");
        } else {
            message = String.format("notify-send \"Magrit\" \"Build failed %s\"", exitCode);
        }
        new DefaultExecutor().execute(CommandLine.parse(message));
    } catch (ExecuteException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.mail.bridge.FolderMonitor.java

public synchronized void runScriptAgainstReceivedFiles(List<File> inboxFiles) {
    if (config.getInboxScript().isEmpty() || Utils.isEmpty(inboxFiles))
        return;/*from w w w. j  av  a2 s .  c  om*/
    LOG.debug("Run script '{}' against files {}", config.getInboxScript(), inboxFiles);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        CommandLine cmd = CommandLine.parse(config.getInboxScript());
        for (File file : inboxFiles)
            cmd.addArgument(file.getName(), true);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(out));
        executor.setWatchdog(new ExecuteWatchdog(SCRIPT_TIMEOUT));
        Map<String, String> environment = EnvironmentUtils.getProcEnvironment();
        environment.putAll(config.asEnvironmentMap());
        executor.setWorkingDirectory(new File(System.getProperty("user.dir")));
        executor.execute(cmd, environment);
        LOG.info("Script '{}' successfully finished", config.getInboxScript());
        LOG.debug("Script output:\n{}", out.toString());
    } catch (ExecuteException e) {
        LOG.error(e.getMessage(), e);
        LOG.error("\nScript '{}' output:\n{}", config.getInboxScript(), out.toString());
        int c = config.getInboxScriptStopCode();
        if (c != 0 && c == e.getExitValue())
            postMessage(new Main.StopMessage(
                    String.format("Script '%s' exited with code %d that is configured as stop code",
                            config.getInboxScript(), c)));
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
        LOG.error("\nScript '{}' output:\n{}", config.getInboxScript(), out.toString());
    }
}

From source file:org.moe.cli.utils.Utils.java

public static String[] execute(File dir, String command, Map<String, String> environment) {

    Map<String, String> current = null;
    try {//  w w w . j  a va2  s  .  c o m
        current = EnvironmentUtils.getProcEnvironment();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    if (environment != null && current != null) {
        current.putAll(environment);
    }

    CommandLine cmdLine = CommandLine.parse(command);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(dir);

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    PumpStreamHandler stHandler = new PumpStreamHandler(stdout, stderr);

    executor.setStreamHandler(stHandler);
    int exitValue = Executor.INVALID_EXITVALUE;

    try {
        exitValue = executor.execute(cmdLine, current);
    } catch (IOException e) {
    }

    return new String[] { stdout.toString(), executor.isFailure(exitValue) ? stderr.toString() : null };
}

From source file:org.ms123.common.management.ManagementServiceImpl.java

private int exec(String line, ByteArrayOutputStream outputStream, ByteArrayOutputStream outputErr,
        int[] exitValues) throws Exception {
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValues(exitValues);/*from   w w w .j  a  v  a2 s.c o  m*/
    PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputErr);
    executor.setStreamHandler(streamHandler);
    int exitValue = executor.execute(cmdLine);
    return exitValue;
}

From source file:org.mule.test.infrastructure.process.MuleUtils.java

public static int executeCommand(String command, String... envVars) throws IOException {
    CommandLine cmdLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    Map<String, String> env = addEnvProperties(envVars);
    ExecuteWatchdog watchDog = new ExecuteWatchdog(TIMEOUT);
    executor.setWatchdog(watchDog);//from   w ww  .j  a va  2 s  .com
    executor.setStreamHandler(new PumpStreamHandler());
    int result = executor.execute(cmdLine, env);
    if (executor.isFailure(result)) {
        if (watchDog.killedProcess()) {
            throw new RuntimeException("Reached timeout while running: " + cmdLine);
        }
        throw new RuntimeException("Process failed with return code [" + result + "]: " + cmdLine);
    }
    return result;
}

From source file:org.mule.tooling.jubula.cliexecutor.internal.DefaultCliExecutor.java

@Override
public int run(final File executable, final String... params) {
    try {/*from   www . j a v a2s  .  c  o m*/
        final DefaultExecutor executor = new DefaultExecutor();
        final CommandLine command = CommandLine.parse(executable.getAbsolutePath());
        command.addArguments(params, true);
        return executor.execute(command);
    } catch (final ExecuteException e) {
        throw new RuntimeException(e);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.mule.tooling.jubula.cliexecutor.internal.DefaultCliExecutor.java

@Override
public void runAsync(final String commandString, final Callback callback, final String... params) {
    try {//from  w w w.ja v a  2  s .c  o  m
        final DefaultExecutor executor = new DefaultExecutor();
        final CommandLine command = CommandLine.parse(commandString);
        command.addArguments(params, true);
        executor.execute(command, new ExecuteResultHandler() {

            @Override
            public void onProcessFailed(final ExecuteException returnCode) {
                callback.failure(returnCode.getExitValue());
            }

            @Override
            public void onProcessComplete(final int returnCode) {
                callback.success(returnCode);
            }
        });
    } catch (final ExecuteException e) {
        throw new RuntimeException(e);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.mybatis.generator.ext.api.MeldMergeShellCallback.java

public static boolean exec(String command) {
    CommandLine commandLine = CommandLine.parse(command);
    DefaultExecutor executor = new DefaultExecutor();
    int exitValue = DefaultExecutor.INVALID_EXITVALUE;
    ExecuteWatchdog watchdog = new ExecuteWatchdog(2000);
    executor.setWatchdog(watchdog);//from w  w w . j  a  v a 2  s .  c  o  m
    try {
        exitValue = executor.execute(commandLine);
    } catch (ExecuteException e) {
        exitValue = e.getExitValue();
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }
    System.out.println("Exit :" + exitValue);
    return (exitValue == 0);
}

From source file:org.n52.movingcode.runtime.processors.python.PythonCLIProbe.java

public static boolean testExecutable() {
    CommandLine commandLine = CommandLine.parse(PythonCLIProcessor.pythonExecutable + " --version");

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    Executor executor = new DefaultExecutor();

    // put a watchdog with a timeout
    ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(timeout_seconds) * 1000);
    executor.setWatchdog(watchdog);//from   w w  w  .  j  a  va2s . c o  m

    try {
        executor.execute(commandLine, resultHandler);
        resultHandler.waitFor();
        int exitVal = resultHandler.getExitValue();
        if (exitVal != 0) {
            return false;
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:org.n52.movingcode.runtime.processors.python.PythonCLIProbe.java

public static String getVersion() {

    try {/* w  w w.j  ava2 s . c om*/
        URL scriptURL = PythonCLIProbe.class.getResource(versionScriptFile);
        File sf = new File(scriptURL.toURI());
        String scriptPath = sf.getAbsolutePath();

        CommandLine commandLine = CommandLine.parse(PythonCLIProcessor.pythonExecutable + " " + scriptPath);

        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        Executor executor = new DefaultExecutor();

        // put a watchdog with a timeout
        ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(timeout_seconds) * 1000);
        executor.setWatchdog(watchdog);

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

        executor.execute(commandLine, resultHandler);
        resultHandler.waitFor();
        int exitVal = resultHandler.getExitValue();
        if (exitVal != 0) {
            return null;
        }

        return (os.toString());

    } catch (Exception e) {
        return null;
    }
}