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:npanday.plugin.wix.CandleMojo.java

public void execute() throws MojoExecutionException {
    String paths = "";
    for (int x = 0; x < sourceFiles.length; x++) {
        File f = sourceFiles[x];//from www . jav a 2 s  .co m
        if (!f.exists()) {
            throw new MojoExecutionException("Source file does not exist " + sourceFiles[x]);
        } else {
            paths = paths + sourceFiles[x].getAbsolutePath() + " ";
        }
    }

    try {
        String line = "candle " + paths;
        CommandLine commandLine = CommandLine.parse(line);
        DefaultExecutor executor = new DefaultExecutor();
        int exitValue = executor.execute(commandLine);

        if (exitValue != 0) {
            throw new MojoExecutionException("Problem executing candle, return code " + exitValue);
        }

    } catch (ExecuteException e) {
        throw new MojoExecutionException("Problem executing candle", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Problem executing candle", e);
    }
}

From source file:npanday.plugin.wix.LightMojo.java

public void execute() throws MojoExecutionException {

    String paths = "";
    for (int x = 0; x < objectFiles.length; x++) {
        File f = objectFiles[x];//w  w  w .j a  va 2s . c  om
        if (!f.exists()) {
            throw new MojoExecutionException("Object file does not exist " + objectFiles[x]);
        } else {
            paths = paths + objectFiles[x].getAbsolutePath() + " ";
        }
    }

    try {
        String line = "light " + paths;

        if (outputFile != null) {
            line = line + " -o " + outputFile;
        }

        CommandLine commandLine = CommandLine.parse(line);
        DefaultExecutor executor = new DefaultExecutor();
        int exitValue = executor.execute(commandLine);

        if (exitValue != 0) {
            throw new MojoExecutionException("Problem executing light, return code " + exitValue);
        }

    } catch (ExecuteException e) {
        throw new MojoExecutionException("Problem executing light", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Problem executing light", e);
    }
}

From source file:org.apache.drill.exec.client.DrillClient.java

/**
 * Run external script/*  ww  w . ja v  a  2  s  . c o  m*/
 *
 * @param command
 */
public void runScript(String command) {
    //System.out.println("RUNNING COMMAND: " + command);
    String sCommandString = command;
    CommandLine oCmdLine = CommandLine.parse(sCommandString);
    DefaultExecutor oDefaultExecutor = new DefaultExecutor();
    oDefaultExecutor.setExitValue(0);
    try {
        int iExitValue = oDefaultExecutor.execute(oCmdLine);
    } catch (ExecuteException e) {
        System.err.println("Execution failed.");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("permission denied.");
        e.printStackTrace();
    }
}

From source file:org.apache.falcon.regression.core.util.ExecUtil.java

public static ExecResult executeCommand(String command) {
    return executeCommand(CommandLine.parse(command));
}

From source file:org.apache.geode.example.utils.ShellUtil.java

public CommandLine parseCommandLine(String fileName) {
    return getFileFromClassLoader(fileName).map(file -> CommandLine.parse(file.getAbsolutePath()))
            .orElseThrow(IllegalArgumentException::new);
}

From source file:org.apache.geode.examples.replicated.ReplicatedTest.java

/**
 * Execute the kill script that looks for pid files
 * @throws IOException/*from   ww w . j a v a2  s .  co  m*/
 * @throws InterruptedException
 */
private void runKillScript() throws IOException, InterruptedException {
    CommandLine cmdLine = CommandLine.parse(shell.getFileFromClassLoader(pidkillerScriptFileName)
            .map(x -> x.getAbsolutePath()).orElseThrow(IllegalArgumentException::new));
    cmdLine.addArgument(testFolder.getRoot().getAbsolutePath());

    DefaultExecuteResultHandler resultHandler = shell.execute(cmdLine, scriptTimeout, environment,
            testFolder.getRoot());
    resultHandler.waitFor(scriptTimeout);
}

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;/* w ww  .  jav a2s . c o  m*/
        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 void start() throws Exception {
    String command = workingDirectory.getAbsolutePath() + File.separator + KIBANA_FOLDER + File.separator
            + "bin" + File.separator + "kibana";
    if (isWindows()) {
        command = workingDirectory.getAbsolutePath() + File.separator + KIBANA_FOLDER + File.separator + "bin"
                + File.separator + "kibana.bat";
    }/*from   www  . j a va 2  s .c o m*/
    LOGGER.debug("Starting Kibana with {}", command);
    executor.setWorkingDirectory(new File(workingDirectory, KIBANA_FOLDER));
    executor.execute(CommandLine.parse(command), executeResultHandler);
}

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

/**
 * Execute shell command/*from   w w  w .ja va 2  s  .  c  om*/
 *
 * @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 www. j  ava  2s  .  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);
    }
}