Example usage for org.apache.commons.exec DefaultExecutor setExitValue

List of usage examples for org.apache.commons.exec DefaultExecutor setExitValue

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecutor setExitValue.

Prototype

public void setExitValue(final int value) 

Source Link

Usage

From source file:org.wisdom.maven.utils.WisdomExecutor.java

/**
 * Launches the Wisdom server. This method blocks until the wisdom server shuts down.
 * It uses the {@literal Java} executable directly.
 *
 * @param mojo        the mojo//from   w  w w. j ava2  s.c o  m
 * @param interactive enables the shell prompt
 * @param debug       the debug port (0 to disable it)
 * @param jvmArgs     JVM arguments to add to the `java` command (before the -jar argument).
 * @param destroyer   a process destroyer that can be used to destroy the process, if {@code null}
 *                    a {@link org.apache.commons.exec.ShutdownHookProcessDestroyer} is used.
 * @throws MojoExecutionException if the Wisdom instance cannot be started or has thrown an unexpected status
 *                                while being stopped.
 */
public void execute(AbstractWisdomMojo mojo, boolean interactive, int debug, String jvmArgs,
        ProcessDestroyer destroyer) throws MojoExecutionException {
    // Get java
    File java = ExecUtils.find("java", new File(mojo.javaHome, "bin"));
    if (java == null) {
        throw new MojoExecutionException("Cannot find the java executable");
    }

    CommandLine cmdLine = new CommandLine(java);

    if (debug != 0) {
        cmdLine.addArgument("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=" + debug, false);
    }

    if (!Strings.isNullOrEmpty(jvmArgs)) {
        cmdLine.addArguments(jvmArgs, false);
    }

    cmdLine.addArgument("-jar");
    cmdLine.addArgument("bin/chameleon-core-" + CHAMELEON_VERSION + ".jar");
    if (interactive) {
        cmdLine.addArgument("--interactive");
    }

    appendSystemPropertiesToCommandLine(mojo, cmdLine);

    DefaultExecutor executor = new DefaultExecutor();
    if (destroyer != null) {
        executor.setProcessDestroyer(destroyer);
    } else {
        executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    }

    executor.setWorkingDirectory(mojo.getWisdomRootDirectory());
    if (interactive) {
        executor.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in)); //NOSONAR
        // Using the interactive mode the framework should be stopped using the 'exit' command,
        // and produce a '0' status.
        executor.setExitValue(0);
    } else {
        executor.setStreamHandler(new PumpStreamHandler(System.out, System.err)); // NOSONAR
        // As the execution is intended to be interrupted using CTRL+C, the status code returned is expected to be 1
        // 137 or 143 is used when stopped by the destroyer.
        executor.setExitValues(new int[] { 1, 137, 143 });
    }
    try {
        mojo.getLog().info("Launching Wisdom Server");
        mojo.getLog().debug("Command Line: " + cmdLine.toString());
        // The message is different whether or not we are in the interactive mode.
        if (interactive) {
            mojo.getLog().info("You are in interactive mode");
            mojo.getLog().info("Hit 'exit' to shutdown");
        } else {
            mojo.getLog().info("Hit CTRL+C to exit");
        }
        if (debug != 0) {
            mojo.getLog().info("Wisdom launched with remote debugger interface enabled on port " + debug);
        }
        // Block execution until ctrl+c
        executor.execute(cmdLine);
    } catch (IOException e) {
        throw new MojoExecutionException("Cannot execute Wisdom", e);
    }
}

From source file:org.wisdom.ractivejs.RactiveJsTemplateCompilerMojo.java

/**
 * Parse the ractive template into a JavaScript file.
 * Run the ractive script from the plugin resource with node, and ractive module.
 *
 * @param template the ractive template file
 * @throws WatchingException if the template cannot be parsed
 *//*  www.j av a2s  .  co m*/
private void parseTemplate(File template) throws WatchingException {
    File destination = getOutputJSFile(template);

    // Create the destination folder.
    if (!destination.getParentFile().isDirectory()) {
        destination.getParentFile().mkdirs();
    }

    // Parse with Ractive.js
    CommandLine cmdLine = new CommandLine(getNodeManager().getNodeExecutable());
    cmdLine.addArgument(ractiveExec.getAbsolutePath(), false);
    cmdLine.addArgument(ractiveModule.getAbsolutePath(), false);
    cmdLine.addArgument(template.getAbsolutePath(), false);
    cmdLine.addArgument(destination.getAbsolutePath(), false);

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);

    PumpStreamHandler streamHandler = new PumpStreamHandler(new LoggedOutputStream(getLog(), false),
            new LoggedOutputStream(getLog(), true));

    executor.setStreamHandler(streamHandler);

    getLog().info("Executing " + cmdLine.toString());

    try {
        executor.execute(cmdLine);
    } catch (IOException e) {
        throw new WatchingException("Error during the execution of " + RACTIVE_SCRIPT_NPM_NAME, e);
    }
}