List of usage examples for org.apache.commons.exec Executor setStreamHandler
void setStreamHandler(ExecuteStreamHandler streamHandler);
From source file:org.openhab.binding.exec.internal.ExecBinding.java
/** * <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>/*from w w w . j a v a 2 s . c om*/ * <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); } // some time later the result handler callback was invoked so we // 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; }
From source file:org.openhab.io.net.exec.ExecUtil.java
/** * <p>//from w w w .ja v a2 s. c o m * 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 * @param timeout * timeout for execution in milliseconds * @return response data from executed command line */ public static String executeCommandLineAndWaitResponse(String commandLine, int timeout) { 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); } // some time later the result handler callback was invoked so we // can safely request the exit code try { resultHandler.waitFor(); int exitCode = resultHandler.getExitValue(); retval = StringUtils.chomp(stdout.toString()); if (resultHandler.getException() != null) { logger.warn(resultHandler.getException().getMessage()); } else { logger.debug("exit code '{}', result '{}'", exitCode, retval); } } catch (InterruptedException e) { logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e); } return retval; }
From source file:org.openqa.selenium.iphone.IPhoneSimulatorBinary.java
public void launch() { Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(createOutputStream())); try {//from w w w .j a va 2s . c o m exitCode = executor.execute(commandLine); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.openqa.selenium.iphone.IPhoneSimulatorBinary.java
public void shutdown() { try {//from w w w. j av a 2 s .c o m File scriptFile = File.createTempFile("iWebDriver.kill.", ".script"); FileWriter writer = new FileWriter(scriptFile); writer.write("ps ax | grep 'iPhone Simulator' | grep -v grep | awk '{print $1}' | xargs kill"); writer.flush(); writer.close(); FileHandler.makeExecutable(scriptFile); CommandLine killCommandLine = CommandLine.parse(scriptFile.getAbsolutePath()); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(null, null)); getOutputIgnoringExecutor().execute(killCommandLine); } catch (Exception ignored) { } // Wait until the process really quits (nothing is bound to port 3001) // TODO something other than Thread.sleep // client = new HttpClientFactory().getHttpClient(); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } exitCode = null; }
From source file:org.openqa.selenium.iphone.IPhoneSimulatorBinary.java
private static Executor getOutputIgnoringExecutor() { Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(null, null)); return executor; }
From source file:org.openqa.selenium.iphone.IPhoneSimulatorCommandExecutorTest.java
private void killIphoneSimulatorProcesses() { try {//from www. j a v a 2s . c om File scriptFile = File.createTempFile("iWebDriver.kill.", ".script"); FileWriter writer = new FileWriter(scriptFile); writer.write("ps ax | grep 'iPhone Simulator' | grep -v grep | awk '{print $1}' | xargs kill"); writer.flush(); writer.close(); FileHandler.makeExecutable(scriptFile); CommandLine killCommandLine = CommandLine.parse(scriptFile.getAbsolutePath()); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(null, null)); executor.execute(killCommandLine); // need to wait for the port to free up // TODO same as needs to be done in IPhoneSimulatorBinary Thread.sleep(5000); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.rhq.server.control.ControlCommand.java
protected void killPid(String pid) throws IOException { Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(getBinDir()); PumpStreamHandler streamHandler = new PumpStreamHandler(createNullOutputStream(), createNullOutputStream()); executor.setStreamHandler(streamHandler); org.apache.commons.exec.CommandLine commandLine; commandLine = new org.apache.commons.exec.CommandLine("kill").addArgument(pid); executor.execute(commandLine);//from ww w . j a v a 2 s . c om }
From source file:org.rhq.server.control.ControlCommand.java
protected boolean isUnixPidRunning(String pid) { Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(getBinDir()); PumpStreamHandler streamHandler = new PumpStreamHandler(createNullOutputStream(), createNullOutputStream()); executor.setStreamHandler(streamHandler); org.apache.commons.exec.CommandLine commandLine; commandLine = new org.apache.commons.exec.CommandLine("kill").addArgument("-0").addArgument(pid); boolean isRunning = true; // assume it is running try {/*from ww w .ja v a 2s .co m*/ int code = executor.execute(commandLine); if (code != 0) { isRunning = false; } } catch (ExecuteException ee) { log.debug("kill -0 for pid [" + pid + "] threw exception with exit value [" + ee.getExitValue() + "]"); if (ee.getExitValue() == 1) { // return code 1 means process does not exist isRunning = false; } } catch (IOException e) { log.error("Checking for running process failed. Will assume it is running. Error: " + e.getMessage()); } log.debug("unix pid [" + pid + "] " + ((isRunning) ? "is" : "is NOT") + " running"); return isRunning; }
From source file:org.rhq.storage.installer.StorageInstaller.java
private String exec(File workingDir, org.apache.commons.exec.CommandLine cmdLine) throws Exception { Executor executor = new DefaultExecutor(); org.apache.commons.io.output.ByteArrayOutputStream buffer = new org.apache.commons.io.output.ByteArrayOutputStream(); NullOutputStream nullOs = new NullOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(nullOs, buffer); executor.setWorkingDirectory(workingDir); executor.setStreamHandler(streamHandler); String result = ""; try {/*from w w w . ja va 2 s . c o m*/ exec(executor, cmdLine); result = buffer.toString(); } finally { try { buffer.close(); nullOs.close(); } catch (Exception e) { // best effort } } return result; }
From source file:org.stanwood.media.info.MediaFileInfoFetcher.java
private String getCommandOutput(boolean captureStdout, boolean captureStderr, boolean failOnExitCode, String command, Object... args) throws StanwoodException { CommandLine cmdLine = new CommandLine(command); for (Object arg : args) { if (arg instanceof File) { cmdLine.addArgument(((File) arg).getAbsolutePath(), false); } else if (arg instanceof String) { cmdLine.addArgument((String) arg, false); }/*from w w w.j av a 2 s .c om*/ } if (log.isDebugEnabled()) { log.debug("About to execute: " + cmdLine.toString()); //$NON-NLS-1$ } Executor exec = new DefaultExecutor(); exec.setExitValues(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, -1 }); try { ByteArrayOutputStream capture = new ByteArrayOutputStream(); OutputStream out; if (captureStdout) { out = capture; } else { out = new LoggerOutputStream(Level.INFO); } OutputStream err; ; if (captureStderr) { err = capture; } else { err = new LoggerOutputStream(Level.ERROR); } exec.setStreamHandler(new PumpStreamHandler(out, err)); int exitCode = exec.execute(cmdLine); if (failOnExitCode && exitCode != 0) { log.error(capture.toString()); throw new StanwoodException(MessageFormat .format(Messages.getString("MediaFileInfoFetcher.NON_ZERO"), exitCode, cmdLine.toString())); //$NON-NLS-1$ } return capture.toString(); } catch (IOException e) { throw new StanwoodException(MessageFormat .format(Messages.getString("MediaFileInfoFetcher.UnableExecuteSysCmd"), cmdLine.toString()), e); //$NON-NLS-1$ } }