Search in sources :

Example 6 with ExecuteException

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

the class ToolchainUtil method findXcodePath.

public static String findXcodePath() throws IOException {
    try {
        String path = new Executor(Logger.NULL_LOGGER, "xcode-select").args("--print-path").execCapture();
        File f = new File(path);
        if (f.exists() && f.isDirectory()) {
            if (new File(f, "Platforms").exists() && new File(f, "Toolchains").exists()) {
                return path;
            }
        }
        throw new IllegalArgumentException(String.format("The path '%s' does not appear to be a valid Xcode path. Use " + "'sudo xcode-select -switch <path-to-xcode>' from a Terminal " + "to switch to the correct Xcode path.", path));
    } catch (ExecuteException e) {
        handleExecuteException(e);
        return null;
    }
}
Also used : ExecuteException(org.apache.commons.exec.ExecuteException) File(java.io.File)

Example 7 with ExecuteException

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

the class HcatDelegator method descExtendedTable.

/**
   * Return a json "show table extended like" with extra info from "desc exteded"
   * This will return table with exact name match.
   */
public Response descExtendedTable(String user, String db, String table) throws HcatException, NotAuthorizedException, BusyException, ExecuteException, IOException {
    String exec = String.format("use %s; show table extended like %s;", db, table);
    try {
        //get detailed "tableInfo" from query "desc extended tablename;"
        Response res0 = descTable(user, db, table, true);
        if (res0.getStatus() != HttpStatus.OK_200)
            return res0;
        Map m = (Map) res0.getEntity();
        Map tableInfo = (Map) m.get("tableInfo");
        String res = jsonRun(user, exec);
        JsonBuilder jb = JsonBuilder.create(singleTable(res, table)).remove("tableName").put("database", db).put("table", table).put("retention", tableInfo.get("retention")).put("sd", tableInfo.get("sd")).put("parameters", tableInfo.get("parameters")).put("parametersSize", tableInfo.get("parametersSize")).put("tableType", tableInfo.get("tableType"));
        // If we can get them from HDFS, add group and permission
        String loc = (String) jb.getMap().get("location");
        if (loc != null && loc.startsWith("hdfs://")) {
            try {
                FileSystem fs = FileSystem.get(appConf);
                FileStatus status = fs.getFileStatus(new Path(new URI(loc)));
                jb.put("group", status.getGroup());
                jb.put("permission", status.getPermission().toString());
            } catch (Exception e) {
                LOG.warn(e.getMessage() + " Couldn't get permissions for " + loc);
            }
        }
        return jb.build();
    } catch (HcatException e) {
        throw new HcatException("unable to show table: " + table, e.execBean, exec);
    }
}
Also used : Response(javax.ws.rs.core.Response) Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) FileSystem(org.apache.hadoop.fs.FileSystem) HashMap(java.util.HashMap) Map(java.util.Map) URI(java.net.URI) IOException(java.io.IOException) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 8 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project frontend-maven-plugin by eirslett.

the class ProcessExecutor method execute.

private int execute(final Logger logger, final OutputStream stdout, final OutputStream stderr) throws ProcessExecutionException {
    logger.debug("Executing command line {}", commandLine);
    try {
        ExecuteStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr);
        executor.setStreamHandler(streamHandler);
        int exitValue = executor.execute(commandLine, environment);
        logger.debug("Exit value {}", exitValue);
        return exitValue;
    } catch (ExecuteException e) {
        if (executor.getWatchdog() != null && executor.getWatchdog().killedProcess()) {
            throw new ProcessExecutionException("Process killed after timeout");
        }
        throw new ProcessExecutionException(e);
    } catch (IOException e) {
        throw new ProcessExecutionException(e);
    }
}
Also used : PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteStreamHandler(org.apache.commons.exec.ExecuteStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException) IOException(java.io.IOException)

Example 9 with ExecuteException

use of org.apache.commons.exec.ExecuteException in project openhab1-addons by openhab.

the class ExecBinding method executeCommandAndWaitResponse.

/**
     * <p>
     * Executes <code>commandLine</code>. Sometimes (especially observed on
     * MacOS) the commandLine isn't executed properly. In that cases another
     * exec-method is to be used. To accomplish this please use the special
     * delimiter '<code>@@</code>'. If <code>commandLine</code> contains this
     * delimiter it is split into a String[] array and the special exec-method
     * is used.
     * </p>
     * <p>
     * A possible {@link IOException} gets logged but no further processing is
     * done.
     * </p>
     *
     * @param commandLine the command line to execute
     * @return response data from executed command line
     */
