List of usage examples for org.apache.commons.exec PumpStreamHandler PumpStreamHandler
public PumpStreamHandler(final OutputStream outAndErr)
PumpStreamHandler
. From source file:org.estatio.webapp.services.other.EstatioOtherServices.java
@Programmatic String execute(String command) { int exitValue = 1; CommandLine commandLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0);/* w ww .ja v a2 s.c om*/ ExecuteStreamHandler handler = executor.getStreamHandler(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); executor.setStreamHandler(psh); try { handler.setProcessOutputStream(System.in); } catch (IOException e) { } try { exitValue = executor.execute(commandLine); } catch (ExecuteException e) { return e.getMessage(); } catch (IOException e) { return e.getMessage(); } return stdout.toString(); }
From source file:org.fuin.esmp.EventStorePostStartMojo.java
@Override protected final void executeGoal() throws MojoExecutionException { if (postStartCommand == null) { throw new MojoExecutionException("postStartCommand not set"); }//from w ww . j av a 2s . com LOG.info("postStartCommand={}", postStartCommand); final CommandLine cmdLine = new CommandLine(postStartCommand); final DefaultExecutor executor = new DefaultExecutor(); try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final PumpStreamHandler psh = new PumpStreamHandler(bos); executor.setStreamHandler(psh); executor.setWorkingDirectory(getEventStoreDir()); final int exitCode = executor.execute(cmdLine); messages = asList(bos.toString()); if (exitCode == 0) { LOG.info("Post-start command executed successfully"); logDebug(messages); } else { LOG.error("Post-start command failed with exit code: {}", exitCode); logError(messages); } } catch (final IOException ex) { throw new MojoExecutionException("Error executing the command line: " + cmdLine, ex); } }
From source file:org.fuin.esmp.EventStoreStartMojo.java
@Override protected final void executeGoal() throws MojoExecutionException { init();/* www . ja v a2s . c o m*/ LOG.info("command={}", command); LOG.info("arguments={}", Arrays.toString(arguments)); final CommandLine cmdLine = createCommandLine(); final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); final DaemonExecutor executor = new DaemonExecutor(); try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final PumpStreamHandler psh = new PumpStreamHandler(bos); executor.setStreamHandler(psh); executor.setWorkingDirectory(getEventStoreDir()); executor.execute(cmdLine, resultHandler); final List<String> messages = waitForHttpServer(resultHandler, bos); logDebug(messages); final String pid = extractPid(messages); LOG.info("Event store process ID: {}", pid); writePid(pid); } catch (final IOException ex) { throw new MojoExecutionException("Error executing the command line: " + cmdLine, ex); } }
From source file:org.fuin.esmp.EventStoreStopMojo.java
@Override protected final void executeGoal() throws MojoExecutionException { init();//from w w w. ja v a2s . c o m LOG.info("command={}", command); final CommandLine cmdLine = createCommandLine(); final Executor executor = new DefaultExecutor(); try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final PumpStreamHandler psh = new PumpStreamHandler(bos); executor.setStreamHandler(psh); executor.setWorkingDirectory(getEventStoreDir()); final int result = executor.execute(cmdLine); if (result != 0) { throw new MojoExecutionException("Error stopping the event store: " + result); } final List<String> messages = asList(bos.toString()); logDebug(messages); deletePid(); LOG.info("Event store successfully stopped"); } catch (final IOException ex) { throw new MojoExecutionException("Error executing the command line: " + cmdLine, ex); } }
From source file:org.fuin.owndeb.commons.DebUtils.java
private static void execute(final CommandLine cmdLine, final File workingDir, final String errorMsg) { LOG.debug("Execute: " + cmdLine.toString()); final Executor executor = new DefaultExecutor(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final PumpStreamHandler psh = new PumpStreamHandler(bos); executor.setStreamHandler(psh);/* ww w.j a va 2 s . c o m*/ executor.setWorkingDirectory(workingDir); try { final int result = executor.execute(cmdLine); if (result != 0) { logError(asList(bos.toString())); throw new RuntimeException("Error # " + result + " / " + errorMsg); } } catch (final IOException ex) { logError(asList(bos.toString())); throw new RuntimeException(errorMsg, ex); } }
From source file:org.jboss.tools.windup.runtime.WindupRmiClient.java
public void startWindup(final IProgressMonitor monitor, String jreHome) { logInfo("Begin start RHAMT."); //$NON-NLS-1$ monitor.worked(1);//from w w w. j a v a 2 s. c o m String windupExecutable = WindupRuntimePlugin.computeWindupExecutable(); if (windupExecutable == null) { WindupRuntimePlugin.logErrorMessage("rhamt-cli not specified."); //$NON-NLS-1$ return; } boolean executable = new File(windupExecutable).setExecutable(true); if (!executable) { WindupRuntimePlugin.logErrorMessage("rhamt-cli not executable."); //$NON-NLS-1$ return; } CommandLine cmdLine = CommandLine.parse(windupExecutable); Map<String, String> env = Maps.newHashMap(); for (Map.Entry<String, String> entry : System.getenv().entrySet()) { env.put(entry.getKey(), entry.getValue()); } if (!jreHome.trim().isEmpty()) { env.put(JAVA_HOME, jreHome); } logInfo("Using " + JAVA_HOME + " - " + jreHome); cmdLine.addArgument("--startServer"); //$NON-NLS-1$ cmdLine.addArgument(String.valueOf(getRmiPort())); watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); ExecuteResultHandler handler = new ExecuteResultHandler() { @Override public void onProcessFailed(ExecuteException e) { logInfo("The RHAMT process failed:"); //$NON-NLS-1$ logInfo(e.getMessage()); //$NON-NLS-1$ executionBuilder = null; notifyServerChanged(); } @Override public void onProcessComplete(int exitValue) { logInfo("The RHAMT process has completed."); //$NON-NLS-1$ executionBuilder = null; notifyServerChanged(); } }; DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int logLevel) { logInfo("Message from RHAMT executor: " + line); //$NON-NLS-1$ monitor.worked(1); } })); executor.setWatchdog(watchdog); executor.setExitValue(1); monitor.worked(1); try { logInfo("Starting RHAMT in server mode..."); //$NON-NLS-1$ logInfo("Command-line: " + cmdLine); //$NON-NLS-1$ executor.execute(cmdLine, env, handler); } catch (IOException e) { WindupRuntimePlugin.log(e); } }
From source file:org.jlab.clara.std.services.DataManager.java
private void stageInputFile(FilePaths files, EngineData output) { Path stagePath = FileUtils.getParent(files.stagedInputFile); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {//from w w w . j av a2s .c o m FileUtils.createDirectories(stagePath); CommandLine cmdLine = new CommandLine("cp"); cmdLine.addArgument(files.inputFile.toString()); cmdLine.addArgument(files.stagedInputFile.toString()); DefaultExecutor executor = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); executor.execute(cmdLine); System.out.printf("%s service: input file '%s' copied to '%s'%n", NAME, files.inputFile, stagePath); returnFilePaths(output, files); } catch (ExecuteException e) { ServiceUtils.setError(output, "could not complete request: " + outputStream.toString().trim()); } catch (IOException e) { ServiceUtils.setError(output, "could not complete request: " + e.getMessage()); } }
From source file:org.jlab.clara.std.services.DataManager.java
private void removeStagedInputFile(FilePaths files, EngineData output) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {//from w ww. j a v a 2 s. c o m CommandLine cmdLine = new CommandLine("rm"); cmdLine.addArgument(files.stagedInputFile.toString()); DefaultExecutor executor = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); executor.execute(cmdLine); System.out.printf("%s service: staged input file %s removed%n", NAME, files.stagedInputFile); returnFilePaths(output, files); } catch (ExecuteException e) { ServiceUtils.setError(output, "could not complete request: " + outputStream.toString().trim()); } catch (IOException e) { ServiceUtils.setError(output, "could not complete request: " + e.getMessage()); } }
From source file:org.jlab.clara.std.services.DataManager.java
private void saveOutputFile(FilePaths files, EngineData output) { Path outputPath = FileUtils.getParent(files.outputFile); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try {/*from w w w . j a v a 2 s.co m*/ FileUtils.createDirectories(outputPath); CommandLine cmdLine = new CommandLine("mv"); // cmdLine.addArgument(files.stagedOutputFile.toString()); // cmdLine.addArgument(files.outputFile.toString()); // modified 09.12.18. Stage back multiple output files. vg Files.list(directoryPaths.stagePath).forEach(name -> { name.startsWith(files.stagedOutputFile.toString()); cmdLine.addArgument(name.toString()); }); cmdLine.addArgument(outputPath.toString()); // vg DefaultExecutor executor = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); executor.execute(cmdLine); System.out.printf("%s service: output file '%s' saved to '%s'%n", NAME, files.stagedOutputFile, outputPath); returnFilePaths(output, files); } catch (ExecuteException e) { ServiceUtils.setError(output, "could not complete request: " + outputStream.toString().trim()); } catch (IOException e) { ServiceUtils.setError(output, "could not complete request: " + e.getMessage()); } }
From source file:org.kercoin.magrit.core.build.BuildTask.java
private int build(ByteArrayOutputStream stdout, PrintStream printOut) throws IOException { String command = findCommand(); printOut.println(String.format("Starting build with command '%s'", command)); CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executable = new DefaultExecutor(); executable.setWorkingDirectory(repository.getDirectory().getParentFile()); executable.setStreamHandler(new PumpStreamHandler(stdout)); return executable.execute(cmdLine); }