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.n52.movingcode.runtime.processors.python.PythonCLIProcessor.java

/**
 * Creates a CommandLine Object for execution
 * //from  ww w.  j  a v  a  2 s  . c om
 * @param paramSet
 *        - the parameter specification
 * @param executionValues
 *        - the values for the parameters
 * @return CommandLine - an executable CommandLine
 */
private static CommandLine buildCommandLine(String executable, SortedMap<ParameterID, String[]> executionValues,
        SortedMap<ParameterID, IOParameter> paramMap) throws IllegalArgumentException {

    CommandLine commandLine = CommandLine.parse(executable);

    // assemble commandLine with values, separators and all that stuff
    for (ParameterID identifier : executionValues.keySet()) {
        String argument = "";
        // 1. add prefix
        argument = argument + paramMap.get(identifier).printPrefix();
        // 2. add values with subsequent separator
        for (String value : executionValues.get(identifier)) {
            argument = argument + value + paramMap.get(identifier).printSeparator();
        }
        // remove last occurrence of separator
        if (argument.length() != 0) {
            argument = argument.substring(0,
                    argument.length() - paramMap.get(identifier).printSeparator().length());
        }
        // 3. add suffix
        argument = argument + paramMap.get(identifier).printSuffix();
        // 4. add to argument to CommandLine
        commandLine.addArgument(argument, false);
    }

    return commandLine;
}

From source file:org.n52.movingcode.runtime.processors.r.RCLIProbe.java

private static boolean testExecutable() {
    CommandLine commandLine = CommandLine.parse(RCLIProcessor.rExecutable + " " + VERSION_CALL);

    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);/*w  w  w . j av a 2 s .c  om*/

    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.r.RCLIProbe.java

private static String getVersion() {
    try {/*from w  w  w.j a v  a2 s  . c  o  m*/
        CommandLine commandLine = CommandLine.parse(RCLIProcessor.rExecutable + " " + VERSION_CALL);

        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;
        }

        String osString = os.toString();

        String versionString = osString.substring(osString.lastIndexOf(": ") + 2);
        versionString = versionString.substring(0, versionString.indexOf('\n'));

        return (versionString);
    } catch (Exception e) {
        LOGGER.error("Could not get version string.", e);
        return null;
    }
}

From source file:org.nanoko.coffee.mill.mojos.reporting.JsDocMojo.java

private void generateJSDOC() throws MavenReportException {
    if (skipJSDOC) {
        getLog().info("JSDoc report generation skipped");
        return;//from  w w  w  .  j a  v a 2s.  c  o m
    }

    File jsdocExec = ExecUtils.findExecutableInPath("jsdoc");
    if (jsdocExec == null) {
        getLog().error("Cannot build jsdoc report - jsdoc not in the system path, the report is ignored.");
        return;
    } else {
        getLog().info("Invoking jsdoc : " + jsdocExec.getAbsolutePath());
        getLog().info("Output directory : " + getOutputDirectory());
    }

    File out = new File(getOutputDirectory());
    out.mkdirs();

    CommandLine cmdLine = CommandLine.parse(jsdocExec.getAbsolutePath());

    // Destination
    cmdLine.addArgument("--destination");
    cmdLine.addArgument(out.getAbsolutePath());

    if (jsdocIncludePrivate) {
        cmdLine.addArgument("--private");
    }

    File input = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".js");
    if (!input.exists()) {
        throw new MavenReportException("Cannot find the project's artifact : " + input.getAbsolutePath());
    }
    cmdLine.addArgument(input.getAbsolutePath());

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(project.getBasedir());
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);
    } catch (IOException e) {
        throw new MavenReportException("Error during jsdoc report generation", e);
    }

}

From source file:org.nanoko.coffee.mill.processors.JpegTranProcessor.java

private void optimize(File file) throws ProcessorException {
    File dir = file.getParentFile();

    // Build command line
    CommandLine cmdLine = CommandLine.parse(jpegTranExec.getAbsolutePath());

    if (verbose) {
        cmdLine.addArgument("-verbose");
    }/*from w w w. j  av a 2 s.  c om*/

    cmdLine.addArgument("-copy");
    cmdLine.addArgument("none");

    cmdLine.addArgument("-optimize");

    cmdLine.addArgument("-outfile");
    cmdLine.addArgument("out.jpeg");

    cmdLine.addArgument(file.getName());

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(dir);
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);

        // Overwrite the original file
        File out = new File(dir, "out.jpeg");
        if (out.exists()) {
            FileUtils.copyFile(new File(dir, "out.jpeg"), file);
        } else {
            throw new IOException("Output file not found : " + out.getAbsolutePath());
        }

        getLog().info(file.getName() + " optimized");
    } catch (IOException e) {
        throw new ProcessorException("Error during JPG optimization of " + file.getAbsolutePath(), e);
    }
}

