List of usage examples for org.apache.commons.exec DefaultExecutor setStreamHandler
public void setStreamHandler(final ExecuteStreamHandler streamHandler)
From source file:edu.stolaf.cs.wmrserver.testjob.TestJobTask.java
public TestJobResult call() throws IOException { // Create the result object TestJobResult result = new TestJobResult(); // Map//from w w w . java 2 s . c om CappedInputStream mapInput = null; try { // List the input files and open a stream FileSystem fs = _inputPath.getFileSystem(_conf); FileStatus[] files = JobServiceHandler.listInputFiles(fs, _inputPath); AggregateInputStream aggregateInput = new AggregateInputStream(fs, files); mapInput = new CappedInputStream(aggregateInput, _inputCap); // Run the mapper result.setMapResult(runTransform(_id, _mapperFile, _packageDir, mapInput)); } finally { IOUtils.closeQuietly(mapInput); } // Return if mapper failed or did not produce output if (result.getMapResult().getExitCode() != 0 || result.getMapResult().getOutputFile() == null) return result; // Sort // While this seems (and is) inefficient for computers, this is //actually probably the shortest way to write this code since // vanilla Java does not provide an equivalent of sort -n. // If you want to write it in Java, use java.util.TreeSet. File intermediateFile = null; FileOutputStream intermediateOutput = null; try { // Create and open temporary file for sorted intermediate output intermediateFile = File.createTempFile("job-" + Long.toString(_id), "-intermediate", _tempDir); intermediateOutput = new FileOutputStream(intermediateFile); // Run the sort CommandLine sortCommand = new CommandLine("sort"); //sortCommand.addArgument("--field-separator=\t"); if (_numericSort) sortCommand.addArgument("-n"); sortCommand.addArgument(result.getMapResult().getOutputFile().getCanonicalPath(), false); DefaultExecutor exec = new DefaultExecutor(); ExecuteWatchdog dog = new ExecuteWatchdog(EXECUTABLE_TIMEOUT); PumpStreamHandler pump = new PumpStreamHandler(intermediateOutput); exec.setWatchdog(dog); exec.setStreamHandler(pump); try { exec.execute(sortCommand); } catch (ExecuteException ex) { throw new IOException("Sort process failed while running test jobs", ex); } } finally { IOUtils.closeQuietly(intermediateOutput); } // Reduce FileInputStream reduceInput = null; try { // Open the intermediate file for reading reduceInput = new FileInputStream(intermediateFile); // Run the reducer result.setReduceResult(runTransform(_id, _reducerFile, _packageDir, reduceInput)); } finally { IOUtils.closeQuietly(reduceInput); // Delete intermediate file intermediateFile.delete(); } return result; }
From source file:edu.cornell.med.icb.goby.modes.RunParallelMode.java
public void execute() throws IOException { final Slice slices[] = new Slice[numParts]; File file = new File(input); if (!(file.isFile() && file.exists() && file.canRead())) { System.err.println("Input file cannot be read: " + input); System.exit(1);//w w w. j a v a2 s . co m } int i = 0; for (final Slice slice : slices) { slices[i++] = new Slice(); } final long fileLength = file.length(); final long sliceLength = fileLength / numParts; long currentOffset = 0; for (final Slice slice : slices) { slice.startOffset = currentOffset; slice.endOffset = currentOffset + sliceLength; currentOffset = slice.endOffset; } final ObjectOpenHashSet<String> allOutputs = new ObjectOpenHashSet<String>(); final ObjectOpenHashSet<String> allFastq = new ObjectOpenHashSet<String>(); final DoInParallel loop = new DoInParallel(numParts) { IsDone done = new IsDone(); @Override public void action(final DoInParallel forDataAccess, final String inputBasename, final int loopIndex) { try { CompactToFastaMode ctfm = new CompactToFastaMode(); ctfm.setInputFilename(input); ctfm.setOutputFormat(CompactToFastaMode.OutputFormat.FASTQ); ctfm.setStartPosition(slices[loopIndex].startOffset); ctfm.setEndPosition(slices[loopIndex].endOffset); String s = FilenameUtils.getBaseName(FilenameUtils.removeExtension(input)) + "-" + Integer.toString(loopIndex); String fastqFilename = s + "-input.fq"; allFastq.add(fastqFilename); File tmp1 = new File(s + "-tmp"); tmp1.deleteOnExit(); File output = new File(s + "-out"); output.deleteOnExit(); ctfm.setOutputFilename(fastqFilename); LOG.info(String.format("Extracting FASTQ for slice [%d-%d] loopIndex=%d %n", slices[loopIndex].startOffset, slices[loopIndex].endOffset, loopIndex)); ctfm.execute(); if (loopIndex > 0) { while (!done.isDone()) { // wait a bit to give the first thread the time to load the database and establish shared memory pool // System.out.println("sleep 5 thread "+loopIndex); sleep(5); } System.out.println("Thread " + loopIndex + " can now start."); } final Map<String, String> replacements = new HashMap<String, String>(); final String outputFilename = output.getName(); replacements.put("%read.fastq%", fastqFilename); replacements.put("%tmp1%", tmp1.getName()); replacements.put("%output%", outputFilename); final String transformedCommand = transform(processPartCommand, replacements); final DefaultExecutor executor = new DefaultExecutor(); OutputStream logStream = null; try { logStream = new LoggingOutputStream(getClass(), Level.INFO, ""); executor.setStreamHandler( new PumpStreamHandler(new StreamSignal(done, "scanning", logStream))); final CommandLine parse = CommandLine.parse(transformedCommand, replacements); LOG.info("About to execute: " + parse); final int exitValue = executor.execute(parse); LOG.info("Exit value = " + exitValue); if (new File(outputFilename + ".header").exists()) { // found output alignment: System.out.println("found output file: " + outputFilename); allOutputs.add(outputFilename + ".header"); } else { System.out.println("Warning: did not find output alignment: " + outputFilename); } } finally { IOUtils.closeQuietly(logStream); // remove the fastq file new File(fastqFilename).delete(); } } catch (IOException e) { LOG.error("Error processing index " + loopIndex + ", " + inputBasename, e); } } }; String[] parts = new String[numParts]; for (int j = 0; j < numParts; j++) { parts[j] = Integer.toString(j); } try { loop.execute(true, parts); } catch (Exception e) { System.err.println("An error occurred executing a parallel command: "); e.printStackTrace(); } System.out.printf("Preparing to concatenate %d outputs..%n", allOutputs.size()); final ConcatenateAlignmentMode concat = new ConcatenateAlignmentMode(); concat.setInputFileNames(allOutputs.toArray(new String[allOutputs.size()])); concat.setOutputFilename(output); concat.setAdjustQueryIndices(false); concat.setAdjustSampleIndices(false); concat.execute(); }
From source file:eu.creatingfuture.propeller.blocklyprop.propeller.OpenSpin.java
/** * https://code.google.com/p/open-source-spin-compiler/wiki/CommandLine * * @param executable// w w w. j a v a2s . c om * @param sourceFile * @return */ protected boolean compile(String executable, File sourceFile) { try { File temporaryDestinationFile = File.createTempFile("blocklyapp", ".binary"); File libDirectory = new File(new File(System.getProperty("user.dir")), "/propeller-lib"); Map map = new HashMap(); map.put("sourceFile", sourceFile); map.put("destinationFile", temporaryDestinationFile); map.put("libDirectory", libDirectory); CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-o").addArgument("${destinationFile}"); cmdLine.addArgument("-L").addArgument("${libDirectory}"); cmdLine.addArgument("${sourceFile}"); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(new int[] { 0, 1 }); 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 { temporaryDestinationFile.delete(); output = outputStream.toString(); } // System.out.println("output: " + output); /* Scanner scanner = new Scanner(output); Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?"); Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$"); while (scanner.hasNextLine()) { String portLine = scanner.nextLine(); if (chipFoundPattern.matcher(portLine).matches()) { Matcher portMatch = pattern.matcher(portLine); if (portMatch.find()) { // String port = portMatch.group("comport"); } } } */ // System.out.println("output: " + output); // System.out.println("exitValue: " + exitValue); 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.OpenSpin.java
protected boolean compileForRam(String executable, File sourceFile, File destinationFile) { try {/*from w w w. j a va2s. c om*/ File libDirectory = new File(new File(System.getProperty("user.dir")), "/propeller-lib"); Map map = new HashMap(); map.put("sourceFile", sourceFile); map.put("destinationFile", destinationFile); map.put("libDirectory", libDirectory); CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-b"); cmdLine.addArgument("-o").addArgument("${destinationFile}"); cmdLine.addArgument("-L").addArgument("${libDirectory}"); cmdLine.addArgument("${sourceFile}"); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); // executor.setExitValues(new int[]{402, 101}); 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(); } // System.out.println("output: " + output); /* Scanner scanner = new Scanner(output); Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?"); Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$"); while (scanner.hasNextLine()) { String portLine = scanner.nextLine(); if (chipFoundPattern.matcher(portLine).matches()) { Matcher portMatch = pattern.matcher(portLine); if (portMatch.find()) { // String port = portMatch.group("comport"); } } } */ // System.out.println("output: " + output); // System.out.println("exitValue: " + exitValue); 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.OpenSpin.java
protected boolean compileForEeprom(String executable, File sourceFile, File destinationFile) { try {//from w ww .j a v a2 s . c o m File libDirectory = new File(new File(System.getProperty("user.dir")), "/propeller-lib"); Map map = new HashMap(); map.put("sourceFile", sourceFile); map.put("destinationFile", destinationFile); map.put("libDirectory", libDirectory); CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-e"); cmdLine.addArgument("-o").addArgument("${destinationFile}"); cmdLine.addArgument("-L").addArgument("${libDirectory}"); cmdLine.addArgument("${sourceFile}"); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); // executor.setExitValues(new int[]{402, 101}); 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(); } // System.out.println("output: " + output); /* Scanner scanner = new Scanner(output); Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?"); Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$"); while (scanner.hasNextLine()) { String portLine = scanner.nextLine(); if (chipFoundPattern.matcher(portLine).matches()) { Matcher portMatch = pattern.matcher(portLine); if (portMatch.find()) { // String port = portMatch.group("comport"); } } } */ // System.out.println("output: " + output); // System.out.println("exitValue: " + exitValue); success = true; return true; } catch (IOException ioe) { logger.log(Level.SEVERE, null, ioe); success = false; return false; } }
From source file:de.jsurf.http.HttpServerHandler.java
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { HttpRequest request = (HttpRequest) e.getMessage(); HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setContent(ChannelBuffers.copiedBuffer(buf.toString(), CharsetUtil.UTF_8)); response.setHeader(CONTENT_TYPE, "video/mpts"); /*response.setChunked(true); response.setHeader(Names.TRANSFER_ENCODING, Values.CHUNKED);*/ Channel c = e.getChannel();// www.j a va 2s .c om // create a media reader String inputStream = HttpServerConfiguration.getConfiguration().getChannelInput(request.getUri()); if (inputStream == null) { response = new DefaultHttpResponse(HTTP_1_1, NOT_FOUND); ChannelFuture future = c.write(response); future.addListener(ChannelFutureListener.CLOSE); return; } String path = new java.io.File(".").getCanonicalPath(); log.debug("Current execution path: " + path); String[] parameters = new String[] { "-loglevel", "error", "-i", inputStream, "-vcodec", "copy", "-acodec", "copy", "-vbsf", "h264_mp4toannexb", "-f", "mpegts", "pipe:1" }; CommandLine cmdLine = CommandLine.parse("ffmpeg.exe"); cmdLine.addArguments(parameters); DefaultExecutor executor = new DefaultExecutor(); final ExecuteWatchdog watchDog = new ExecuteWatchdog(86400000); // One day timeout executor.setWatchdog(watchDog); PipedInputStream pin = new PipedInputStream(); PipedOutputStream pout = new PipedOutputStream(pin); PumpStreamHandler streamHandler = new PumpStreamHandler(pout, System.err); executor.setStreamHandler(streamHandler); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.execute(cmdLine, resultHandler); c.write(response); InputStream in = new BufferedInputStream(pin); ChannelFuture future = c.write(new ChunkedStream(in)); future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { try { log.debug("operationComplete: closeChannel"); future.getChannel().close(); } catch (Exception e) { } log.debug("operationComplete: Destroy ffmpeg process"); watchDog.destroyProcess(); } }); }
From source file:eu.creatingfuture.propeller.webLoader.propeller.OpenSpin.java
/** * https://code.google.com/p/open-source-spin-compiler/wiki/CommandLine * * @param executable//from w w w. ja va 2 s. c om * @param sourceFile * @return */ protected boolean compile(String executable, File sourceFile) { try { File temporaryDestinationFile = File.createTempFile("blocklyapp", ".binary"); File libDirectory = new File(new File(System.getProperty("user.dir")), "/propeller-lib"); Map map = new HashMap(); map.put("sourceFile", sourceFile); map.put("destinationFile", temporaryDestinationFile); map.put("libDirectory", libDirectory); CommandLine cmdLine = new CommandLine(executable); cmdLine.addArgument("-o").addArgument("${destinationFile}"); cmdLine.addArgument("-L").addArgument("${libDirectory}"); cmdLine.addArgument("${sourceFile}"); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); // executor.setExitValues(new int[]{402, 101}); 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 { temporaryDestinationFile.delete(); output = outputStream.toString(); } // System.out.println("output: " + output); /* Scanner scanner = new Scanner(output); Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?"); Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$"); while (scanner.hasNextLine()) { String portLine = scanner.nextLine(); if (chipFoundPattern.matcher(portLine).matches()) { Matcher portMatch = pattern.matcher(portLine); if (portMatch.find()) { // String port = portMatch.group("comport"); } } } */ // System.out.println("output: " + output); // System.out.println("exitValue: " + exitValue); success = true; return true; } catch (IOException ioe) { logger.log(Level.SEVERE, null, ioe); success = false; return false; } }
From source file:edu.stolaf.cs.wmrserver.testjob.TestJobTask.java
protected TestJobResult.TransformResult runTransform(long id, File executable, File workingDir, InputStream input) throws IOException { // Create the result object TestJobResult.TransformResult result = new TestJobResult.TransformResult(); CountingOutputStream output = null;//from w w w . j a va 2 s . co m CountingOutputStream error = null; try { // Create and open temporary file for standard output File outputFile = File.createTempFile("job-" + Long.toString(_id), "-output", _tempDir); output = new CountingOutputStream(new FileOutputStream(outputFile)); // Create and open temporary file for standard error File errorFile = File.createTempFile("job-" + Long.toString(_id), "-error", _tempDir); error = new CountingOutputStream(new FileOutputStream(errorFile)); // If executable is relative, try to resolve in working directory // (This emulates the behavior of Streaming) if (!executable.isAbsolute()) { File resolvedExecutable = new File(workingDir, executable.toString()); if (resolvedExecutable.isFile()) { resolvedExecutable.setExecutable(true); executable = resolvedExecutable.getAbsoluteFile(); } } // Run the transform CommandLine command; if (_switchUserCommand == null) command = new CommandLine(executable); else { command = CommandLine.parse(_switchUserCommand); HashMap<String, String> substitutionMap = new HashMap<String, String>(); substitutionMap.put("cmd", executable.toString()); command.setSubstitutionMap(substitutionMap); } DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog dog = new ExecuteWatchdog(EXECUTABLE_TIMEOUT); PumpStreamHandler pump = new PumpStreamHandler(output, error, input); executor.setWorkingDirectory(workingDir); executor.setWatchdog(dog); executor.setStreamHandler(pump); executor.setExitValues(null); int exitCode = executor.execute(command); result.setExitCode(exitCode); // Check whether it produced any output if (output.getByteCount() == 0) { output.close(); outputFile.delete(); } else result.setOutputFile(outputFile); // Check whether it produced any error output if (error.getByteCount() == 0) { error.close(); errorFile.delete(); } else result.setErrorFile(errorFile); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(error); } return result; }
From source file:io.vertx.config.vault.utils.VaultProcess.java
public void runAndProcess(String command, Consumer<String> processor) { String cli = executable.getAbsolutePath() + " " + command; System.out.println(">> " + cli); CommandLine parse = CommandLine.parse(cli); DefaultExecutor executor = new DefaultExecutor(); PumpStreamHandler pump = new PumpStreamHandler(new VaultOutputStream().addExtractor(processor), System.err); ExecuteWatchdog watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(watchDog);//ww w . j av a 2s.c o m executor.setStreamHandler(pump); try { executor.execute(parse); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:io.rhiot.utils.process.ExecProcessManager.java
@Override public List<String> executeAndJoinOutput(String... command) { CommandLine cmdLine = CommandLine.parse(String.join(" ", command)); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0);/* w ww . j ava 2s .c o m*/ ExecResultHandler resultHandler = null; if (getTimeout() > 0) { ExecuteWatchdog watchdog = new ExecuteWatchdog(getTimeout()); executor.setWatchdog(watchdog); resultHandler = new ExecResultHandler(watchdog); } try { CollectingLogOutputStream outAndErr = new CollectingLogOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outAndErr)); if (resultHandler != null) { executor.execute(cmdLine, resultHandler); } else { executor.execute(cmdLine); } resultHandler.waitFor(); return outAndErr.getLines(); } catch (Exception e) { throw new RuntimeException(e); } }