List of usage examples for org.apache.commons.exec Executor setWorkingDirectory
void setWorkingDirectory(File dir);
From source file:org.fornax.toolsupport.sculptor.maven.plugin.GeneratorMojo.java
/** * Executes the commandline running the Eclipse MWE2 launcher and returns * the commandlines exit value./*from w w w . ja v a 2s. co m*/ * * @param changedFiles * list of files from <code>checkFileSets</code> which are * modified since last generator execution or <code>null</code> * if code generation is enforced */ protected List<File> executeGenerator(Set<String> changedFiles) throws MojoExecutionException { List<File> createdFiles = null; // Build executor for projects base directory ScanningMavenLogOutputStream stdout = getStdoutStream(); ScanningMavenLogOutputStream stderr = getStderrStream(); Executor exec = getExecutor(); exec.setWorkingDirectory(project.getBasedir()); exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in)); // Execute commandline and check return code try { int exitValue = exec.execute(getGeneratorCommandLine(changedFiles)); // Check exit value and output streams for errors if (exitValue == 0 && stdout.getErrorCount() == 0 && stderr.getLineCount() == 0) { // Return list of created files createdFiles = stdout.getCreatedFiles(); } } catch (ExecuteException e) { // ignore } catch (IOException e) { // ignore } return createdFiles; }
From source file:org.fornax.toolsupport.sculptor.maven.plugin.GraphvizMojo.java
/** * Executes the command line tool <code>dot</code>. * //from ww w . j a va2 s. co m * @param dotFiles * list of dot files from the * {@link AbstractSculptorMojo#statusFile} */ protected boolean executeDot(Set<String> dotFiles) throws MojoExecutionException { // Build executor for projects base directory MavenLogOutputStream stdout = getStdoutStream(); MavenLogOutputStream stderr = getStderrStream(); Executor exec = getExecutor(); exec.setWorkingDirectory(project.getBasedir()); exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in)); // Execute commandline and check return code try { int exitValue = exec.execute(getDotCommandLine(dotFiles)); if (exitValue == 0 && stdout.getErrorCount() == 0) { return true; } } catch (ExecuteException e) { // ignore } catch (IOException e) { // ignore } return false; }
From source file:org.fuin.esmp.EventStoreStopMojo.java
@Override protected final void executeGoal() throws MojoExecutionException { init();// w w w.jav a 2s . 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.kickstart4j.ApplicationStarter.java
/** * Execute the target application./*from w w w . jav a2 s .c o m*/ */ public void execute() { final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); final ByteArrayOutputStream errStream = new ByteArrayOutputStream(); final Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(destDir); exec.setStreamHandler(new PumpStreamHandler(outStream, errStream)); try { // Spawns an separate thread exec.execute(commandLine, new ExecuteResultHandler() { public void onProcessFailed(final ExecuteException ex) { failed = true; if (startFrame != null) { startFrame.setVisible(false); } System.out.print(errStream.toString()); ErrorDialog.showAndExit(errStream, ex, 1); } public void onProcessComplete(final int exitValue) { // We never get here because the Kickstart4J process // will be killed with "System.exit(..)" before... } }); if (startFrame != null) { startFrameSleep(); startFrame.setVisible(false); } // TODO Any idea for a better handling of this situation? // Should be a rare situation but "onProcessFailed(..)" and this // thread may interfere... If this part is faster than the // "Executor" // this may call "System.exit(..)" and kills the error message // display! // Abort processing if the concurrent thread signals a failure if (failed) { return; } System.out.print(outStream.toString()); if (failed) { return; } if (config.isExitAfterExecute()) { if (failed) { return; } System.exit(0); } if (failed) { return; } listener.startupComplete(); } catch (final IOException ex) { throw new RuntimeException("Error executing target application!", 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);//from w w w . j a va 2 s. com 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.kualigan.maven.plugins.api.DefaultPrototypeHelper.java
protected int executeCommandLine(final String... args) throws ExecuteException, IOException { final Executor exec = new DefaultExecutor(); final Map enviro = new HashMap(); try {// w w w . ja v a 2 s . com final Properties systemEnvVars = CommandLineUtils.getSystemEnvVars(); enviro.putAll(systemEnvVars); } catch (IOException x) { getCaller().getLog().error("Could not assign default system enviroment variables.", x); } final File workingDirectory = new File(System.getProperty("java.io.tmpdir")); final org.apache.commons.exec.CommandLine commandLine = new org.apache.commons.exec.CommandLine(args[0]); for (int i = 1; i < args.length; i++) { commandLine.addArgument(args[i]); } exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in)); exec.setWorkingDirectory(workingDirectory); return exec.execute(commandLine, enviro); }
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 . java 2s . c o m*/ }
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 . j a v a2 s. c o 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 {/*w w w.jav a 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.xdi.util.process.ProcessHelper.java
/** * /*from w w w. j ava 2 s.c o m*/ * @param printJobTimeout * the printJobTimeout (ms) before the watchdog terminates the * print process * @param printInBackground * printing done in the background or blocking * @param streamHandler * @return a print result handler (implementing a future) * @throws IOException * the test failed */ public static PrintResultHandler executeProgram(CommandLine commandLine, String workingDirectory, long printJobTimeout, boolean printInBackground, int successExitValue, ExecuteStreamHandler streamHandler) throws IOException { ExecuteWatchdog watchdog = null; PrintResultHandler resultHandler; // Create the executor and consider the successExitValue as success Executor executor = new DefaultExecutor(); executor.setExitValue(successExitValue); if (StringHelper.isNotEmpty(workingDirectory)) { executor.setWorkingDirectory(new File(workingDirectory)); } // Redirect streams if needed if (streamHandler != null) { executor.setStreamHandler(streamHandler); } // Create a watchdog if requested if (printJobTimeout > 0) { watchdog = new ExecuteWatchdog(printJobTimeout); executor.setWatchdog(watchdog); } // Pass a "ExecuteResultHandler" when doing background printing if (printInBackground) { log.debug(String.format("Executing non-blocking process %s", commandLine.toString())); resultHandler = new PrintResultHandler(watchdog); executor.execute(commandLine, resultHandler); } else { log.debug(String.format("Executing blocking process %s", commandLine.toString())); successExitValue = executor.execute(commandLine); resultHandler = new PrintResultHandler(successExitValue); } return resultHandler; }