From source file:org.nanoko.coffee.mill.processors.OptiPNGProcessor.java

private void optimize(File file) throws ProcessorException {
    File dir = file.getParentFile();

    // Build command line
    CommandLine cmdLine = CommandLine.parse(optiPNGExec.getAbsolutePath());
    cmdLine.addArgument(file.getName());

    if (verbose) {
        cmdLine.addArgument("-v");
    }//from   w  ww .  ja va 2s. c  o  m

    cmdLine.addArgument("-o" + level);

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(dir);
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);
        getLog().info(file.getName() + " optimized");
    } catch (IOException e) {
        throw new ProcessorException("Error during PNG optimization of " + file.getAbsolutePath(), e);
    }
}

From source file:org.nanoko.coffeemill.mojos.processresources.OptiJpegMojo.java

private void optimize(File file) throws WatchingException {
    File dir = file.getParentFile();

    // Build command line
    CommandLine cmdLine = CommandLine.parse(jpegTranExec.getAbsolutePath());

    if (verbose) {
        cmdLine.addArgument("-verbose");
    }/*from  w w w. j  a v  a 2 s. c om*/

    cmdLine.addArgument("-copy");
    cmdLine.addArgument("none");

    cmdLine.addArgument("-optimize");

    cmdLine.addArgument("-outfile");
    cmdLine.addArgument("__out.jpeg");

    cmdLine.addArgument(file.getName());

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(dir);
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);

        // Overwrite the original file
        File out = new File(dir, "__out.jpeg");

        getLog().info("output jpeg file : " + out.getAbsolutePath());
        if (out.exists()) {
            FileUtils.copyFile(out, file);
            FileUtils.deleteQuietly(out);
        } else {
            throw new IOException("Output file not found : " + out.getAbsolutePath());
        }

        getLog().info(file.getName() + " optimized");
    } catch (IOException e) {
        throw new WatchingException("Error during JPG optimization of " + file.getAbsolutePath(), e);
    }
}

From source file:org.nanoko.coffeemill.mojos.processresources.OptiPngMojo.java

private void optimize(File file) throws WatchingException {
    File dir = file.getParentFile();

    // Build command line
    CommandLine cmdLine = CommandLine.parse(optiPNGExec.getAbsolutePath());
    cmdLine.addArgument(file.getName());

    if (verbose) {
        cmdLine.addArgument("-v");
    }//from w  w  w.  j  a  v  a 2s  .com

    cmdLine.addArgument("-o" + level);

    DefaultExecutor executor = new DefaultExecutor();

    executor.setWorkingDirectory(dir);
    executor.setExitValue(0);
    try {
        getLog().info("Executing " + cmdLine.toString());
        executor.execute(cmdLine);
        getLog().info(file.getName() + " optimized");
    } catch (IOException e) {
        throw new WatchingException("Error during PNG optimization of " + file.getAbsolutePath(), e);
    }
}

From source file:org.nanoko.playframework.mojo.AbstractPlay2SimpleMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    String line = this.getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    this.addCommandLineArgs(cmdLine);

    DefaultExecutor executor = new DefaultExecutor();
    if (this.timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(this.timeout);
        executor.setWatchdog(watchdog);//w  ww  . ja v a 2 s .  c  om
    }

    executor.setExitValue(0);
    executor.setWorkingDirectory(this.project.getBasedir());
    try {
        executor.execute(cmdLine, this.getEnvironment());
    } catch (Exception e) {
        this.onExecutionException(e);
    }
}

From source file:org.nanoko.playframework.mojo.Play2DebugMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);

    cmdLine.addArgument("debug");
    cmdLine.addArguments(getPlay2SystemPropertiesArguments(), false);
    cmdLine.addArgument("run");
    DefaultExecutor executor = new DefaultExecutor();

    // As where not linked to a project, we can't set the working directory.
    // So it will use the directory where mvn was launched.

    executor.setExitValue(0);//  w  ww  .j a  v  a  2 s.c  om
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        // Ignore.
    }
}