List of usage examples for org.apache.commons.exec DefaultExecutor DefaultExecutor
public DefaultExecutor()
From source file:org.eclipse.php.composer.core.launch.execution.ScriptExecutor.java
public ScriptExecutor() { outBuilder = new StringBuilder(); errBuilder = new StringBuilder(); errStream = new LogOutputStream() { @Override//from ww w . j a va 2 s . c o m protected void processLine(String line, int level) { if (!line.isEmpty()) { errBuilder.append(line + "\n"); //$NON-NLS-1$ for (ExecutionResponseListener listener : listeners) { listener.executionMessage(line); } } } }; outStream = new LogOutputStream() { @Override protected void processLine(String line, int level) { if (!line.isEmpty()) { outBuilder.append(line + "\n"); //$NON-NLS-1$ for (ExecutionResponseListener listener : listeners) { listener.executionMessage(line); } } } }; streamHandler = new PumpStreamHandler(outStream, errStream); executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); }
From source file:org.eclipse.sisu.equinox.launching.internal.DefaultEquinoxLauncher.java
@Override public int execute(LaunchConfiguration configuration, int forkedProcessTimeoutInSeconds) throws EquinoxLaunchingException { String executable = configuration.getJvmExecutable(); if (executable == null || "".equals(executable)) { // use the same JVM as the one used to run Maven (the "java.home" one) executable = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; if (File.separatorChar == '\\') { executable = executable + ".exe"; }/*from w w w .j a v a2 s. c o m*/ } CommandLine cli = new CommandLine(executable); final boolean handleQuotes = false; cli.addArguments(configuration.getVMArguments(), handleQuotes); cli.addArguments(new String[] { "-jar", getCanonicalPath(configuration.getLauncherJar()) }, handleQuotes); cli.addArguments(configuration.getProgramArguments(), handleQuotes); log.info("Command line:\n\t" + cli.toString()); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = null; if (forkedProcessTimeoutInSeconds > 0) { watchdog = new ExecuteWatchdog(forkedProcessTimeoutInSeconds * 1000L); executor.setWatchdog(watchdog); } // best effort to avoid orphaned child process executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); executor.setWorkingDirectory(configuration.getWorkingDirectory()); try { return executor.execute(cli, getMergedEnvironment(configuration)); } catch (ExecuteException e) { if (watchdog != null && watchdog.killedProcess()) { log.error("Timeout " + forkedProcessTimeoutInSeconds + " s exceeded. Process was killed."); } return e.getExitValue(); } catch (IOException e) { throw new EquinoxLaunchingException(e); } }
From source file:org.eclipse.smarthome.io.net.exec.ExecUtil.java
/** * <p>//from w ww . j a 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()); 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.epics.archiverappliance.TomcatSetup.java
private void createAndStartTomcatInstance(String testName, final String applianceName, int port, int startupPort) throws IOException { File workFolder = makeTomcatFolders(testName, applianceName, port, startupPort); HashMap<String, String> environment = createEnvironment(testName, applianceName); File logsFolder = new File(workFolder, "logs"); assert (logsFolder.exists()); CommandLine cmdLine = new CommandLine( System.getenv("TOMCAT_HOME") + File.separator + "bin" + File.separator + "catalina.sh"); cmdLine.addArgument("run"); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); final CountDownLatch latch = new CountDownLatch(1); PumpStreamHandler pump = new PumpStreamHandler(new LogOutputStream() { @Override// www. jav a 2 s .c om protected void processLine(String msg, int level) { if (msg != null && msg.contains("All components in this appliance have started up")) { logger.info(applianceName + " has started up."); latch.countDown(); } System.out.println(msg); } }, System.err); ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); Executor executor = new DefaultExecutor(); executor.setExitValue(1); executor.setWatchdog(watchdog); executor.setStreamHandler(pump); executor.setWorkingDirectory(logsFolder); executor.execute(cmdLine, environment, resultHandler); executorWatchDogs.add(watchdog); // We wait for some time to make sure the server started up try { latch.await(2, TimeUnit.MINUTES); } catch (InterruptedException ex) { } logger.info("Done starting tomcat for the testing."); }
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);//from w w w. ja v a 2 s. c o m 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.evosuite.utils.ProcessLauncher.java
public int launchNewProcess(File baseDir, String cmdString, int timeout) throws IOException, ProcessTimeoutException { DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog timeoutWatchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(timeoutWatchdog); PumpStreamHandler streamHandler = new PumpStreamHandler(this.outAndErr, this.outAndErr, this.input); executor.setStreamHandler(streamHandler); if (baseDir != null) { executor.setWorkingDirectory(baseDir); }/*from w w w. j a v a 2 s .co m*/ int exitValue; try { logger.debug("About to execute command " + cmdString); exitValue = executor.execute(CommandLine.parse(cmdString)); if (executor.isFailure(exitValue) && timeoutWatchdog.killedProcess()) { // it was killed on purpose by the watchdog logger.debug("A timeout occured while executing a process"); logger.debug("The command is " + cmdString); throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString); } return exitValue; } catch (ExecuteException e) { if (timeoutWatchdog.killedProcess()) { logger.debug("A timeout occured while executing a process"); logger.debug("The command is " + cmdString); throw new ProcessTimeoutException("A timeout occurred while executing command " + cmdString); } else { throw e; } } }
From source file:org.excalibur.service.aws.CommandExample.java
public static void main(String[] args) throws ExecuteException, IOException, InterruptedException { CommandLine command = new CommandLine("/bin/bash"); command.addArgument("-c", false); command.addArgument("iperf3 -t 30 -c iperf.scottlinux.com >> output.txt", false); //Process process = Runtime.getRuntime().exec(new String[]{"bash", "-c", "iperf3 -t 60 -c localhost"}); // System.out.println(new Mirror().on(process).get().field("pid")); //process.waitFor(); // System.out.println(process.exitValue()); // ManagementFactory.getRuntimeMXBean().getName(); // System.out.println(IOUtils.readLines(process.getInputStream())); //String command = "iperf3 -t 30 -c iperf.scottlinux.com"; ExecuteWatchdog watchdog = new ExecuteWatchdog(10); final DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler()); executor.setExitValue(1);// www . j a v a2 s .c om executor.execute(command, new ExecuteResultHandler() { @Override public void onProcessFailed(ExecuteException e) { e.printStackTrace(); } @Override public void onProcessComplete(int exitValue) { System.out.println(exitValue); } }); }
From source file:org.excalibur.service.compute.executor.Worker.java
public void execute(final Application application, ExecuteResultHandler executeResultHandler) throws ExecuteException, IOException { final String commandLine = application.getExecutableCommandLine(); DefaultExecutor executor = new DefaultExecutor(); CommandLine command = new CommandLine("/bin/sh"); command.addArgument("-c", false); command.addArgument(commandLine, false); executor.execute(command, System.getenv(), executeResultHandler); LOG.debug("Launched the execution of task: [{}], uuid: [{}]", commandLine, application.getId()); }
From source file:org.fornax.toolsupport.sculptor.maven.plugin.GeneratorMojo.java
/** * Returns {@link Executor} implementation. * <p>//from w ww. j a v a 2 s.c om * Can be overriden to replace the default implementation of * {@link DefaultExecutor}. */ protected Executor getExecutor() { return new DefaultExecutor(); }
From source file:org.fuin.esmp.EventStoreDownloadMojo.java
private void applyFileMode(final File file, final FileMode fileMode) throws MojoExecutionException { if (OS.isFamilyUnix() || OS.isFamilyMac()) { final String smode = fileMode.toChmodStringFull(); final CommandLine cmdLine = new CommandLine("chmod"); cmdLine.addArgument(smode);/*from w ww . j a va2s. co m*/ cmdLine.addArgument(file.getAbsolutePath()); final Executor executor = new DefaultExecutor(); try { final int result = executor.execute(cmdLine); if (result != 0) { throw new MojoExecutionException("Error # " + result + " while trying to set mode \"" + smode + "\" for file: " + file.getAbsolutePath()); } } catch (final IOException ex) { throw new MojoExecutionException( "Error while trying to set mode \"" + smode + "\" for file: " + file.getAbsolutePath(), ex); } } else { file.setReadable(fileMode.isUr() || fileMode.isGr() || fileMode.isOr()); file.setWritable(fileMode.isUw() || fileMode.isGw() || fileMode.isOw()); file.setExecutable(fileMode.isUx() || fileMode.isGx() || fileMode.isOx()); } }