Example usage for org.apache.commons.exec ExecuteException getMessage

List of usage examples for org.apache.commons.exec ExecuteException getMessage

Introduction

In this page you can find the example usage for org.apache.commons.exec ExecuteException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.mooregreatsoftware.gitprocess.lib.Pusher.java

private static ThePushResult doGitProgPush(GitLib gitLib, Branch localBranch, String remoteBranchName,
        boolean forcePush, String remoteName) {
    String cmd = String.format("git push --porcelain %s %s %s:%s", remoteName, forcePush ? "--force" : "",
            localBranch.shortName(), remoteBranchName);
    CommandLine commandLine = CommandLine.parse(cmd);
    DefaultExecutor executor = new DefaultExecutor();
    executor.setWorkingDirectory(gitLib.workingDirectory());
    final StringWriter stdOutWriter = new StringWriter();
    final StringWriter stdErrWriter = new StringWriter();
    executor.setStreamHandler(//  w w w  .  j a  v a 2s  .c om
            new PumpStreamHandler(new WriterOutputStream(stdOutWriter), new WriterOutputStream(stdErrWriter)));
    final int exitCode = Try.of(() -> {
        try {
            return executor.execute(commandLine);
        } catch (ExecuteException e) {
            return e.getExitValue();
        } catch (IOException e) {
            final String message = e.getMessage();
            if (message != null && message.contains("No such file or directory")) {
                return 1;
            }
            e.printStackTrace();
            return -1;
        }
    }).get();

    return new ProcPushResult(stdOutWriter, stdErrWriter, exitCode);
}

From source file:com.boundlessgeo.wps.grass.GrassProcesses.java

@DescribeProcess(title = "GRASS Version", description = "Retreive the version of GRASS used for computation")
@DescribeResult(description = "Version")
public static String version() {
    if (EXEC == null) {
        return "unavailable";
    }//  w w w.ja  v a2 s .co  m
    CommandLine cmd = new CommandLine(EXEC);
    cmd.addArgument("-v");

    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
    executor.setWatchdog(watchdog);
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        executor.setStreamHandler(new PumpStreamHandler(outputStream));

        LOGGER.info("exec: " + cmd.toString());
        int exitValue = executor.execute(cmd);
        return outputStream.toString();
    } catch (ExecuteException huh) {
        return "exit code: " + huh.getExitValue() + " (" + huh.getMessage() + ")";
    } catch (IOException e) {
        return "unavailable: " + e.getClass().getSimpleName() + ":" + e.getMessage();
    }
}

From source file:fr.gouv.culture.vitam.utils.Executor.java

/**
 * Execute an external command/*from   w w  w.  j  a  v  a2 s .c  o  m*/
 * @param cmd
 * @param tempDelay
 * @param correctValues
 * @param showOutput
 * @param realCommand
 * @return correctValues if ok, < 0 if an execution error occurs, or other error values
 */
public static int exec(List<String> cmd, long tempDelay, int[] correctValues, boolean showOutput,
        String realCommand) {
    // Create command with parameters
    CommandLine commandLine = new CommandLine(cmd.get(0));
    for (int i = 1; i < cmd.size(); i++) {
        commandLine.addArgument(cmd.get(i));
    }
    DefaultExecutor defaultExecutor = new DefaultExecutor();
    ByteArrayOutputStream outputStream;
    outputStream = new ByteArrayOutputStream();
    PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream);
    defaultExecutor.setStreamHandler(pumpStreamHandler);

    defaultExecutor.setExitValues(correctValues);
    AtomicBoolean isFinished = new AtomicBoolean(false);
    ExecuteWatchdog watchdog = null;
    Timer timer = null;
    if (tempDelay > 0) {
        // If delay (max time), then setup Watchdog
        timer = new Timer(true);
        watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
        defaultExecutor.setWatchdog(watchdog);
        CheckEndOfExecute endOfExecute = new CheckEndOfExecute(isFinished, watchdog, realCommand);
        timer.schedule(endOfExecute, tempDelay);
    }
    int status = -1;
    try {
        // Execute the command
        status = defaultExecutor.execute(commandLine);
    } catch (ExecuteException e) {
        if (e.getExitValue() == -559038737) {
            // Cannot run immediately so retry once
            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
            }
            try {
                status = defaultExecutor.execute(commandLine);
            } catch (ExecuteException e1) {
                pumpStreamHandler.stop();
                System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                        + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
                status = -2;
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                return status;
            } catch (IOException e1) {
                pumpStreamHandler.stop();
                System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                        + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
                status = -2;
                try {
                    outputStream.close();
                } catch (IOException e2) {
                }
                return status;
            }
        } else {
            pumpStreamHandler.stop();
            System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                    + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
            status = -2;
            try {
                outputStream.close();
            } catch (IOException e2) {
            }
            return status;
        }
    } catch (IOException e) {
        pumpStreamHandler.stop();
        System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage()
                + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString());
        status = -2;
        try {
            outputStream.close();
        } catch (IOException e2) {
        }
        return status;
    } finally {
        isFinished.set(true);
        if (timer != null) {
            timer.cancel();
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e1) {
        }
    }
    pumpStreamHandler.stop();
    if (defaultExecutor.isFailure(status) && watchdog != null) {
        if (watchdog.killedProcess()) {
            // kill by the watchdoc (time out)
            if (showOutput) {
                System.err.println(StaticValues.LBL.error_error.get() + "Exec is in Time Out");
            }
        }
        status = -3;
        try {
            outputStream.close();
        } catch (IOException e2) {
        }
    } else {
        if (showOutput) {
            System.out.println("Exec: " + outputStream.toString());
        }
        try {
            outputStream.close();
        } catch (IOException e2) {
        }
    }
    return status;
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qdel command.//from   ww  w.j a  v a  2 s .co m
 * <p>
 * Equivalent to qdel [param]
 *
 * @param jobId job id
 */
