Search in sources :

Example 11 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project robovm by robovm.

the class Executor method exec.

public int exec() throws ExecuteException, IOException {
    CommandLine commandLine = generateCommandLine();
    logCommandLine(commandLine);
    try {
        return initExecutor(new DefaultExecutor()).execute(commandLine, generateEnv());
    } catch (ExecuteException e) {
        ExecuteException ex = new ExecuteException("Command '" + commandLine + "' failed ", e.getExitValue());
        ex.setStackTrace(e.getStackTrace());
        throw ex;
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 12 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project jstorm by alibaba.

the class JStormUtils method launchProcess.

/**
     * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler
     * If it is frontend, ByteArrayOutputStream.toString will return the calling result
     * <p/>
     * This function will ignore whether the command is successfully executed or not
     *
     * @param command       command to be executed
     * @param environment   env vars
     * @param workDir       working directory
     * @param resultHandler exec result handler
     * @return output stream
     * @throws IOException
     */
@Deprecated
public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler) throws IOException {
    String[] cmdlist = command.split(" ");
    CommandLine cmd = new CommandLine(cmdlist[0]);
    for (String cmdItem : cmdlist) {
        if (!StringUtils.isBlank(cmdItem)) {
            cmd.addArgument(cmdItem);
        }
    }
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);
    if (!StringUtils.isBlank(workDir)) {
        executor.setWorkingDirectory(new File(workDir));
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(out, out);
    executor.setStreamHandler(streamHandler);
    try {
        if (resultHandler == null) {
            executor.execute(cmd, environment);
        } else {
            executor.execute(cmd, environment, resultHandler);
        }
    } catch (ExecuteException ignored) {
    }
    return out;
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 13 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project cloudstack by apache.

the class HeatMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {
        CommandLine commandLine = new CommandLine("heat");
        if (dir != null && !dir.trim().isEmpty()) {
            commandLine.addArgument("dir");
            commandLine.addArgument(dir);
        }
        commandLine.addArgument("-gg");
        commandLine.addArgument("-cg");
        commandLine.addArgument(componentGroup);
        commandLine.addArgument("-ke");
        commandLine.addArgument("-sfrag");
        if (template == null || template.trim().isEmpty()) {
            commandLine.addArgument("-template");
            commandLine.addArgument("fragment");
        } else {
            commandLine.addArgument("-template");
            commandLine.addArgument(template);
        }
        if (outputFile != null) {
            commandLine.addArgument("-out");
            commandLine.addArgument(outputFile.getAbsolutePath());
        }
        if (directoryName != null) {
            commandLine.addArgument("-dr");
            commandLine.addArgument(directoryName);
        }
        if (vars != null) {
            commandLine.addArguments(vars, false);
        }
        DefaultExecutor executor = new DefaultExecutor();
        getLog().debug("working directory " + commandLine.toString());
        executor.setWorkingDirectory(getWorkingDirectory(workingDirectory));
        int exitValue = executor.execute(commandLine);
        if (exitValue != 0) {
            throw new MojoExecutionException("Problem executing heat, return code " + exitValue);
        }
    } catch (ExecuteException e) {
        throw new MojoExecutionException("Problem executing heat", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Problem executing heat", e);
    }
}
Also used : CommandLine(org.apache.commons.exec.CommandLine) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) IOException(java.io.IOException)

Example 14 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project stanbol by apache.

the class JarExecutor method start.

/**
     * Start the jar if not done yet, and setup runtime hook to stop it.
     */
public void start() throws Exception {
    final ExecuteResultHandler h = new ExecuteResultHandler() {

        @Override
        public void onProcessFailed(ExecuteException ex) {
            log.error("Process execution failed:" + ex, ex);
        }

        @Override
        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = System.getProperty("jar.executor.vm.options");
    final Executor e = new DefaultExecutor();
    if (this.workingDirectory != null) {
        e.setWorkingDirectory(this.workingDirectory);
    }
    final CommandLine cl = new CommandLine(javaExecutable);
    if (vmOptions != null && vmOptions.length() > 0) {
        // not the case for common usage patterns
        for (String option : StringUtils.split(vmOptions, " ")) {
            cl.addArgument(option);
        }
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    cl.addArgument("-p");
    cl.addArgument(String.valueOf(serverPort));
    log.info("Executing " + cl);
    e.setStreamHandler(new PumpStreamHandler());
    e.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    e.execute(cl, h);
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 15 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project sling by apache.

the class JarExecutor method start.

/** Start the jar if not done yet, and setup runtime hook
     *  to stop it.
     */
public void start() throws Exception {
    final ExecuteResultHandler h = new ExecuteResultHandler() {

        public void onProcessFailed(ExecuteException ex) {
            log.error("Process execution failed:" + ex, ex);
        }

        public void onProcessComplete(int result) {
            log.info("Process execution complete, exit code=" + result);
        }
    };
    final String vmOptions = config.getProperty(PROP_VM_OPTIONS);
    executor = new DefaultExecutor();
    final CommandLine cl = new CommandLine(jvmFullPath);
    if (vmOptions != null && vmOptions.length() > 0) {
        cl.addArguments(vmOptions);
    }
    cl.addArgument("-jar");
    cl.addArgument(jarToExecute.getAbsolutePath());
    // Additional options for the jar that's executed.
    // $JAREXEC_SERVER_PORT$ is replaced our serverPort value
    String jarOptions = config.getProperty(PROP_JAR_OPTIONS);
    if (jarOptions != null && jarOptions.length() > 0) {
        jarOptions = jarOptions.replaceAll("\\$JAREXEC_SERVER_PORT\\$", String.valueOf(serverPort));
        log.info("Executable jar options: {}", jarOptions);
        cl.addArguments(jarOptions);
    }
    final String workFolderOption = config.getProperty(PROP_WORK_FOLDER);
    if (workFolderOption != null && workFolderOption.length() > 0) {
        final File workFolder = new File(workFolderOption);
        if (!workFolder.isDirectory()) {
            throw new IOException("Work dir set by " + PROP_WORK_FOLDER + " option does not exist: " + workFolder.getAbsolutePath());
        }
        log.info("Setting working directory for executable jar: {}", workFolder.getAbsolutePath());
        executor.setWorkingDirectory(workFolder);
    }
    String tmStr = config.getProperty(PROP_EXIT_TIMEOUT_SECONDS);
    final int exitTimeoutSeconds = tmStr == null ? DEFAULT_EXIT_TIMEOUT : Integer.valueOf(tmStr);
    if ("true".equals(config.getProperty(PROP_SYNC_EXEC, ""))) {
        final long start = System.currentTimeMillis();
        log.info("Executing and waiting for result: " + cl);
        final int result = executor.execute(cl);
        final int expected = Integer.valueOf(config.getProperty(PROP_SYNC_EXEC_EXPECTED, "0"));
        log.info("Execution took " + (System.currentTimeMillis() - start) + " msec");
        if (result != expected) {
            throw new ExecutorException("Expected result code " + expected + ", got " + result);
        }
    } else {
        log.info("Executing asynchronously: " + cl);
        executor.setStreamHandler(new PumpStreamHandler());
        final ShutdownHookSingleProcessDestroyer pd = new ShutdownHookSingleProcessDestroyer("java -jar " + jarToExecute.getName(), exitTimeoutSeconds);
        final boolean waitOnShutdown = Boolean.valueOf(config.getProperty(PROP_WAIT_ONSHUTDOWN, "false"));
        log.info("Setting up ProcessDestroyer with waitOnShutdown=" + waitOnShutdown);
        pd.setWaitOnShutdown(waitOnShutdown);
        executor.setProcessDestroyer(pd);
        executor.execute(cl, h);
    }
}
Also used : ExecuteResultHandler(org.apache.commons.exec.ExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) File(java.io.File)

Aggregations

ExecuteException (org.apache.commons.exec.ExecuteException)16 CommandLine (org.apache.commons.exec.CommandLine)11 DefaultExecutor (org.apache.commons.exec.DefaultExecutor)11 IOException (java.io.IOException)10 PumpStreamHandler (org.apache.commons.exec.PumpStreamHandler)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)5 ExecuteResultHandler (org.apache.commons.exec.ExecuteResultHandler)3 ExecuteWatchdog (org.apache.commons.exec.ExecuteWatchdog)3 ExecuteStreamHandler (org.apache.commons.exec.ExecuteStreamHandler)2 Executor (org.apache.commons.exec.Executor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipFile (java.util.zip.ZipFile)1