List of usage examples for org.apache.commons.exec DefaultExecutor setExitValues
public void setExitValues(final int[] values)
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 ww .ja va 2 s . c o 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:edu.stolaf.cs.wmrserver.TransformProcessor.java
private File compile(SubnodeConfiguration languageConf, String transformTypeString, File srcTransformFile, File jobTransformDir) throws CompilationException, IOException { // Determine correct compiler, returning if none specified String compiler = languageConf.getString("compiler-" + transformTypeString, ""); if (compiler.isEmpty()) compiler = languageConf.getString("compiler", ""); if (compiler.isEmpty()) return srcTransformFile; // Determine destination filename File compiledTransformFile = new File(jobTransformDir, "compiled-job-" + transformTypeString); // Create map to replace ${wmr:...} variables. // NOTE: Commons Configuration's built-in interpolator does not work here // for some reason. File jobTempDir = getJobTempDir(); Hashtable<String, String> variableMap = new Hashtable<String, String>(); File libDir = getLibraryDirectory(languageConf); variableMap.put("wmr:lib.dir", (libDir != null) ? libDir.toString() : ""); variableMap.put("wmr:src.dir", relativizeFile(srcTransformFile.getParentFile(), jobTempDir).toString()); variableMap.put("wmr:src.file", relativizeFile(srcTransformFile, jobTempDir).toString()); variableMap.put("wmr:dest.dir", relativizeFile(jobTransformDir, jobTempDir).toString()); variableMap.put("wmr:dest.file", relativizeFile(compiledTransformFile, jobTempDir).toString()); // Replace variables in compiler string compiler = StrSubstitutor.replace(compiler, variableMap); // Run the compiler CommandLine compilerCommand = CommandLine.parse(compiler); DefaultExecutor exec = new DefaultExecutor(); ExecuteWatchdog dog = new ExecuteWatchdog(60000); // 1 minute ByteArrayOutputStream output = new ByteArrayOutputStream(); PumpStreamHandler pump = new PumpStreamHandler(output); exec.setWorkingDirectory(jobTempDir); exec.setWatchdog(dog);//from ww w. j av a 2 s .c o m exec.setStreamHandler(pump); exec.setExitValues(null); // Can't get the exit code if it throws exception int exitStatus = -1; try { exitStatus = exec.execute(compilerCommand); } catch (IOException ex) { // NOTE: Exit status is still -1 in this case, since exception was thrown throw new CompilationException("Compiling failed for " + transformTypeString, exitStatus, new String(output.toByteArray())); } // Check for successful exit if (exitStatus != 0) throw new CompilationException("Compiling failed for " + transformTypeString, exitStatus, new String(output.toByteArray())); // Check that output exists and is readable, and make it executable if (!compiledTransformFile.isFile()) throw new CompilationException( "Compiler did not output a " + transformTypeString + " executable (or it was not a regular file).", exitStatus, new String(output.toByteArray())); if (!compiledTransformFile.canRead()) throw new IOException(StringUtils.capitalize(transformTypeString) + " executable output from compiler was not readable: " + compiledTransformFile.toString()); if (!compiledTransformFile.canExecute()) compiledTransformFile.setExecutable(true, false); return compiledTransformFile; }
From source file:com.github.seqware.queryengine.tutorial.PosterSGE.java
/** * <p>benchmark.</p>//w w w . j ava 2 s. co m * * @throws java.io.IOException if any. */ public void benchmark() throws IOException { if (args.length != 3) { System.err.println(args.length + " arguments found"); System.out.println( PosterSGE.class.getSimpleName() + " <outputKeyValueFile> <input file dir> <simultaneous jobs>"); System.exit(-1); } File outputFile = Utility.checkOutput(args[0]); // check if reference has been properly created Reference reference = SWQEFactory.getQueryInterface().getLatestAtomByRowKey("hg_19", Reference.class); if (reference == null) { SGID refID = ReferenceCreator.mainMethod(new String[] { HG_19 }); reference = SWQEFactory.getQueryInterface().getAtomBySGID(Reference.class, refID); } // record reference, starting disk space keyValues.put("referenceID", reference.getSGID().getRowKey()); recordSpace("start"); Utility.writeKeyValueFile(outputFile, keyValues); // create new FeatureSet id and pass it onto our children CreateUpdateManager manager = SWQEFactory.getModelManager(); FeatureSet initialFeatureSet = manager.buildFeatureSet().setReference(reference).build(); manager.flush(); int count = 0; // go through all input files File fileDirectory = new File(args[1]); File[] listFiles = fileDirectory.listFiles(); // record start and finish time Date startDate = new Date(); keyValues.put(count + "-start-date-long", Long.toString(startDate.getTime())); keyValues.put(count + "-start-date-human", startDate.toString()); // submit all jobs in parallel via SGE StringBuilder jobNames = new StringBuilder(); for (File inputFile : listFiles) { // run without unnecessary parameters String cargs = "-w VCFVariantImportWorker -i " + inputFile.getAbsolutePath() + " -b " + String.valueOf(BENCHMARKING_BATCH_SIZE) + " -f " + initialFeatureSet.getSGID().getRowKey() + " -r " + reference.getSGID().getRowKey(); String command = "java -Xmx2048m -classpath " + System.getProperty("user.dir") + "/seqware-queryengine-0.12.0-full.jar com.github.seqware.queryengine.system.importers.SOFeatureImporter"; command = command + " " + cargs; command = "qsub -q long -l h_vmem=3G -cwd -N dyuen-" + inputFile.getName() + " -b y " + command; jobNames.append("dyuen-").append(inputFile.getName()).append(","); System.out.println("Running: " + command); CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); int exitValue = executor.execute(cmdLine); } String jobs = jobNames.toString().substring(0, jobNames.length() - 1); // submit a job that just waits on all the preceding jobs for synchronization String command = "java -Xmx1024m -version"; command = "qsub -cwd -N dyuen-wait -hold_jid " + jobs + " -b y -sync y " + command; System.out.println("Running wait: " + command); CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null); int exitValue = executor.execute(cmdLine); FeatureSet fSet = SWQEFactory.getQueryInterface().getLatestAtomBySGID(initialFeatureSet.getSGID(), FeatureSet.class); keyValues.put(count + "-featuresSet-id", fSet.getSGID().getRowKey()); keyValues.put(count + "-featuresSet-id-timestamp", Long.toString(fSet.getSGID().getBackendTimestamp().getTime())); // // runs count query, touches everything but does not write // // keyValues.put(count + "-start-count-date-long", Long.toString(System.currentTimeMillis())); // long fsetcount = fSet.getCount(); // keyValues.put(count + "-features-loaded", Long.toString(fsetcount)); // keyValues.put(count + "-end-count-date-long", Long.toString(System.currentTimeMillis())); Date endDate = new Date(); keyValues.put(count + "-end-date-long", Long.toString(endDate.getTime())); keyValues.put(count + "-end-date-human", endDate.toString()); recordSpace(String.valueOf(count)); Utility.writeKeyValueFile(outputFile, keyValues); count++; }
From source file:com.github.zeroxff.executor.ExtendedExecutor.java
public ExecuteResult execute() throws ExtendedExecuteException { this.clearOut(); if (this.commandLine == null) { throw new ExtendedExecuteException("CommandLine cannot be null", Executor.INVALID_EXITVALUE); }//from w w w .j a v a 2s. c o m if (this.commandLine.length == 0) { throw new ExtendedExecuteException("CommandLine cannot be empty", Executor.INVALID_EXITVALUE); } if (this.maxExecutiontime != ExecuteWatchdog.INFINITE_TIMEOUT && this.maxExecutiontime < 1) { throw new ExtendedExecuteException("Max execution time must not be less than 1", Executor.INVALID_EXITVALUE); } try { // load the command line as an array of strings CommandLine cmdLine = new CommandLine(this.commandLine[0]); for (int counter = 1; counter < commandLine.length; counter++) { cmdLine.addArgument(this.commandLine[counter], quoteCommandlineArgs); } // load the substitution map, if defined if (this.substitutionMap != null) { cmdLine.setSubstitutionMap(this.substitutionMap); } // load the watchdog timer, it can be set to infinite time ExecuteWatchdog watchdog = new ExecuteWatchdog(this.maxExecutiontime); ExtendedResultHandler resultHandler = new ExtendedResultHandler(watchdog); // inizialize outputstream processors. OutStreamProcessor outLinee = null; OutStreamProcessor errLinee = null; PumpStreamHandler streamHandler = null; if (outputFilter != null && outputFilter.size() > 0) { outLinee = new OutStreamProcessor(outputFilter); } else { outLinee = new OutStreamProcessor(); } if (this.enableAllLinesOut) { outLinee.enableAllLines(); } if (mergeOutStreams) { // Using Std out for the output/error stream streamHandler = new PumpStreamHandler(outLinee); } else { if (errorFilter != null && errorFilter.size() > 0) { errLinee = new OutStreamProcessor(errorFilter); } else { errLinee = new OutStreamProcessor(); } if (enableAllLinesErr) { errLinee.enableAllLines(); } // Using Std out for the output/error stream streamHandler = new PumpStreamHandler(outLinee, errLinee); } DefaultExecutor executor = new DefaultExecutor(); // set the working directory... // if the working directory doesn't exists, it can crash the // executor. if (workingDirecory != null) { executor.setWorkingDirectory(workingDirecory); } // set the accepted exit values for the command line // default is '0'. if (okExitValues != null && okExitValues.length > 0) { executor.setExitValues(okExitValues); } executor.setWatchdog(watchdog); executor.setStreamHandler(streamHandler); try { executor.execute(cmdLine, resultHandler); resultHandler.waitFor(); returnCode = resultHandler.getExitValue(); exitMode = resultHandler.getExitMode(); switch (exitMode) { case ERROR_IN_EXECUTION: this.message = resultHandler.getException().getMessage(); break; default: break; } } catch (ExecuteException e) { exitMode = ExecuteResult.EXCEPTION; exception = e; this.message = e.getMessage(); } catch (IOException e) { exitMode = ExecuteResult.EXCEPTION; exception = e; this.message = e.getMessage(); } catch (InterruptedException e) { exitMode = ExecuteResult.EXCEPTION; exception = e; this.message = e.getMessage(); } // if (outLinee != null) { outputLines = outLinee.getLines(); allOutputLines = outLinee.getAllLines(); } if (errLinee != null) { errorLines = errLinee.getLines(); allErrorLines = errLinee.getAllLines(); } this.closeStreams(outLinee, errLinee); } catch (Exception e) { throw new ExtendedExecuteException(e.getMessage(), Executor.INVALID_EXITVALUE, e); } return exitMode; }
From source file:cc.arduino.Compiler.java
private void exec(String[] command) throws RunnerException { // eliminate any empty array entries List<String> stringList = new ArrayList<>(); for (String string : command) { string = string.trim();//w w w.j a v a 2 s. c om if (string.length() != 0) stringList.add(string); } command = stringList.toArray(new String[stringList.size()]); if (command.length == 0) return; if (verbose) { for (String c : command) System.out.print(c + " "); System.out.println(); } DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(new PumpStreamHandler() { @Override protected Thread createPump(InputStream is, OutputStream os, boolean closeWhenExhausted) { final Thread result = new Thread(new MyStreamPumper(is, Compiler.this)); result.setName("MyStreamPumper Thread"); result.setDaemon(true); return result; } }); CommandLine commandLine = new DoubleQuotedArgumentsOnWindowsCommandLine(command[0]); for (int i = 1; i < command.length; i++) { commandLine.addArgument(command[i], false); } int result; executor.setExitValues(null); try { result = executor.execute(commandLine); } catch (IOException e) { RunnerException re = new RunnerException(e.getMessage()); re.hideStackTrace(); throw re; } executor.setExitValues(new int[0]); // an error was queued up by message(), barf this back to compile(), // which will barf it back to Editor. if you're having trouble // discerning the imagery, consider how cows regurgitate their food // to digest it, and the fact that they have five stomaches. // //System.out.println("throwing up " + exception); if (exception != null) throw exception; if (result > 1) { // a failure in the tool (e.g. unable to locate a sub-executable) System.err.println(I18n.format(tr("{0} returned {1}"), command[0], result)); } if (result != 0) { RunnerException re = new RunnerException(tr("Error compiling.")); re.hideStackTrace(); throw re; } }
From source file:org.apache.camel.component.exec.impl.DefaultExecCommandExecutor.java
protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null); if (execCommand.getWorkingDir() != null) { executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile()); }/*w w w. j av a 2 s .c o m*/ if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) { executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout())); } executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); return executor; }
From source file:org.apache.hcatalog.templeton.ExecServiceImpl.java
private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null); // Setup stdout and stderr int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1); ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes); ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes); executor.setStreamHandler(new PumpStreamHandler(outStream, errStream)); // Only run for N milliseconds int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog);//from ww w . j av a 2s.c o m CommandLine cmd = makeCommandLine(program, args); LOG.info("Running: " + cmd); ExecBean res = new ExecBean(); res.exitcode = executor.execute(cmd, execEnv(env)); String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME); res.stdout = outStream.toString(enc); res.stderr = errStream.toString(enc); return res; }
From source file:org.apache.hive.hcatalog.templeton.StreamOutputWriter.java
private ExecBean auxRun(String program, List<String> args, Map<String, String> env) throws NotAuthorizedException, ExecuteException, IOException { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null); // Setup stdout and stderr int nbytes = appConf.getInt(AppConfig.EXEC_MAX_BYTES_NAME, -1); ByteArrayOutputStream outStream = new MaxByteArrayOutputStream(nbytes); ByteArrayOutputStream errStream = new MaxByteArrayOutputStream(nbytes); executor.setStreamHandler(new PumpStreamHandler(outStream, errStream)); // Only run for N milliseconds int timeout = appConf.getInt(AppConfig.EXEC_TIMEOUT_NAME, 0); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog);// w ww . j a v a2 s .com CommandLine cmd = makeCommandLine(program, args); LOG.info("Running: " + cmd); ExecBean res = new ExecBean(); res.exitcode = executor.execute(cmd, execEnv(env)); String enc = appConf.get(AppConfig.EXEC_ENCODING_NAME); res.stdout = outStream.toString(enc); res.stderr = errStream.toString(enc); try { watchdog.checkException(); } catch (Exception ex) { LOG.error("Command: " + cmd + " failed. res=" + res, ex); } if (watchdog.killedProcess()) { String msg = " was terminated due to timeout(" + timeout + "ms). See " + AppConfig.EXEC_TIMEOUT_NAME + " property"; LOG.warn("Command: " + cmd + msg + " res=" + res); res.stderr += " Command " + msg; } if (res.exitcode != 0) { LOG.info("Command: " + cmd + " failed. res=" + res); } return res; }
From source file:org.jberet.support.io.OsCommandBatchlet.java
/** * {@inheritDoc}//from ww w. j a v a 2 s .c o m * <p> * This method runs the OS command. * If the command completes successfully, its process exit code is returned. * If there is exception while running the OS command, which may be * caused by timeout, the command being stopped, or other errors, the process * exit code is set as the step exit status, and the exception is thrown. * * @return the OS command process exit code * * @throws Exception upon errors */ @Override public String process() throws Exception { final DefaultExecutor executor = new DefaultExecutor(); final CommandLine commandLineObj; if (commandLine != null) { commandLineObj = CommandLine.parse(commandLine); } else { if (commandArray == null) { throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, null, "commandArray"); } else if (commandArray.isEmpty()) { throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, commandArray.toString(), "commandArray"); } commandLineObj = new CommandLine(commandArray.get(0)); final int len = commandArray.size(); if (len > 1) { for (int i = 1; i < len; i++) { commandLineObj.addArgument(commandArray.get(i)); } } } if (workingDir != null) { executor.setWorkingDirectory(workingDir); } if (streamHandler != null) { executor.setStreamHandler((ExecuteStreamHandler) streamHandler.newInstance()); } SupportLogger.LOGGER.runCommand(commandLineObj.getExecutable(), Arrays.toString(commandLineObj.getArguments()), executor.getWorkingDirectory().getAbsolutePath()); if (commandOkExitValues != null) { executor.setExitValues(commandOkExitValues); } watchdog = new ExecuteWatchdog( timeoutSeconds > 0 ? timeoutSeconds * 1000 : ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(watchdog); executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.execute(commandLineObj, environment, resultHandler); resultHandler.waitFor(); final ExecuteException exception = resultHandler.getException(); if (exception != null) { stepContext.setExitStatus(String.valueOf(resultHandler.getExitValue())); if (!isStopped) { throw exception; } else { SupportLogger.LOGGER.warn("", exception); } } return String.valueOf(resultHandler.getExitValue()); }
From source file:org.ms123.common.management.ManagementServiceImpl.java
private int exec(String line, ByteArrayOutputStream outputStream, ByteArrayOutputStream outputErr, int[] exitValues) throws Exception { CommandLine cmdLine = CommandLine.parse(line); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(exitValues); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, outputErr); executor.setStreamHandler(streamHandler); int exitValue = executor.execute(cmdLine); return exitValue; }