List of usage examples for org.apache.commons.exec Executor setStreamHandler
void setStreamHandler(ExecuteStreamHandler streamHandler);
From source file:com.impetus.ankush.common.utils.CommandExecutor.java
/** * Exec.//from ww w . j a va 2s . c o m * * @param command the command * @param out the out * @param err the err * @return the int * @throws IOException Signals that an I/O exception has occurred. * @throws InterruptedException the interrupted exception */ public static int exec(String command, OutputStream out, OutputStream err) throws IOException, InterruptedException { CommandLine cmdLine = CommandLine.parse(command); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(out, err, null)); return executor.execute(cmdLine); }
From source file:de.pawlidi.openaletheia.utils.exec.ProcessExecutor.java
/** * Creates executor with system watchdog for given output stream. * /*ww w .ja v a2 s. c om*/ * @param outputStream * @return */ private static Executor createExecutor(OutputStream outputStream) { // create process watchdog with timeout 60000 milliseconds ExecuteWatchdog watchdog = new ExecuteWatchdog(WATCHDOG_TIMEOUT); // set watchdog and stream handler Executor executor = new DefaultExecutor(); executor.setWatchdog(watchdog); executor.setStreamHandler(new PumpStreamHandler(outputStream, outputStream)); return executor; }
From source file:com.bptselenium.jenkins.BPTSeleniumJenkins.RunCommand.java
public static void runCommand(String command, TaskListener listener, Run<?, ?> build) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CommandLine cmdLine = CommandLine.parse(command); ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); Executor executor = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); executor.setExitValue(10);/*from w w w .j a v a 2s. com*/ executor.setWatchdog(watchdog); try { executor.execute(cmdLine); } catch (ExecuteException ee) { //getting a non-standard execution value, set build result to unstable Result result = Result.UNSTABLE; if (build.getResult() == null) { build.setResult(result); } else if (build.getResult().isBetterThan(result)) { build.setResult(result.combine(build.getResult())); } } catch (IOException e) { e.printStackTrace(); } finally { listener.getLogger().println(outputStream.toString()); } }
From source file:com.github.genium_framework.appium.support.command.CommandManager.java
/** * Execute a command on the operating system using Apache Commons Exec. This * function runs asynchronously and dumps both stderr and stdout streams to * a temp file.//from w w w .j a v a 2 s . c o m * * @param commandLine The command to be executed. * @param outputStreamHandler An output stream to dump the process stderr * and stdout to it. */ public static void executeCommandUsingApacheExec(CommandLine commandLine, OutputStream outputStreamHandler) { try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStreamHandler); Executor process = new DefaultExecutor(); process.setExitValue(0); process.setStreamHandler(streamHandler); process.execute(commandLine, resultHandler); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "An exception was thrown.", ex); } }
From source file:com.zxy.commons.exec.CmdExecutor.java
/** * //from w w w.j a va 2 s. co m * * @param workHome workHome * @param command command * @throws InterruptedException InterruptedException * @throws IOException IOException */ public static void execAsyc(String workHome, String command) throws InterruptedException, IOException { CommandLine cmdLine = CommandLine.parse(PRE_CMD + command); Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File(workHome)); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int level) { LOGGER.debug(line); } }, new LogOutputStream() { @Override protected void processLine(String line, int level) { LOGGER.debug(line); } })); ExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.execute(cmdLine, EnvironmentUtils.getProcEnvironment(), resultHandler); // resultHandler.waitFor(); }
From source file:net.ishchenko.idea.nginx.platform.NginxCompileParametersExtractor.java
/** * Runs file with -V argument and matches the output against OUTPUT_PATTERN. * @param from executable to be run with -V command line argument * @return Parameters parsed out from process output * @throws PlatformDependentTools.ThisIsNotNginxExecutableException if file could not be run, or * output would not match against expected pattern *//*from w w w .j av a 2s.co m*/ public static NginxCompileParameters extract(VirtualFile from) throws PlatformDependentTools.ThisIsNotNginxExecutableException { NginxCompileParameters result = new NginxCompileParameters(); Executor executor = new DefaultExecutor(); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { executor.setStreamHandler(new PumpStreamHandler(os, os)); executor.execute(CommandLine.parse(from.getPath() + " -V")); } catch (IOException e) { throw new PlatformDependentTools.ThisIsNotNginxExecutableException(e); } String output = os.toString(); Matcher versionMatcher = Pattern.compile("nginx version: nginx/([\\d\\.]+)").matcher(output); Matcher configureArgumentsMatcher = Pattern.compile("configure arguments: (.*)").matcher(output); if (versionMatcher.find() && configureArgumentsMatcher.find()) { String version = versionMatcher.group(1); String params = configureArgumentsMatcher.group(1); result.setVersion(version); Iterable<String> namevalues = StringUtil.split(params, " "); for (String namevalue : namevalues) { int eqPosition = namevalue.indexOf('='); if (eqPosition == -1) { handleFlag(result, namevalue); } else { handleNameValue(result, namevalue.substring(0, eqPosition), namevalue.substring(eqPosition + 1)); } } } else { throw new PlatformDependentTools.ThisIsNotNginxExecutableException( NginxBundle.message("run.configuration.outputwontmatch")); } return result; }
From source file:eu.forgestore.ws.util.Utils.java
public static int executeSystemCommand(String cmdStr) { CommandLine cmdLine = CommandLine.parse(cmdStr); final Executor executor = new DefaultExecutor(); // create the executor and consider the exitValue '0' as success executor.setExitValue(0);//from w ww.j av a 2s .co m ByteArrayOutputStream out = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(out); executor.setStreamHandler(streamHandler); int exitValue = -1; try { exitValue = executor.execute(cmdLine); } catch (ExecuteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return exitValue; }
From source file:com.tascape.qa.th.android.comm.Adb.java
private static void loadAllSerials() { SERIALS.clear();/*from w ww . jav a 2s. com*/ String serials = SystemConfiguration.getInstance().getProperty(SYSPROP_SERIALS); if (null != serials) { LOG.info("Use specified devices from system property {}={}", SYSPROP_SERIALS, serials); SERIALS.addAll(Lists.newArrayList(serials.split(","))); } else { CommandLine cmdLine = new CommandLine(ADB); cmdLine.addArgument("devices"); LOG.debug("{}", cmdLine.toString()); List<String> output = new ArrayList<>(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new ESH(output)); try { if (executor.execute(cmdLine) != 0) { throw new RuntimeException(cmdLine + " failed"); } } catch (IOException ex) { throw new RuntimeException(cmdLine + " failed", ex); } output.stream().filter((line) -> (line.endsWith("device"))).forEach((line) -> { String s = line.split("\\t")[0]; LOG.info("serial {}", s); SERIALS.add(s); }); } if (SERIALS.isEmpty()) { throw new RuntimeException("No device detected."); } }
From source file:com.tascape.qa.th.android.comm.Adb.java
private static void loadSerialProductMap() { SERIAL_PRODUCT.clear();//from w ww. j a va 2 s . c o m String serials = SystemConfiguration.getInstance().getProperty(SYSPROP_SERIALS); if (null != serials) { LOG.info("Use specified devices from system property {}={}", SYSPROP_SERIALS, serials); Lists.newArrayList(serials.split(",")).forEach(s -> SERIAL_PRODUCT.put(s, "na")); } else { CommandLine cmdLine = new CommandLine(ADB); cmdLine.addArgument("devices"); cmdLine.addArgument("-l"); LOG.debug("{}", cmdLine.toString()); List<String> output = new ArrayList<>(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new ESH(output)); try { if (executor.execute(cmdLine) != 0) { throw new RuntimeException(cmdLine + " failed"); } } catch (IOException ex) { throw new RuntimeException(cmdLine + " failed", ex); } output.stream().map(line -> StringUtils.split(line, " ", 3)) .filter(ss -> ss.length == 3 && ss[1].equals("device")).forEach(ss -> { LOG.info("device {} -> {}", ss[0], ss[2]); SERIAL_PRODUCT.put(ss[0], ss[2]); }); } if (SERIAL_PRODUCT.isEmpty()) { throw new RuntimeException("No device detected."); } SERIALS.addAll(SERIAL_PRODUCT.keySet()); }
From source file:com.zxy.commons.exec.CmdExecutor.java
/** * //from w ww . jav a 2s . c o m * * @param workHome workHome * @param command command * @return ??? * @throws InterruptedException InterruptedException * @throws IOException IOException */ public static ExecutorResult exec(String workHome, String command) throws InterruptedException, IOException { CommandLine cmdLine = CommandLine.parse(PRE_CMD + command); Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File(workHome)); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream); executor.setStreamHandler(streamHandler); int code = executor.execute(cmdLine, EnvironmentUtils.getProcEnvironment()); String successMsg = outputStream.toString(ENCODING); String errorMsg = errorStream.toString(ENCODING); return new ExecutorResult(code, successMsg, errorMsg); }