List of usage examples for org.apache.commons.exec Executor execute
int execute(CommandLine command) throws ExecuteException, IOException;
From source file:com.jivesoftware.os.jive.utils.shell.utils.Invoke.java
public static int invoke(File home, String[] command, final InputStream writeToProcess, final ConcurrentLinkedQueue<String> response) throws Exception { Executor executor = new DefaultExecutor(); executor.setExitValue(0);//ww w . j a v a2 s. c om if (home != null) { executor.setWorkingDirectory(home); } //give all the processes 120s to return that they started successfully ExecuteWatchdog watchdog = new ExecuteWatchdog(120000); executor.setWatchdog(watchdog); LogOutputStream outputStream = new LogOutputStream(20000) { @Override protected void processLine(final String line, final int level) { response.add(line); } }; LogOutputStream errorStream = new LogOutputStream(40000) { @Override protected void processLine(final String line, final int level) { response.add(line); } }; PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream, writeToProcess); executor.setStreamHandler(pumpStreamHandler); executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); CommandLine commandLine = new CommandLine(command[0]); for (int i = 1; i < command.length; i++) { commandLine.addArgument(command[i]); } try { //executor.execute(commandLine, handler); return executor.execute(commandLine); //handler.waitFor(20000); } catch (Exception x) { x.printStackTrace(); return 1; } }
From source file:com.tascape.qa.th.android.comm.Adb.java
public List<String> adb(final List<Object> arguments) throws IOException { CommandLine cmdLine = new CommandLine(ADB); if (!this.serial.isEmpty()) { cmdLine.addArgument("-s"); cmdLine.addArgument(serial);//from w w w . ja va2s .c o m } arguments.forEach((arg) -> { cmdLine.addArgument(arg + ""); }); LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " ")); List<String> output = new ArrayList<>(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new ESH(output)); if (executor.execute(cmdLine) != 0) { throw new IOException(cmdLine + " failed"); } return output; }
From source file:com.github.mjeanroy.maven.plugins.node.commands.CommandExecutor.java
/** * Execute command line and return the result status. * * @param workingDirectory Working directory (i.e where the command line is executed). * @param command Command, containing executable path with arguments. * @param logger Logger to use to log command output. * @return Command result object.//from w w w. j a v a2s . c o m */ public CommandResult execute(File workingDirectory, Command command, Log logger) { CommandLine commandLine = new CommandLine(command.getExecutable()); for (String argument : command.getArguments()) { commandLine.addArgument(argument); } try { Executor executor = new DefaultExecutor(); executor.setWorkingDirectory(workingDirectory); executor.setExitValue(0); // Define custom output stream LogStreamHandler stream = new LogStreamHandler(logger); PumpStreamHandler handler = new PumpStreamHandler(stream); executor.setStreamHandler(handler); int status = executor.execute(commandLine); return new CommandResult(status); } catch (ExecuteException ex) { return new CommandResult(ex.getExitValue()); } catch (IOException ex) { throw new CommandException(ex); } }
From source file:io.gatling.mojo.Fork.java
public void run() throws Exception { if (propagateSystemProperties) { for (Entry<Object, Object> systemProp : System.getProperties().entrySet()) { String name = systemProp.getKey().toString(); String value = systemProp.getValue().toString(); if (isPropagatableProperty(name)) { String escapedValue = StringUtils.escape(value); String safeValue = escapedValue.contains(" ") ? '"' + escapedValue + '"' : escapedValue; this.jvmArgs.add("-D" + name + "=" + safeValue); }//from w w w.j ava 2 s. co m } } this.jvmArgs.add("-jar"); this.jvmArgs .add(MojoUtils.createBooterJar(classpath, MainWithArgsInFile.class.getName()).getCanonicalPath()); List<String> command = buildCommand(); Executor exec = new DefaultExecutor(); exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in)); exec.setProcessDestroyer(new ShutdownHookProcessDestroyer()); CommandLine cl = new CommandLine(javaExecutable); for (String arg : command) { cl.addArgument(arg, false); } int exitValue = exec.execute(cl); if (exitValue != 0) { throw new MojoFailureException("command line returned non-zero value:" + exitValue); } }
From source file:com.adaptris.hpcc.DfuPlusWrapper.java
protected void executeInternal(CommandLine cmdLine, OutputStream stdout) throws ProduceException, AbortJobException { int exit = -1; ExecuteWatchdog watchdog = new ExecuteWatchdog(EXEC_TIMEOUT_INTERVAL.toMilliseconds()); try (OutputStream out = stdout) { Executor cmd = new DefaultExecutor(); cmd.setWatchdog(watchdog);/*w w w. j a va 2 s . com*/ PumpStreamHandler pump = new ManagedPumpStreamHandler(out); cmd.setStreamHandler(pump); cmd.setExitValues(null); exit = cmd.execute(cmdLine); } catch (Exception e) { throw ExceptionHelper.wrapProduceException(e); } if (watchdog.killedProcess() || exit != 0) { throw new AbortJobException("Job killed due to timeout/ExitCode != 0"); } }
From source file:io.gatling.mojo.GatlingJavaMainCallerByFork.java
@Override public boolean run(boolean displayCmd, boolean throwFailure) throws Exception { List<String> cmd = buildCommand(); displayCmd(displayCmd, cmd);/*from ww w . ja va 2s .c om*/ Executor exec = new DefaultExecutor(); // err and out are redirected to out exec.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in)); exec.setProcessDestroyer(new ShutdownHookProcessDestroyer()); CommandLine cl = new CommandLine(cmd.get(0)); for (int i = 1; i < cmd.size(); i++) { cl.addArgument(cmd.get(i), false); } try { int exitValue = exec.execute(cl); if (exitValue != 0) { if (throwFailure) { throw new MojoFailureException("command line returned non-zero value:" + exitValue); } return false; } return true; } catch (ExecuteException exc) { if (throwFailure) { throw exc; } return false; } }
From source file:cc.arduino.contributions.packages.ContributionInstaller.java
private void executeScripts(File folder, Collection<File> postInstallScripts, boolean trusted, boolean trustAll) throws IOException { File script = postInstallScripts.iterator().next(); if (!trusted && !trustAll) { System.err.println(/*ww w. j a v a 2 s. c o m*/ I18n.format(tr("Warning: non trusted contribution, skipping script execution ({0})"), script)); return; } if (trustAll) { System.err.println(I18n.format(tr("Warning: forced untrusted script execution ({0})"), script)); } ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler(stdout, stderr)); executor.setWorkingDirectory(folder); executor.setExitValues(null); int exitValue = executor.execute(new CommandLine(script)); executor.setExitValues(new int[0]); System.out.write(stdout.toByteArray()); System.err.write(stderr.toByteArray()); if (executor.isFailure(exitValue)) { throw new IOException(); } }
From source file:gr.upatras.ece.nam.baker.impl.InstalledBunLifecycleMgmt.java
public int executeSystemCommand(String cmdStr) { logger.info(" ================> Execute :" + cmdStr); CommandLine cmdLine = CommandLine.parse(cmdStr); final Executor executor = new DefaultExecutor(); // create the executor and consider the exitValue '0' as success executor.setExitValue(0);// w w w . j a va 2 s . com ByteArrayOutputStream out = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(out); executor.setStreamHandler(streamHandler); int exitValue = -1; try { exitValue = executor.execute(cmdLine); logger.info(" ================> EXIT (" + exitValue + ") FROM :" + cmdStr); } catch (ExecuteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } logger.info("out>" + out); return exitValue; }
From source file:com.github.peterjanes.node.NpmPackMojo.java
/** * * @throws MojoExecutionException if anything unexpected happens. *///from www . j av a2 s. c o m public void execute() throws MojoExecutionException { if (!outputDirectory.exists()) { outputDirectory.mkdirs(); } CommandLine commandLine = new CommandLine(executable); Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(outputDirectory); List commandArguments = new ArrayList(); commandArguments.add("pack"); commandArguments.add("./commonjs"); String[] args = new String[commandArguments.size()]; for (int i = 0; i < commandArguments.size(); i++) { args[i] = (String) commandArguments.get(i); } commandLine.addArguments(args, false); OutputStream stdout = System.out; OutputStream stderr = System.err; try { getLog().debug("Executing command line: " + commandLine); exec.setStreamHandler(new PumpStreamHandler(stdout, stderr, System.in)); int resultCode = exec.execute(commandLine); if (0 != resultCode) { throw new MojoExecutionException( "Result of " + commandLine + " execution is: '" + resultCode + "'."); } Artifact artifact = mavenProject.getArtifact(); String fileName = String.format("%s-%s.tgz", artifact.getArtifactId(), mavenProject.getProperties().getProperty("node.project.version")); artifact.setFile(new File(outputDirectory, fileName)); } catch (ExecuteException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } catch (IOException e) { throw new MojoExecutionException(EXECUTION_FAILED, e); } }
From source file:edu.isi.misd.scanner.network.registry.data.service.RegistryServiceImpl.java
@Override public String convertDataProcessingSpecification(String workingDir, String execName, String args, String input, Integer dataSetId) throws Exception { Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(new File(workingDir)); CommandLine cl = new CommandLine(execName); cl.addArgument(args);/*from ww w. j ava 2 s . c o m*/ ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes("UTF-8")); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(out, err, in); exec.setStreamHandler(streamHandler); try { exec.execute(cl); } catch (Exception e) { throw new Exception(e.toString() + "\n\n" + err.toString("UTF-8")); } String output = out.toString("UTF-8"); if (dataSetId != null) { DataSetDefinition dataSet = dataSetDefinitionRepository.findOne(dataSetId); if (dataSet == null) { throw new ResourceNotFoundException(dataSetId); } dataSet.setDataProcessingSpecification(input); dataSet.setDataProcessingProgram(output); dataSetDefinitionRepository.save(dataSet); } return output; }