List of usage examples for org.apache.commons.exec PumpStreamHandler PumpStreamHandler
public PumpStreamHandler(final OutputStream out, final OutputStream err)
PumpStreamHandler
. From source file:au.com.jwatmuff.genericp2p.windows.ExecUtil.java
public static String getStdoutForCommand(String cmd) { CommandLine cmdLine = CommandLine.parse(cmd); DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(60000)); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(stdout, null)); try {/*from www . ja v a2 s . c o m*/ executor.execute(cmdLine); } catch (ExecuteException e) { log.error("Exception executing '" + cmd + "'", e); } catch (IOException e) { log.error("IOException executing '" + cmd + "'", e); } return stdout.toString(); }
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 www. j a va2 s. c o 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:adb4j.executor.CmdResultHandler.java
public PumpStreamHandler getStreamHandler(CmdExecutor executor) { this.executor = executor; outputStream = new CmdOutputStream(this); errorStream = new CmdOutputStream(this); return new PumpStreamHandler(getOutputStream(), getErrorStream()); }
From source file:de.torstenwalter.maven.plugins.ExpdpMojo.java
public void execute() throws MojoExecutionException, MojoFailureException { CommandLine commandLine = new CommandLine(expdp); addCommonArguments(commandLine);/*from w w w . j a va 2 s. co m*/ getLog().debug("Executing command line: " + obfuscateCredentials(commandLine.toString(), getCredentials())); Executor exec = new DefaultExecutor(); exec.setStreamHandler(new PumpStreamHandler(System.out, System.err)); try { exec.execute(commandLine); } catch (ExecuteException e) { throw new MojoExecutionException("Command execution failed.", e); } catch (IOException e) { throw new MojoExecutionException("Command execution failed.", e); } }
From source file:net.minecraft.client.MineExec.java
@Override protected Integer doInBackground() throws IOException, InterruptedException { Executor exe = new DefaultExecutor(); CommandLine mineExec = CommandLine.parse(mineCmd); PipedOutputStream stdout = new PipedOutputStream(); PipedOutputStream stderr = new PipedOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr); exe.setStreamHandler(streamHandler); MinecraftApplet configCrusher = new MinecraftApplet(); try {/*w w w . j av a 2 s. c o m*/ File target = new File(VersionInfo.getTechnicFolder()); BufferedInputStream biserr = new BufferedInputStream(new PipedInputStream(stderr)); ExecuteResultHandler rh = new DefaultExecuteResultHandler(); exe.execute(mineExec, rh); BufferedReader reader = new BufferedReader(new InputStreamReader(biserr)); configCrusher.minecraftLoaded(); String line = reader.readLine(); logWindow.append(line + "\n"); int dupLen = 0; int dupMax = 25; while (line != null) { String line2 = reader.readLine(); if ((line2.contains("Aether") || line2.contains("aether") && watchCfg && new File(target + File.separator + VersionInfo.getModpackName(), "MenuAPI.properties") .exists())) { configCrusher.prepareConfigs(); } if ((Boolean) nbDebug[2]) { if (!line.equals(line2) || dupLen >= dupMax) { if (dupLen > 0) { logWindow.append(line + "(" + dupLen + ")\n"); } else { if (!line.equals(line2)) { logWindow.append(line2 + "\n"); } } dupLen = 0; } else { dupLen++; } line = line2; } else { logWindow.append(line2 + "\n"); line = line2; } } } catch (IOException e) { e.printStackTrace(); } if ((Integer) (nbDebug[1]) <= 1) { configCrusher.remExtras(); configCrusher.minecraftClosed(password); } return 1; }
From source file:com.dubture.composer.core.launch.DefaultExecutableLauncher.java
public DefaultExecutableLauncher() { outputStream = new ByteArrayOutputStream(); errStream = new ByteArrayOutputStream(); streamHandler = new PumpStreamHandler(outputStream, errStream); executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); watchdog = new ExecuteWatchdog(TIMEOUT); executor.setWatchdog(watchdog);//from w w w. j a v a 2 s.co m }
From source file:com.jaeksoft.searchlib.util.ExecuteUtils.java
final public static int command(File workingDirectory, String cmd, String classpath, boolean setJavaTempDir, OutputStream outputStream, OutputStream errorStream, Long timeOut, String... arguments) throws ExecuteException, IOException { Map<String, String> envMap = null; if (classpath != null) { envMap = new HashMap<String, String>(); envMap.put("CLASSPATH", classpath); }/* w w w . ja va 2s. co m*/ CommandLine commandLine = new CommandLine(cmd); if (setJavaTempDir) if (!StringUtils.isEmpty(SystemUtils.JAVA_IO_TMPDIR)) commandLine.addArgument(StringUtils.fastConcat("-Djava.io.tmpdir=", SystemUtils.JAVA_IO_TMPDIR), false); if (arguments != null) for (String argument : arguments) commandLine.addArgument(argument); DefaultExecutor executor = new DefaultExecutor(); if (workingDirectory != null) executor.setWorkingDirectory(workingDirectory); if (outputStream != null) { PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream); executor.setStreamHandler(pumpStreamHandler); } if (timeOut != null) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeOut); executor.setWatchdog(watchdog); } return envMap != null ? executor.execute(commandLine, envMap) : executor.execute(commandLine); }
From source file:com.jredrain.base.utils.CommandUtils.java
public static String executeShell(File shellFile, String... args) { String info = null;//from www . j av a2 s . co m ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { String params = " "; if (CommonUtils.notEmpty(args)) { for (String p : args) { params += p + " "; } } CommandLine commandLine = CommandLine.parse("/bin/bash +x " + shellFile.getAbsolutePath() + params); DefaultExecutor exec = new DefaultExecutor(); exec.setExitValues(null); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputStream); exec.setStreamHandler(streamHandler); exec.execute(commandLine); info = outputStream.toString().trim(); } catch (Exception e) { e.printStackTrace(); } finally { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } return info; } }
From source file:com.nts.alphamale.shell.AdbShellExecutor.java
/** * @param cmd adb /*from w w w . j ava 2s .c o m*/ * @param synch ? ? true: synchronous, false: asynchronous * @param (ms) * @return Executor Standard Output? Map */ public Map<String, Object> execute(CommandLine cmd, boolean synch, long timeoutMilliseconds) { Map<String, Object> executorMap = new HashMap<String, Object>(); DefaultExecutor executor = new DefaultExecutor(); PipedOutputStream pos = new PipedOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(pos, System.err); streamHandler.setStopTimeout(timeoutMilliseconds); DataInputStream dis = null; try { dis = new DataInputStream(new PipedInputStream(pos)); } catch (IOException e) { //log.error(e.getCause() + " : " + e.getMessage()); } executor.setStreamHandler(streamHandler); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeoutMilliseconds); executor.setWatchdog(watchDog); try { if (synch) executor.execute(cmd); else executor.execute(cmd, new DefaultExecuteResultHandler()); log.debug(cmd.toString()); LineIterator li = IOUtils.lineIterator(dis, "UTF-8"); if (li != null) { executorMap.put("executor", executor); executorMap.put("stdOut", li); } } catch (Exception e) { log.error(e.getCause() + ":" + e.getMessage() + "[" + cmd + "]"); } return executorMap; }
From source file:de.woq.osgi.java.itestsupport.ContainerRunner.java
public ContainerRunner(String profile) throws Exception { this.profile = profile; executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(new ContainerOutputStream(), System.err)); watchdog = new ExecuteWatchdog(-1); executor.setWatchdog(watchdog);//from w w w . ja v a2s . c om }