Java Code Examples for org.apache.commons.exec.DefaultExecuteResultHandler#getExitValue()
The following examples show how to use
org.apache.commons.exec.DefaultExecuteResultHandler#getExitValue() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalLbAdapter.java From Baragon with Apache License 2.0 | 6 votes |
private int executeWithTimeout(CommandLine command, int timeout) throws LbAdapterExecuteException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(baos)); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); // Start async and do our own time limiting instead of using a watchdog to avoid https://issues.apache.org/jira/browse/EXEC-62 try { long start = System.currentTimeMillis(); executor.execute(command, resultHandler); while (System.currentTimeMillis() - start < timeout && !resultHandler.hasResult()) { Thread.sleep(50); } if (resultHandler.hasResult()) { if (resultHandler.getException() != null) { throw resultHandler.getException(); } return resultHandler.getExitValue(); } else { CompletableFuture.runAsync(() -> executor.getWatchdog().destroyProcess(), destroyProcessExecutor); throw new LbAdapterExecuteException(baos.toString(Charsets.UTF_8.name()), command.toString()); } } catch (ExecuteException|InterruptedException e) { throw new LbAdapterExecuteException(baos.toString(Charsets.UTF_8.name()), e, command.toString()); } }
Example 2
Source File: CommonsExecOsCommandOperations.java From spring-data-dev-tools with Apache License 2.0 | 5 votes |
private Future<CommandResult> executeCommand(String command, File executionDirectory, boolean silent) throws IOException { StringWriter writer = new StringWriter(); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); try (WriterOutputStream outputStream = new WriterOutputStream(writer)) { String outerCommand = "/bin/bash -lc"; CommandLine outer = CommandLine.parse(outerCommand); outer.addArgument(command, false); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(executionDirectory); executor.setStreamHandler(new PumpStreamHandler(silent ? outputStream : System.out, null)); executor.execute(outer, ENVIRONMENT, resultHandler); resultHandler.waitFor(); } catch (InterruptedException e) { throw new IllegalStateException(e); } return new AsyncResult<CommandResult>( new CommandResult(resultHandler.getExitValue(), writer.toString(), resultHandler.getException())); }
Example 3
Source File: SolrCLI.java From lucene-solr with Apache License 2.0 | 4 votes |
protected Map<String,Object> startSolr(File solrHomeDir, boolean cloudMode, CommandLine cli, int port, String zkHost, int maxWaitSecs) throws Exception { String extraArgs = readExtraArgs(cli.getArgs()); String host = cli.getOptionValue('h'); String memory = cli.getOptionValue('m'); String hostArg = (host != null && !"localhost".equals(host)) ? " -h "+host : ""; String zkHostArg = (zkHost != null) ? " -z "+zkHost : ""; String memArg = (memory != null) ? " -m "+memory : ""; String cloudModeArg = cloudMode ? "-cloud " : ""; String forceArg = cli.hasOption("force") ? " -force" : ""; String addlOpts = cli.getOptionValue('a'); String addlOptsArg = (addlOpts != null) ? " -a \""+addlOpts+"\"" : ""; File cwd = new File(System.getProperty("user.dir")); File binDir = (new File(script)).getParentFile(); boolean isWindows = (OS.isFamilyDOS() || OS.isFamilyWin9x() || OS.isFamilyWindows()); String callScript = (!isWindows && cwd.equals(binDir.getParentFile())) ? "bin/solr" : script; String cwdPath = cwd.getAbsolutePath(); String solrHome = solrHomeDir.getAbsolutePath(); // don't display a huge path for solr home if it is relative to the cwd if (!isWindows && cwdPath.length() > 1 && solrHome.startsWith(cwdPath)) solrHome = solrHome.substring(cwdPath.length()+1); String startCmd = String.format(Locale.ROOT, "\"%s\" start %s -p %d -s \"%s\" %s %s %s %s %s %s", callScript, cloudModeArg, port, solrHome, hostArg, zkHostArg, memArg, forceArg, extraArgs, addlOptsArg); startCmd = startCmd.replaceAll("\\s+", " ").trim(); // for pretty printing echo("\nStarting up Solr on port " + port + " using command:"); echo(startCmd + "\n"); String solrUrl = String.format(Locale.ROOT, "%s://%s:%d/solr", urlScheme, (host != null ? host : "localhost"), port); Map<String,Object> nodeStatus = checkPortConflict(solrUrl, solrHomeDir, port, cli); if (nodeStatus != null) return nodeStatus; // the server they are trying to start is already running int code = 0; if (isWindows) { // On Windows, the execution doesn't return, so we have to execute async // and when calling the script, it seems to be inheriting the environment that launched this app // so we have to prune out env vars that may cause issues Map<String,String> startEnv = new HashMap<>(); Map<String,String> procEnv = EnvironmentUtils.getProcEnvironment(); if (procEnv != null) { for (Map.Entry<String, String> entry : procEnv.entrySet()) { String envVar = entry.getKey(); String envVarVal = entry.getValue(); if (envVarVal != null && !"EXAMPLE".equals(envVar) && !envVar.startsWith("SOLR_")) { startEnv.put(envVar, envVarVal); } } } DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler(); executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd), startEnv, handler); // wait for execution. try { handler.waitFor(3000); } catch (InterruptedException ie) { // safe to ignore ... Thread.interrupted(); } if (handler.hasResult() && handler.getExitValue() != 0) { throw new Exception("Failed to start Solr using command: "+startCmd+" Exception : "+handler.getException()); } } else { try { code = executor.execute(org.apache.commons.exec.CommandLine.parse(startCmd)); } catch(ExecuteException e){ throw new Exception("Failed to start Solr using command: "+startCmd+" Exception : "+ e); } } if (code != 0) throw new Exception("Failed to start Solr using command: "+startCmd); return getNodeStatus(solrUrl, maxWaitSecs); }