Example usage for org.apache.commons.exec Executor INVALID_EXITVALUE

List of usage examples for org.apache.commons.exec Executor INVALID_EXITVALUE

Introduction

In this page you can find the example usage for org.apache.commons.exec Executor INVALID_EXITVALUE.

Prototype

int INVALID_EXITVALUE

To view the source code for org.apache.commons.exec Executor INVALID_EXITVALUE.

Click Source Link

Document

Invalid exit code.

Usage

From source file:com.arhs.mojo.pack200.AbstractPluginMojo.java

/**
 * Executes command.//from w ww  .  j a  va2s .  c  o  m
 *
 * @throws IOException Execution of subprocess failed or the subprocess returned a exit value indicating a failure
 *                     {@code Executor.setExitValue(int)}.
 * @see AbstractPluginMojo#command
 * @see AbstractPluginMojo#executor
 */
private void executeCommand() throws IOException, MojoExecutionException {
    if (debug) {
        getLog().info("Executable: " + command.getExecutable());
        getLog().info("Arguments: " + StringUtils.join(command.getArguments(), " "));
    }

    int processExitValue = executor.execute(command);
    if (debug) {
        getLog().info(String.format("Process returned %d (0x%01X)", processExitValue, processExitValue));
    }

    if (processExitValue == Executor.INVALID_EXITVALUE) {
        throw new MojoExecutionException("An error occurred during the execution of the program.");
    }
}

From source file:com.github.zeroxff.executor.ExtendedExecutor.java

public ExecuteResult execute() throws ExtendedExecuteException {
    this.clearOut();
    if (this.commandLine == null) {
        throw new ExtendedExecuteException("CommandLine cannot be null", Executor.INVALID_EXITVALUE);
    }/*from w  w w  .  ja v a  2 s .  c om*/
    if (this.commandLine.length == 0) {
        throw new ExtendedExecuteException("CommandLine cannot be empty", Executor.INVALID_EXITVALUE);
    }
    if (this.maxExecutiontime != ExecuteWatchdog.INFINITE_TIMEOUT && this.maxExecutiontime < 1) {
        throw new ExtendedExecuteException("Max execution time must not be less than 1",
                Executor.INVALID_EXITVALUE);
    }

    try {
        // load the command line as an array of strings
        CommandLine cmdLine = new CommandLine(this.commandLine[0]);
        for (int counter = 1; counter < commandLine.length; counter++) {
            cmdLine.addArgument(this.commandLine[counter], quoteCommandlineArgs);
        }

        // load the substitution map, if defined
        if (this.substitutionMap != null) {
            cmdLine.setSubstitutionMap(this.substitutionMap);
        }

        // load the watchdog timer, it can be set to infinite time
        ExecuteWatchdog watchdog = new ExecuteWatchdog(this.maxExecutiontime);
        ExtendedResultHandler resultHandler = new ExtendedResultHandler(watchdog);

        // inizialize outputstream processors.
        OutStreamProcessor outLinee = null;
        OutStreamProcessor errLinee = null;
        PumpStreamHandler streamHandler = null;
        if (outputFilter != null && outputFilter.size() > 0) {
            outLinee = new OutStreamProcessor(outputFilter);
        } else {
            outLinee = new OutStreamProcessor();
        }
        if (this.enableAllLinesOut) {
            outLinee.enableAllLines();
        }
        if (mergeOutStreams) {
            // Using Std out for the output/error stream
            streamHandler = new PumpStreamHandler(outLinee);
        } else {
            if (errorFilter != null && errorFilter.size() > 0) {
                errLinee = new OutStreamProcessor(errorFilter);
            } else {
                errLinee = new OutStreamProcessor();
            }
            if (enableAllLinesErr) {
                errLinee.enableAllLines();
            }
            // Using Std out for the output/error stream
            streamHandler = new PumpStreamHandler(outLinee, errLinee);
        }
        DefaultExecutor executor = new DefaultExecutor();
        // set the working directory...
        // if the working directory doesn't exists, it can crash the
        // executor.
        if (workingDirecory != null) {
            executor.setWorkingDirectory(workingDirecory);
        }

        // set the accepted exit values for the command line
        // default is '0'.
        if (okExitValues != null && okExitValues.length > 0) {
            executor.setExitValues(okExitValues);
        }

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

        try {
            executor.execute(cmdLine, resultHandler);
            resultHandler.waitFor();
            returnCode = resultHandler.getExitValue();
            exitMode = resultHandler.getExitMode();
            switch (exitMode) {
            case ERROR_IN_EXECUTION:
                this.message = resultHandler.getException().getMessage();
                break;
            default:
                break;
            }
        } catch (ExecuteException e) {
            exitMode = ExecuteResult.EXCEPTION;
            exception = e;
            this.message = e.getMessage();
        } catch (IOException e) {
            exitMode = ExecuteResult.EXCEPTION;
            exception = e;
            this.message = e.getMessage();
        } catch (InterruptedException e) {
            exitMode = ExecuteResult.EXCEPTION;
            exception = e;
            this.message = e.getMessage();
        }

        //

        if (outLinee != null) {
            outputLines = outLinee.getLines();
            allOutputLines = outLinee.getAllLines();
        }
        if (errLinee != null) {
            errorLines = errLinee.getLines();
            allErrorLines = errLinee.getAllLines();
        }

        this.closeStreams(outLinee, errLinee);
    } catch (Exception e) {
        throw new ExtendedExecuteException(e.getMessage(), Executor.INVALID_EXITVALUE, e);
    }

    return exitMode;
}

From source file:maker.task.compile.ReplTestPumpStreamHandler.java

/**
 * Stopping a pumper thread. The implementation actually waits
 * longer than specified in 'timeout' to detect if the timeout
 * was indeed exceeded. If the timeout was exceeded an IOException
 * is created to be thrown to the caller.
 *
 * @param thread  the thread to be stopped
 * @param timeout the time in ms to wait to join
 *///from  www .  j a  va2s  .  c om
protected void stopThread(final Thread thread, final long timeout) {

    if (thread != null) {
        try {
            if (timeout == 0) {
                thread.join();
            } else {
                final long timeToWait = timeout + STOP_TIMEOUT_ADDITION;
                final long startTime = System.currentTimeMillis();
                thread.join(timeToWait);
                if (!(System.currentTimeMillis() < startTime + timeToWait)) {
                    final String msg = "The stop timeout of " + timeout + " ms was exceeded";
                    caught = new ExecuteException(msg, Executor.INVALID_EXITVALUE);
                }
            }
        } catch (final InterruptedException e) {
            thread.interrupt();
        }
    }
}

From source file:com.github.zeroxff.executor.ExtendedExecutor.java

private void clearOut() {
    exitMode = ExecuteResult.OK_COMPLETED;
    returnCode = Executor.INVALID_EXITVALUE;
    outputLines = null;// w  w  w  . j  ava2 s . c o m
    errorLines = null;
    allOutputLines = null;
    allErrorLines = null;
    exception = null;
    message = null;
}

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 {/*from   w w w  .  jav  a 2 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.openflamingo.core.cmd.TailExecuteResultHandler.java

/**
 * Constructor.
 */
public TailExecuteResultHandler() {
    this.hasResult = false;
    this.exitValue = Executor.INVALID_EXITVALUE;
}