List of usage examples for org.apache.commons.exec DefaultExecutor setStreamHandler
public void setStreamHandler(final ExecuteStreamHandler streamHandler)
From source file:hoot.services.nativeinterfaces.CommandRunnerImpl.java
@Override public CommandResult exec(String[] command) throws IOException { logger.debug("Executing the following command: {}", Arrays.toString(command)); try (OutputStream stdout = new ByteArrayOutputStream(); OutputStream stderr = new ByteArrayOutputStream()) { this.stdout = stdout; this.stderr = stderr; CommandLine cmdLine = new CommandLine(command[0]); for (int i = 1; i < command.length; i++) { cmdLine.addArgument(command[i], false); }//from w ww . j av a2s .c o m ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(stdout, stderr); DefaultExecutor executor = new DefaultExecutor(); this.watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(this.watchDog); executor.setStreamHandler(executeStreamHandler); int exitValue; try { exitValue = executor.execute(cmdLine); if (executor.isFailure(exitValue) && this.watchDog.killedProcess()) { // it was killed on purpose by the watchdog logger.info("Process for '{}' command was killed!", cmdLine); } } catch (Exception e) { exitValue = -1; logger.warn("Error executing: {}", cmdLine, e); } CommandResult commandResult = new CommandResult(cmdLine.toString(), exitValue, stdout.toString(), stderr.toString()); logger.debug("Finished executing: {}", commandResult); return commandResult; } }
From source file:com.blackducksoftware.tools.scmconnector.core.CommandLineExecutor.java
@Override public CommandResults executeCommandForOutput(Logger log, CommandLine command, File targetDir) throws Exception { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0);// w w w. j a va2 s. c o m ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(outputStream); executor.setStreamHandler(psh); int exitStatus = 1; executor.setWorkingDirectory(targetDir); exitStatus = executor.execute(command); String outputString = outputStream.toString(); // This log msg would reveal password // log.info("Command: " + command.toString() + " executed in: " // + targetDir.toString() + "; output: " + outputString); return new CommandResults(exitStatus, outputString); }
From source file:com.k42b3.aletheia.filter.request.Process.java
public void exec(Request request) { String cmd = getConfig().getProperty("cmd"); try {//from w w w.j a va 2s. c o m logger.info("Execute: " + cmd); CommandLine commandLine = CommandLine.parse(cmd); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); DefaultExecutor executor = new DefaultExecutor(); this.baos = new ByteArrayOutputStream(); this.baosErr = new ByteArrayOutputStream(); this.bais = new ByteArrayInputStream(request.getContent().getBytes()); executor.setStreamHandler(new PumpStreamHandler(this.baos, this.baosErr, this.bais)); executor.setWatchdog(watchdog); executor.execute(commandLine); logger.info("Output: " + this.baos.toString()); request.setContent(this.baos.toString()); } catch (Exception e) { logger.warning(e.getMessage()); } }
From source file:de.tu_dresden.psy.fca.ConexpCljBridge.java
public ConexpCljBridge() { this.b = new byte[1]; /**/*from w ww . j a v a 2s .c om*/ * build the command line (see conexp-clj/bin/conexp-clj) */ String java_bin = Launcher.getJavaCommand(); CommandLine conexp_cmd = new CommandLine(java_bin); conexp_cmd.addArgument("-server"); conexp_cmd.addArgument("-cp"); conexp_cmd.addArgument("./conexp-clj/lib/conexp-clj-0.0.7-alpha-SNAPSHOT-standalone.jar"); conexp_cmd.addArgument("clojure.main"); conexp_cmd.addArgument("-e"); conexp_cmd.addArgument(""); conexp_cmd.addArgument("./conexp-clj/lib/conexp-clj.clj"); /** * open the pipes */ this.to_conexp = new PipedOutputStream(); try { this.stream_to_conexp = new PipedInputStream(this.to_conexp, 2048); } catch (IOException e2) { e2.printStackTrace(); } this.stream_error_conexp = new PipedOutputStream(); this.stream_from_conexp = new PipedOutputStream(); try { this.from_conexp = new PipedInputStream(this.stream_from_conexp, 2048); } catch (IOException e1) { e1.printStackTrace(); } try { this.error_conexp = new PipedInputStream(this.stream_error_conexp, 2048); } catch (IOException e1) { e1.printStackTrace(); } /** * setup apache commons exec */ this.result = new DefaultExecuteResultHandler(); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setStreamHandler( new PumpStreamHandler(this.stream_from_conexp, this.stream_error_conexp, this.stream_to_conexp)); /** * run in non-blocking mode */ try { executor.execute(conexp_cmd, this.result); } catch (ExecuteException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } this.output_buffer = ""; }
From source file:com.oneops.inductor.ProcessRunner.java
/** * Creates a process and logs the output * * @param cmd/* www. ja v a2 s . co m*/ * @param logKey * @param result */ private void executeProcess(String[] cmd, String logKey, ProcessResult result) { Map<String, String> env = getEnvVars(logKey, cmd); logger.info(logKey + " Cmd: " + String.join(" ", cmd) + ", Env: " + env); // run the cmd try { CommandLine cmdLine = new CommandLine(cmd[0]); // add rest of cmd string[] as arguments for (int i = 1; i < cmd.length; i++) { // needs the quote handling=false or else doesn't work // http://www.techques.com/question/1-5080109/How-to-execute--bin-sh-with-commons-exec? cmdLine.addArgument(cmd[i], false); } DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWatchdog(new ExecuteWatchdog(timeoutInSeconds * 1000)); executor.setStreamHandler(new OutputHandler(logger, logKey, result)); result.setResultCode(executor.execute(cmdLine, env)); // set fault to last error if fault map is empty if (result.getResultCode() != 0 && result.getFaultMap().keySet().size() < 1) { result.getFaultMap().put("ERROR", result.getLastError()); } } catch (ExecuteException ee) { logger.error(logKey + ee); result.setResultCode(ee.getExitValue()); } catch (IOException e) { logger.error(e); result.setResultCode(1); } }
From source file:com.boulmier.machinelearning.jobexecutor.job.Job.java
public void start() throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final DefaultExecutor exec = new DefaultExecutor(); final ExecuteWatchdog wd = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); final PumpStreamHandler output = new PumpStreamHandler(out); final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler(); exec.setWatchdog(wd);// w ww .j a v a2 s .c o m exec.setStreamHandler(output); exec.execute(cl, handler); JobExecutor.logger.info("Running job " + jobid); new Thread(new Runnable() { @Override public void run() { try { handler.waitFor(); Computer.ComputeProperties properties = Computer.ComputeProperties.buildFromRequest(req); new SenderComputer(new StorageComputer(out.toString(), properties)).compute(); JobExecutor.logger.info("Job complete " + jobid); } catch (InterruptedException ex) { exec.getWatchdog().destroyProcess(); JobExecutor.logger.error( "Job (" + jobid + ") has been destroyed due to internal error " + ex.getMessage()); } } }).start(); }
From source file:com.github.cshubhamrao.MediaConverter.MainUI.java
/** Runs ffmpeg -version */ @Override//from www .j ava2s . c o m protected Void doInBackground() { ffmpeg = FFMpegLoader.getFFMpegExecutable(); if (ffmpeg != null) { try { cmd = new CommandLine(ffmpeg); cmd.addArgument("-version"); OutputStream outputStream = new ByteArrayOutputStream(); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(10000); exec.setWatchdog(watchdog); exec.execute(cmd); publish(outputStream.toString()); } catch (ExecuteException ex) { Logger.getLogger(DisplayVersion.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(DisplayVersion.class.getName()).log(Level.SEVERE, null, ex); } } else { try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(DisplayVersion.class.getName()).log(Level.SEVERE, null, ex); } } return null; }
From source file:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java
protected boolean loadIntoRam(String executable, File ramFile, String comPort) { try {//from ww w . ja va 2 s . c o m Map map = new HashMap(); map.put("ramFile", ramFile); CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-r"); if (comPort != null) { cmdLine.addArgument("-p").addArgument(comPort); } cmdLine.addArgument("${ramFile}"); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); //executor.setExitValues(new int[]{451, 301}); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); try { exitValue = executor.execute(cmdLine); } catch (ExecuteException ee) { exitValue = ee.getExitValue(); logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue); success = false; return false; } finally { output = outputStream.toString(); } success = true; return true; } catch (IOException ioe) { logger.log(Level.SEVERE, null, ioe); success = false; return false; } }
From source file:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java
protected List<String> getPorts(String executable) { List<String> ports = new ArrayList<>(); try {/*from w w w . j ava 2s . com*/ CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-P"); DefaultExecutor executor = new DefaultExecutor(); //executor.setExitValues(new int[]{451, 301}); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); try { exitValue = executor.execute(cmdLine); } catch (ExecuteException ee) { exitValue = ee.getExitValue(); logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue); return ports; } finally { output = outputStream.toString(); } /* if (exitValue == 301) { return ports; } */ // System.out.println("output: " + output); Scanner scanner = new Scanner(output); while (scanner.hasNextLine()) { ports.add(scanner.nextLine()); } return ports; } catch (IOException ioe) { logger.log(Level.SEVERE, null, ioe); return null; } }
From source file:eu.creatingfuture.propeller.blocklyprop.propeller.PropellerLoad.java
protected boolean loadIntoEeprom(String executable, File eepromFile, String comPort) { try {/*from w w w .j a va 2 s. c o m*/ Map map = new HashMap(); map.put("eepromFile", eepromFile); CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-r"); cmdLine.addArgument("-e"); if (comPort != null) { cmdLine.addArgument("-p").addArgument(comPort); } cmdLine.addArgument("${eepromFile}"); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); //executor.setExitValues(new int[]{451, 301}); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); executor.setStreamHandler(streamHandler); try { exitValue = executor.execute(cmdLine); } catch (ExecuteException ee) { exitValue = ee.getExitValue(); logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue); success = false; return false; } finally { output = outputStream.toString(); } success = true; return true; } catch (IOException ioe) { logger.log(Level.SEVERE, null, ioe); success = false; return false; } }