private String executeCommandAndWaitResponse(String commandLine) {
    String retval = null;
    CommandLine cmdLine = null;
    if (commandLine.contains(CMD_LINE_DELIMITER)) {
        String[] cmdArray = commandLine.split(CMD_LINE_DELIMITER);
        cmdLine = new CommandLine(cmdArray[0]);
        for (int i = 1; i < cmdArray.length; i++) {
            cmdLine.addArgument(cmdArray[i], false);
        }
    } else {
        cmdLine = CommandLine.parse(commandLine);
    }
    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
    Executor executor = new DefaultExecutor();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout);
    executor.setExitValue(1);
    executor.setStreamHandler(streamHandler);
    executor.setWatchdog(watchdog);
    try {
        executor.execute(cmdLine, resultHandler);
        logger.debug("executed commandLine '{}'", commandLine);
    } catch (ExecuteException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    } catch (IOException e) {
        logger.error("couldn't execute commandLine '" + commandLine + "'", e);
    }
    // can safely request the exit code
    try {
        resultHandler.waitFor();
        int exitCode = resultHandler.getExitValue();
        retval = StringUtils.chomp(stdout.toString());
        logger.debug("exit code '{}', result '{}'", exitCode, retval);
    } catch (InterruptedException e) {
        logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e);
    }
    return retval;
}
Also used : DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CommandLine(org.apache.commons.exec.CommandLine) Executor(org.apache.commons.exec.Executor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ExecuteException(org.apache.commons.exec.ExecuteException)

Example 10 with ExecuteException

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

the class DefaultExecCommandExecutor method execute.

@Override
public ExecResult execute(ExecCommand command) {
    notNull(command, "command");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    DefaultExecutor executor = prepareDefaultExecutor(command);
    // handle error and output of the process and write them to the given
    // out stream
    PumpStreamHandler handler = new PumpStreamHandler(out, err, command.getInput());
    executor.setStreamHandler(handler);
    CommandLine cl = toCommandLine(command);
    try {
        int exitValue = executor.execute(cl);
        // if the size is zero, we have no output, so construct the result
        // with null (required by ExecResult)
        InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
        InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
        ExecResult result = new ExecResult(command, stdout, stderr, exitValue);
        return result;
    } catch (ExecuteException ee) {
        LOG.error("ExecException while executing command: " + command.toString() + " - " + ee.getMessage());
        InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
        InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
        throw new ExecException("Failed to execute command " + command, stdout, stderr, ee.getExitValue(), ee);
    } catch (IOException ioe) {
        InputStream stdout = out.size() == 0 ? null : new ByteArrayInputStream(out.toByteArray());
        InputStream stderr = err.size() == 0 ? null : new ByteArrayInputStream(err.toByteArray());
        // use 0 as exit value as the executor didn't return the value
        int exitValue = 0;
        if (executor instanceof ExecDefaultExecutor) {
            // get the exit value from the executor as it captures this to work around the common-exec bug
            exitValue = ((ExecDefaultExecutor) executor).getExitValue();
        }
        // workaround to ignore if the stream was already closes due some race condition in commons-exec
        String msg = ioe.getMessage();
        if (msg != null && "stream closed".equals(msg.toLowerCase(Locale.ENGLISH))) {
            LOG.debug("Ignoring Stream closed IOException", ioe);
            ExecResult result = new ExecResult(command, stdout, stderr, exitValue);
            return result;
        }
        // invalid working dir
        LOG.error("IOException while executing command: " + command.toString() + " - " + ioe.getMessage());
        throw new ExecException("Unable to execute command " + command, stdout, stderr, exitValue, ioe);
    } finally {
        // the inputStream must be closed after the execution
        IOUtils.closeQuietly(command.getInput());
    }
}
Also used : ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExecException(org.apache.camel.component.exec.ExecException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ExecEndpoint(org.apache.camel.component.exec.ExecEndpoint) ExecDefaultExecutor(org.apache.camel.component.exec.ExecDefaultExecutor) CommandLine(org.apache.commons.exec.CommandLine) PumpStreamHandler(org.apache.commons.exec.PumpStreamHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecuteException(org.apache.commons.exec.ExecuteException) ExecResult(org.apache.camel.component.exec.ExecResult)

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