List of usage examples for org.apache.commons.exec PumpStreamHandler PumpStreamHandler
public PumpStreamHandler(final OutputStream outAndErr)
PumpStreamHandler
. From source file:org.apache.zeppelin.util.ProcessLauncher.java
public void launch() { DefaultExecutor executor = new DefaultExecutor(); this.processOutput = new ProcessLogOutputStream(); executor.setStreamHandler(new PumpStreamHandler(processOutput)); this.watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(watchdog);//from w ww .j av a 2 s .c o m try { executor.execute(commandLine, envs, this); transition(State.LAUNCHED); LOGGER.info("Process is launched: {}", commandLine); } catch (IOException e) { this.processOutput.stopCatchLaunchOutput(); LOGGER.error("Fail to launch process: " + commandLine, e); transition(State.TERMINATED); errorMessage = e.getMessage(); } }
From source file:org.cloudifysource.dsl.context.utils.VolumeUtils.java
private static void executeCommandLine(final String commandLine, final long timeout) throws LocalStorageOperationException, TimeoutException { Executor executor = new DefaultExecutor(); executor.setExitValue(0);/*from ww w .java 2 s . c om*/ ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); ProcessOutputStream outAndErr = new ProcessOutputStream(); try { PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr); executor.setStreamHandler(streamHandler); logger.info("Executing commandLine : '" + commandLine + "'"); executor.execute(CommandLine.parse(commandLine)); logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput()); } catch (final Exception e) { if (watchdog.killedProcess()) { throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'"); } throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine + ". Process output was : " + outAndErr.getOutput(), e); } }
From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java
private static void executeCommandLine(final String commandLine, final long timeout) throws LocalStorageOperationException, TimeoutException { Executor executor = new DefaultExecutor(); executor.setExitValue(0);//from w ww .j a v a2 s .c o m ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); ProcessOutputStream outAndErr = new ProcessOutputStream(); try { PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr); executor.setStreamHandler(streamHandler); logger.info("Executing commandLine : '" + commandLine + "'"); executor.execute(CommandLine.parse(commandLine)); logger.info("Execution completed successfully. Process output was : " + outAndErr.getOutput()); } catch (final Exception e) { if (watchdog.killedProcess()) { throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'"); } throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine + ". Process output was : " + outAndErr.getOutput(), e); } }
From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java
private static String executeSilentCommandLineReturnOutput(final String commandLine, final long timeout) throws LocalStorageOperationException, TimeoutException { Executor executor = new DefaultExecutor(); executor.setExitValue(0);// w w w .ja v a 2 s . c om ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); ProcessOutputStream outAndErr = new ProcessOutputStream(); try { PumpStreamHandler streamHandler = new PumpStreamHandler(outAndErr); executor.setStreamHandler(streamHandler); executor.execute(CommandLine.parse(commandLine)); } catch (final Exception e) { if (watchdog.killedProcess()) { throw new TimeoutException("Timed out while executing commandLine : '" + commandLine + "'"); } throw new LocalStorageOperationException("Failed executing commandLine : '" + commandLine + ". Process output was : " + outAndErr.getOutput(), e); } return outAndErr.getOutput(); }
From source file:org.codehaus.mojo.exec.ExecMojo.java
protected int executeCommandLine(Executor exec, CommandLine commandLine, Map<String, String> enviro, FileOutputStream outputFile) throws ExecuteException, IOException { BufferedOutputStream bos = new BufferedOutputStream(outputFile); PumpStreamHandler psh = new PumpStreamHandler(bos); return executeCommandLine(exec, commandLine, enviro, psh); }
From source file:org.codice.alliance.distribution.sdk.video.stream.mpegts.MpegTsUdpClient.java
private static Duration getVideoDuration(final String videoFilePath) { String utfOutputStream;// w ww . ja v a 2 s .co m try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); final CommandLine command = getFFmpegInfoCommand(videoFilePath); final DefaultExecuteResultHandler resultHandler = executeFFmpeg(command, 3, streamHandler); resultHandler.waitFor(); utfOutputStream = outputStream.toString(StandardCharsets.UTF_8.name()); return parseVideoDuration(utfOutputStream); } catch (InterruptedException e) { logErrorMessage(String.format("Thread interrupted when executing ffmpeg command. %s", e), false); return null; } catch (UnsupportedEncodingException e) { logErrorMessage(String.format("Unsupported encoding in ffmpeg output. %s", e), false); return null; } catch (IllegalArgumentException e) { logErrorMessage(String.format("Unable to parse video duration. %s", e), false); return null; } catch (IOException | IllegalStateException e) { logErrorMessage(String.format("Unable to execute ffmpeg command. %s", e), false); return null; } }
From source file:org.dataconservancy.dcs.access.server.util.VivoUtil.java
public static String cmdExec(String query, String sparqlEndpoint) { String cmd = "curl -s -S -X POST --data-binary \"" + query + "\" " + sparqlEndpoint; CommandLine cmdLine = CommandLine.parse(cmd); DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); executor.setStreamHandler(psh);//from w w w . j a v a 2 s .com int exitValue; try { exitValue = executor.execute(cmdLine); } catch (ExecuteException e) { //logger.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } catch (IOException e) { //.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } //String str = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(stdout.toByteArray()))); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String output_line = null; String xml = ""; try { int flag = 0; while ((output_line = br.readLine()) != null) { if (output_line.contains("--:--")) { if (output_line.contains("<?xml version")) output_line = output_line.substring(output_line.indexOf("<?xml version")); else continue; } xml += output_line; } } catch (IOException e) { //logger.log(Level.SEVERE, e.getMessage()); e.printStackTrace(); } return xml; }
From source file:org.dataconservancy.dcs.id.impl.DataciteIdService.java
ByteArrayOutputStream executeCommand(String command) throws IOException { CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); executor.setStreamHandler(psh);//from w w w .j a v a 2s . c o m int exitValue = executor.execute(cmdLine); return stdout; }
From source file:org.eclipse.ecf.python.AbstractPythonLauncher.java
@Override public void launch(String[] args, OutputStream output) throws Exception { synchronized (this.launchLock) { if (isLaunched()) throw new IllegalStateException("Already started"); this.shuttingDown = false; if (enabled) { String pythonLaunchCommand = createPythonLaunchCommand(); if (pythonLaunchCommand == null) throw new NullPointerException("pythonLaunchCommand must not be null"); logger.debug("pythonLaunchCommand=" + pythonLaunchCommand); this.executor = createExecutor(); if (this.pythonWorkingDirectory != null) this.executor.setWorkingDirectory(pythonWorkingDirectory); if (output == null) { output = new LogOutputStream() { @Override/*w w w. java 2s. c o m*/ protected void processLine(String line, int level) { logger.debug("PYTHON: " + line); } }; } executor.setStreamHandler(new PumpStreamHandler(output)); this.executor.setProcessDestroyer(new PythonProcessDestroyer()); ExecuteResultHandler executeHandler = new DefaultExecuteResultHandler() { @Override public void onProcessComplete(int exitValue) { logger.debug("PYTHON EXIT=" + exitValue); } @Override public void onProcessFailed(ExecuteException e) { if (!shuttingDown) logger.debug("PYTHON EXCEPTION", e); } }; CommandLine commandLine = new CommandLine(pythonExec).addArgument(PYTHON_LAUNCH_COMMAND_OPTION); commandLine.addArgument(pythonLaunchCommand, true); List<String> argsList = (args == null) ? Collections.emptyList() : Arrays.asList(args); if (this.javaPort != null && !argsList.contains(JAVA_PORT_OPTION)) { commandLine.addArgument(JAVA_PORT_OPTION); commandLine.addArgument(String.valueOf(this.javaPort)); } if (this.pythonPort != null && !argsList.contains(PYTHON_PORT_OPTION)) { commandLine.addArgument(PYTHON_PORT_OPTION); commandLine.addArgument(String.valueOf(this.pythonPort)); } if (args != null) commandLine.addArguments(args); logger.debug("PythonLauncher.launch: " + commandLine); try { executor.execute(commandLine, executeHandler); } catch (Exception e) { this.executor = null; throw e; } } else logger.debug("PythonLauncher DISABLED. Python process must be started manually"); } }
From source file:org.eclipse.smarthome.io.net.exec.ExecUtil.java
/** * <p>/* ww w. j ava2 s .co 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()); logger.debug("exit code '{}', result '{}'", exitCode, retval); } catch (InterruptedException e) { logger.error("Timeout occured when executing commandLine '" + commandLine + "'", e); } return retval; }