public static void qdel(String jobId) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QDEL);
    cmdLine.addArgument(jobId);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, null, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qdel command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qdel command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qdel command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qdel exit value: " + exitValue);

    if (exitValue != 0)
        throw new PBSException("Failed to delete job " + jobId + ". Error output: " + err.toString());
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command.//from   w  ww  . j ava2 s.c om
 * <p>
 * Equivalent to qsub [param]
 *
 * @param input job input file
 * @return job id
 */
public static String qsub(String input) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, null, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * <p>//w w  w  .j a va 2 s. c o m
 * PBS qsub command with arguments resource overrides
 * </p>
 *
 * <p>
 * Equivalent to qsub [param] -l [resource_name=value,resource_name=value]]
 * </p>
 *
 * @param input job input file
 * @param resourceOverrides variable number of resources to override
 * @return job id
 */
public static String qsub(String input, String... resourceOverrides) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_RESOURCE_OVERRIDE_STATUS);
    String resourceOverrideArgument = StringUtils.join(resourceOverrides, ",");
    cmdLine.addArgument(resourceOverrideArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>// ww  w. j  ava  2  s .co m
 * Equivalent to qsub -t 1,2,3 [param]
 *
 * @param input job input file
 * @param pbsArrayIDs list of specified PBS indices
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    cmdLine.addArgument(listArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit
 * <p>/*  w ww  . j  ava2s .  co m*/
 * Equivalent to qsub -t 5-20 [param]
 *
 * @param input job input file
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    cmdLine.addArgument(rangeArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command./*from   w  ww  . ja  v  a2  s . c o  m*/
 * <p>
 * Equivalent to qsub [param]
 *
 * @param inputs job input file
 * @param environment environment variables
 * @return job id
 */
public static String qsub(String[] inputs, Map<String, String> environment) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    for (int i = 0; i < inputs.length; ++i) {
        cmdLine.addArgument(inputs[i]);
    }

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, environment, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0) {
        throw new PBSException("Failed to submit job script with command line '" + cmdLine.toString()
                + "'. Error output: " + err.toString());
    }

    String jobId = out.toString();
    return jobId.trim();
}

From source file:com.tupilabs.pbs.PBS.java

/**
 * PBS qsub command for an Array Job with Specific PBS_ARRAY_IDs to submit AND a range to submit
 * <p>/*from   w w w .j  a v a  2  s.  c  o m*/
 * Equivalent to qsub -t 1,2,3,5-20 [param]
 *
 * @param input job input file
 * @param pbsArrayIDs list of specified indices
 * @param beginIndex beginning of index range
 * @param endIndex end of index range
 * @return job id of array job
 */
public static String qsubArrayJob(String input, List<Integer> pbsArrayIDs, int beginIndex, int endIndex) {
    final CommandLine cmdLine = new CommandLine(COMMAND_QSUB);
    cmdLine.addArgument(PARAMETER_ARRAY_JOB_STATUS);
    String rangeArgument = beginIndex + "-" + endIndex;
    String listArgument = StringUtils.join(pbsArrayIDs, ",");
    String combinedArgument = listArgument + "," + rangeArgument;
    cmdLine.addArgument(combinedArgument);
    cmdLine.addArgument(input);

    final OutputStream out = new ByteArrayOutputStream();
    final OutputStream err = new ByteArrayOutputStream();

    DefaultExecuteResultHandler resultHandler;
    try {
        resultHandler = execute(cmdLine, out, err);
        resultHandler.waitFor(DEFAULT_TIMEOUT);
    } catch (ExecuteException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    } catch (InterruptedException e) {
        throw new PBSException("Failed to execute qsub command: " + e.getMessage(), e);
    }

    final int exitValue = resultHandler.getExitValue();
    LOGGER.info("qsub exit value: " + exitValue);
    LOGGER.fine("qsub output: " + out.toString());

    if (exitValue != 0)
        throw new PBSException("Failed to submit job script " + input + ". Error output: " + err.toString());

    String jobId = out.toString();
    return jobId.trim();
}