List of usage examples for org.apache.commons.exec Executor setStreamHandler
void setStreamHandler(ExecuteStreamHandler streamHandler);
From source file:org.xdi.util.process.ProcessHelper.java
/** * //from w w w. j a v a2s. c om * @param printJobTimeout * the printJobTimeout (ms) before the watchdog terminates the * print process * @param printInBackground * printing done in the background or blocking * @param streamHandler * @return a print result handler (implementing a future) * @throws IOException * the test failed */ public static PrintResultHandler executeProgram(CommandLine commandLine, String workingDirectory, long printJobTimeout, boolean printInBackground, int successExitValue, ExecuteStreamHandler streamHandler) throws IOException { ExecuteWatchdog watchdog = null; PrintResultHandler resultHandler; // Create the executor and consider the successExitValue as success Executor executor = new DefaultExecutor(); executor.setExitValue(successExitValue); if (StringHelper.isNotEmpty(workingDirectory)) { executor.setWorkingDirectory(new File(workingDirectory)); } // Redirect streams if needed if (streamHandler != null) { executor.setStreamHandler(streamHandler); } // Create a watchdog if requested if (printJobTimeout > 0) { watchdog = new ExecuteWatchdog(printJobTimeout); executor.setWatchdog(watchdog); } // Pass a "ExecuteResultHandler" when doing background printing if (printInBackground) { log.debug(String.format("Executing non-blocking process %s", commandLine.toString())); resultHandler = new PrintResultHandler(watchdog); executor.execute(commandLine, resultHandler); } else { log.debug(String.format("Executing blocking process %s", commandLine.toString())); successExitValue = executor.execute(commandLine); resultHandler = new PrintResultHandler(successExitValue); } return resultHandler; }
From source file:org.zanata.sync.jobs.utils.ProcessUtils.java
public static List<String> runNativeCommand(Path workingDir, long timeoutInMilli, String... commands) { Preconditions.checkArgument(commands != null && commands.length > 0, "You must provide commands to run"); CommandLine commandLine = CommandLine.parse(commands[0]); ImmutableList<String> args = ImmutableList.copyOf(commands).subList(1, commands.length); for (String arg : args) { commandLine.addArgument(arg);//from w w w.ja va 2 s . c om } Executor executor = new DefaultExecutor(); ImmutableList.Builder<String> output = ImmutableList.builder(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int logLevel) { log.info(line); output.add(line); } })); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeoutInMilli); executor.setWatchdog(watchDog); executor.setWorkingDirectory(workingDir.toFile()); executor.setProcessDestroyer(PROCESS_DESTROYER); try { int exitCode = executor.execute(commandLine); if (Execute.isFailure(exitCode) && watchDog.killedProcess()) { // it was killed on purpose by the watchdog log.error("process {} taking too long to run and killed by watchdog", commandLine); } } catch (IOException e) { log.error("error running:{}", commandLine); throw Throwables.propagate(e); } return output.build(); }
From source file:processing.app.macosx.Platform.java
private void discoverRealOsArch() throws IOException { CommandLine uname = CommandLine.parse("uname -m"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(baos, null)); executor.execute(uname);// www .ja v a2 s. co m osArch = StringUtils.trim(new String(baos.toByteArray())); }