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

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

Introduction

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

Prototype

void setExitValues(final int[] values);

Source Link

Document

Define a list of exitValue of the process to be considered successful.

Usage

From source file:com.netflix.spinnaker.clouddriver.jobs.local.JobExecutorLocal.java

private Executor buildExecutor(ExecuteStreamHandler streamHandler) {
    Executor executor = new DefaultExecutor();
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(new ExecuteWatchdog(timeoutMinutes * 60 * 1000));
    // Setting this to null causes the executor to skip verifying exit codes; we'll handle checking the exit status
    // instead of having the executor throw an exception for non-zero exit codes.
    executor.setExitValues(null);

    return executor;
}

From source file:com.adaptris.hpcc.DfuPlusWrapper.java

protected void executeInternal(CommandLine cmdLine, OutputStream stdout)
        throws ProduceException, AbortJobException {
    int exit = -1;
    ExecuteWatchdog watchdog = new ExecuteWatchdog(EXEC_TIMEOUT_INTERVAL.toMilliseconds());
    try (OutputStream out = stdout) {
        Executor cmd = new DefaultExecutor();
        cmd.setWatchdog(watchdog);//from   w  ww  .  ja va2  s .co  m
        PumpStreamHandler pump = new ManagedPumpStreamHandler(out);
        cmd.setStreamHandler(pump);
        cmd.setExitValues(null);
        exit = cmd.execute(cmdLine);
    } catch (Exception e) {
        throw ExceptionHelper.wrapProduceException(e);
    }
    if (watchdog.killedProcess() || exit != 0) {
        throw new AbortJobException("Job killed due to timeout/ExitCode != 0");
    }
}

From source file:cc.arduino.contributions.packages.ContributionInstaller.java

private void executeScripts(File folder, Collection<File> postInstallScripts, boolean trusted, boolean trustAll)
        throws IOException {
    File script = postInstallScripts.iterator().next();

    if (!trusted && !trustAll) {
        System.err.println(/*from   w  w w .j  a va2s.co m*/
                I18n.format(tr("Warning: non trusted contribution, skipping script execution ({0})"), script));
        return;
    }

    if (trustAll) {
        System.err.println(I18n.format(tr("Warning: forced untrusted script execution ({0})"), script));
    }

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    Executor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(stdout, stderr));
    executor.setWorkingDirectory(folder);
    executor.setExitValues(null);
    int exitValue = executor.execute(new CommandLine(script));
    executor.setExitValues(new int[0]);

    System.out.write(stdout.toByteArray());
    System.err.write(stderr.toByteArray());

    if (executor.isFailure(exitValue)) {
        throw new IOException();
    }
}

From source file:kr.motd.maven.exec.ExecMojo.java

private void fillSuccessCodes(Executor exec) {
    if (successCodes != null && successCodes.length > 0) {
        exec.setExitValues(successCodes);
    }// ww w .  ja v a 2s .c  o  m
}

From source file:hr.fer.zemris.vhdllab.service.impl.GhdlSimulator.java

private List<String> executeProcess(CommandLine cl, java.io.File tempDirectory)
        throws ExecuteException, IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing process: " + cl.toString());
    }/*from ww  w .  j  a v a 2s  .c  o  m*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ExecuteStreamHandler handler = new PumpStreamHandler(bos);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(PROCESS_TIMEOUT);
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(tempDirectory);
    executor.setWatchdog(watchdog);
    executor.setStreamHandler(handler);
    /*
     * It seems that when ExecuteWatchdog terminates process by invoking
     * destroy method, process terminates with exit code 143. And since we
     * manually ask watchdog if he killed the process, exit code 143 is
     * marked as successful (just so our code can be executed).
     * 
     * Exit code 1 in case of compilation error(s).
     */
    executor.setExitValues(new int[] { 0, 1, 143 });
    try {
        executor.execute(cl);
    } catch (ExecuteException e) {
        LOG.warn("Process output dump:\n" + bos.toString());
        throw e;
    }

    if (watchdog.killedProcess()) {
        throw new SimulatorTimeoutException(PROCESS_TIMEOUT);
    }
    String output;
    try {
        output = bos.toString(IOUtil.DEFAULT_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw new UnhandledException(e);
    }
    if (StringUtils.isBlank(output)) {
        return Collections.emptyList();
    }
    return Arrays.asList(StringUtil.splitToNewLines(output));
}

From source file:org.apache.maven.plugin.cxx.utils.ExecutorService.java

public static int executeCommandLine(Executor exec, CommandLine commandLine, Properties enviro,
        OutputStream out, OutputStream err, InputStream in) throws ExecuteException, IOException {
    exec.setExitValues(null); // let us decide of exit value
    exec.setStreamHandler(new PumpStreamHandler(out, err, in));
    return exec.execute(commandLine, enviro);
}

From source file:org.stanwood.media.info.MediaFileInfoFetcher.java

private String getCommandOutput(boolean captureStdout, boolean captureStderr, boolean failOnExitCode,
        String command, Object... args) throws StanwoodException {
    CommandLine cmdLine = new CommandLine(command);
    for (Object arg : args) {
        if (arg instanceof File) {
            cmdLine.addArgument(((File) arg).getAbsolutePath(), false);
        } else if (arg instanceof String) {
            cmdLine.addArgument((String) arg, false);
        }/*from  w ww  .  ja v  a 2s.c  om*/
    }
    if (log.isDebugEnabled()) {
        log.debug("About to execute: " + cmdLine.toString()); //$NON-NLS-1$
    }
    Executor exec = new DefaultExecutor();
    exec.setExitValues(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, -1 });

    try {
        ByteArrayOutputStream capture = new ByteArrayOutputStream();
        OutputStream out;
        if (captureStdout) {
            out = capture;
        } else {
            out = new LoggerOutputStream(Level.INFO);
        }
        OutputStream err;
        ;
        if (captureStderr) {
            err = capture;
        } else {
            err = new LoggerOutputStream(Level.ERROR);
        }
        exec.setStreamHandler(new PumpStreamHandler(out, err));
        int exitCode = exec.execute(cmdLine);
        if (failOnExitCode && exitCode != 0) {
            log.error(capture.toString());
            throw new StanwoodException(MessageFormat
                    .format(Messages.getString("MediaFileInfoFetcher.NON_ZERO"), exitCode, cmdLine.toString())); //$NON-NLS-1$
        }
        return capture.toString();
    } catch (IOException e) {
        throw new StanwoodException(MessageFormat
                .format(Messages.getString("MediaFileInfoFetcher.UnableExecuteSysCmd"), cmdLine.toString()), e); //$NON-NLS-1$
    }
}