List of usage examples for org.apache.commons.exec DefaultExecutor setStreamHandler
public void setStreamHandler(final ExecuteStreamHandler streamHandler)
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 w w w . ja v 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.alibaba.jstorm.utils.JStormUtils.java
/** * If it is backend, please set resultHandler, such as DefaultExecuteResultHandler If it is frontend, ByteArrayOutputStream.toString get the result * <p/>/*from ww w. j a v a 2s . c om*/ * This function don't care whether the command is successfully or not * * @param command * @param environment * @param workDir * @param resultHandler * @return * @throws IOException */ public static ByteArrayOutputStream launchProcess(String command, final Map environment, final String workDir, ExecuteResultHandler resultHandler) throws IOException { String[] cmdlist = command.split(" "); CommandLine cmd = new CommandLine(cmdlist[0]); for (String cmdItem : cmdlist) { if (StringUtils.isBlank(cmdItem) == false) { cmd.addArgument(cmdItem); } } DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); if (StringUtils.isBlank(workDir) == false) { executor.setWorkingDirectory(new File(workDir)); } ByteArrayOutputStream out = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(out, out); if (streamHandler != null) { executor.setStreamHandler(streamHandler); } try { if (resultHandler == null) { executor.execute(cmd, environment); } else { executor.execute(cmd, environment, resultHandler); } } catch (ExecuteException e) { // @@@@ // failed to run command } return out; }
From source file:io.vertx.config.vault.utils.VaultProcess.java
private void startServer() { String line = executable.getAbsolutePath() + " server -config=src/test/resources/config.json"; System.out.println(">> " + line); CommandLine parse = CommandLine.parse(line); DefaultExecutor executor = new DefaultExecutor(); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); AtomicBoolean ready = new AtomicBoolean(); PumpStreamHandler pump = new PumpStreamHandler(new VaultOutputStream().addExtractor(l -> { if (l.contains("Vault server started!")) { ready.set(true);//from w w w .j ava 2 s.c o m } }), System.err); watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(watchDog); executor.setStreamHandler(pump); try { executor.execute(parse, resultHandler); } catch (IOException e) { throw new RuntimeException(e); } await().untilAtomic(ready, is(true)); System.out.println("Vault Server ready - but not yet initialized"); }
From source file:io.vertx.config.vault.utils.VaultProcess.java
private void init() { String line = executable.getAbsolutePath() + " init -key-shares=1 -key-threshold=1 " + CA_CERT_ARG; System.out.println(">> " + line); CommandLine parse = CommandLine.parse(line); DefaultExecutor executor = new DefaultExecutor(); PumpStreamHandler pump = new PumpStreamHandler(new VaultOutputStream().addExtractor(l -> { if (l.contains("Unseal Key 1:")) { unseal = l.replace("Unseal Key 1: ", "").trim(); } else if (l.contains("Initial Root Token:")) { token = l.replace("Initial Root Token: ", "").trim(); }//from w ww . j av a2 s. com }), System.err); ExecuteWatchdog watchDog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); executor.setWatchdog(watchDog); executor.setStreamHandler(pump); try { executor.execute(parse); } catch (IOException e) { throw new RuntimeException(e); } // await().until(() -> token != null); // await().until(() -> unseal != null); System.out.println("Vault Server initialized (but sealed)"); System.out.println("Root token: " + token); System.out.println("Unseal key: " + unseal); }
From source file:Logi.GSeries.Service.LogiGSKService.java
public String execToString(String command) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CommandLine commandline = CommandLine.parse(command); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); exec.execute(commandline);/*from w w w . ja v a 2s .c o m*/ return (outputStream.toString()); }
From source file:edu.kit.dama.dataworkflow.impl.LocalExecutionHandler.java
/** * Execute the user application. This method will start a new process running * the prepared user application locally. The method will return as soon as * the application has terminated. An asnychronous monitoring task my check * whether the process is still running or not via {@link #getTaskStatus(edu.kit.dama.mdm.dataworkflow.DataWorkflowTask) * } This method will check the runningIndicator file '.RUNNING', which only * exists as long as the application is running. * * @param pTask The task whose application should be executed. * * @throws DataWorkflowProcessingException If either the startup or the * processing fails for any reason, or if the user application returns an exit * code != 0./*from w ww .j av a 2 s .c om*/ */ @Override public void startUserApplication(DataWorkflowTask pTask) throws DataWorkflowProcessingException { //simply start the process...monitoring will be connected later File runningIndicator = getRunningIndicator(pTask); FileOutputStream fout = null; FileOutputStream ferr = null; File executablePath; try { executablePath = DataWorkflowHelper.getTaskMainExecutable(pTask); File executionBasePath = DataWorkflowHelper.getExecutionBasePath(pTask); File workingDirectory = DataWorkflowHelper.getTaskWorkingDirectory(executionBasePath); File tempDirectory = DataWorkflowHelper.getTaskTempDirectory(executionBasePath); File inputDirectory = DataWorkflowHelper.getTaskInputDirectory(executionBasePath); File outputDirectory = DataWorkflowHelper.getTaskOutputDirectory(executionBasePath); if (!executablePath.canExecute()) { LOGGER.debug("Executable at location {} seems not to be executable. Taking care of this..."); if (executablePath.setExecutable(true)) { LOGGER.debug("Executable was successfully set to be executable."); } else { LOGGER.warn("Failed to set executable to be executable. Trying to continue."); } } String cmdLineString = executablePath.getAbsolutePath() + " " + pTask.getConfiguration().getApplicationArguments() + " " + pTask.getApplicationArguments(); LOGGER.debug("Building up command array from string '{}'", cmdLineString); CommandLine cmdLine = CommandLine.parse(cmdLineString); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); Map<String, String> env = new HashMap<>(); env.put("WORKING_DIR", workingDirectory.getAbsolutePath()); env.put("TEMP_DIR", tempDirectory.getAbsolutePath()); env.put("INPUT_DIR", inputDirectory.getAbsolutePath()); env.put("OUTPUT_DIR", outputDirectory.getAbsolutePath()); fout = new FileOutputStream(new File(tempDirectory, "stdout.log")); ferr = new FileOutputStream(new File(tempDirectory, "stderr.log")); LOGGER.debug("Setting stream handler for stdout and stderr."); executor.setStreamHandler(new PumpStreamHandler(fout, ferr)); LOGGER.debug("Creating .RUNNING file for monitoring."); FileUtils.touch(runningIndicator); LOGGER.debug("Executing process."); int exitCode = executor.execute(cmdLine); if (exitCode != 0) { throw new DataWorkflowProcessingException( "Execution returned exit code " + exitCode + ". See logfiles for details."); } else { LOGGER.debug("Process successfully finished with exit code {}", exitCode); } } catch (IOException | UnsupportedOperatingSystemException e) { throw new DataWorkflowProcessingException("Failed to start executable for task " + pTask.getId(), e); } finally { LOGGER.debug("Removing running indicator file {}", runningIndicator); FileUtils.deleteQuietly(runningIndicator); if (fout != null) { try { fout.close(); } catch (IOException ex) { } } if (ferr != null) { try { ferr.close(); } catch (IOException ex) { } } } }
From source file:info.pancancer.arch3.worker.WorkflowRunner.java
@Override public WorkflowResult call() throws IOException { PumpStreamHandler streamHandler = new PumpStreamHandler(this.outputStream, this.errorStream); WorkflowResult result = new WorkflowResult(); DefaultExecutor executor = new DefaultExecutor(); /*/* ww w.j a v a2 s . c om*/ * CommandLine cli = new CommandLine("docker"); cli.addArguments(new String[] { "run", "--rm", "-h", "master", "-t" ,"-v", * "/var/run/docker.sock:/var/run/docker.sock", "-v", job.getWorkflowPath() + ":/workflow", "-v",pathToINI + ":/ini", "-v", * "/datastore:/datastore", "-v","/home/"+this.userName+"/.ssh/gnos.pem:/home/ubuntu/.ssh/gnos.pem", * "seqware/seqware_whitestar_pancancer", "seqware", "bundle", "launch", "--dir", "/workflow", "--ini", "/ini", "--no-metadata" }); */ LOG.info("Executing command: " + this.cli.toString().replace(",", "")); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.setStreamHandler(streamHandler); try { if (this.preworkDelay > 0) { LOG.info("Sleeping before executing workflow for " + this.preworkDelay + " ms."); Thread.sleep(this.preworkDelay); } executor.execute(cli, resultHandler); // Use the result handler for non-blocking call, so this way we should be able to get updates of // stdout and stderr while the command is running. resultHandler.waitFor(); result.setWorkflowStdout(outputStream.getAllLinesAsString()); result.setWorkflowStdErr(errorStream.getAllLinesAsString()); LOG.debug("Exit code: " + resultHandler.getExitValue()); result.setExitCode(resultHandler.getExitValue()); if (this.postworkDelay > 0) { LOG.info("Sleeping after executing workflow for " + this.postworkDelay + " ms."); Thread.sleep(this.postworkDelay); } } catch (ExecuteException e) { LOG.error(e.getMessage(), e); result.setWorkflowStdout(this.getStdErr()); } catch (InterruptedException | IOException e) { LOG.error(e.getMessage(), e); result.setWorkflowStdout(this.getStdErr()); } finally { this.outputStream.close(); this.errorStream.close(); } LOG.debug("Workflowrunner exiting"); return result; }
From source file:it.drwolf.ridire.utility.RIDIREReTagger.java
public String retagFile(File f) throws ExecuteException, IOException { // Map<String, File> map = new HashMap<String, File>(); String fileIN = f.getAbsolutePath(); String fileOut = f.getAbsolutePath() + ".iso"; String posOld = f.getAbsolutePath() + ".iso.pos"; String posNew = f.getAbsolutePath() + ".pos"; // first convert from utf8 to iso8859-1 CommandLine commandLine = CommandLine.parse("iconv"); commandLine.addArgument("-c").addArgument("-s").addArgument("-f").addArgument("utf8").addArgument("-t") .addArgument("iso8859-1//TRANSLIT").addArgument("-o").addArgument(fileOut, false) .addArgument(fileIN, false); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(RIDIREReTagger.TREETAGGER_TIMEOUT); executor.setWatchdog(watchdog);/*from w w w. j av a 2s . c o m*/ int exitValue = executor.execute(commandLine); if (exitValue == 0) { // tag using latin1 and Baroni's tagset commandLine = CommandLine.parse(this.treeTaggerBin); commandLine.addArgument(fileOut, false); executor = new DefaultExecutor(); executor.setExitValue(0); watchdog = new ExecuteWatchdog(RIDIREReTagger.TREETAGGER_TIMEOUT); executor.setWatchdog(watchdog); TreeTaggerLog treeTaggerLog = new TreeTaggerLog(); PumpStreamHandler executeStreamHandler = new PumpStreamHandler(treeTaggerLog, null); executor.setStreamHandler(executeStreamHandler); int exitValue2 = executor.execute(commandLine); if (exitValue2 == 0) { // FileUtils.deleteQuietly(new File(fileOut)); File posTagFile = new File(posOld); FileUtils.writeLines(posTagFile, treeTaggerLog.getLines()); } // reconvert to utf8 commandLine = CommandLine.parse("iconv"); commandLine.addArgument("-s").addArgument("-f").addArgument("iso8859-1").addArgument("-t") .addArgument("utf8//TRANSLIT").addArgument("-o").addArgument(posNew, false) .addArgument(posOld, false); executor = new DefaultExecutor(); watchdog = new ExecuteWatchdog(RIDIREReTagger.TREETAGGER_TIMEOUT); executor.setWatchdog(watchdog); int exitValue3 = executor.execute(commandLine); if (exitValue3 == 0) { // FileUtils.deleteQuietly(new File(f.getPath() + ".iso.pos")); return new File(posNew).getCanonicalPath(); } } return null; }
From source file:io.takari.maven.testing.executor.ForkedLauncher.java
public int run(String[] cliArgs, Map<String, String> envVars, File multiModuleProjectDirectory, File workingDirectory, File logFile) throws IOException, LauncherException { String javaHome;// w w w .j a va2 s . c o m if (envVars == null || envVars.get("JAVA_HOME") == null) { javaHome = System.getProperty("java.home"); } else { javaHome = envVars.get("JAVA_HOME"); } File executable = new File(javaHome, Os.isFamily(Os.FAMILY_WINDOWS) ? "bin/javaw.exe" : "bin/java"); CommandLine cli = new CommandLine(executable); cli.addArgument("-classpath").addArgument(classworldsJar.getAbsolutePath()); cli.addArgument("-Dclassworlds.conf=" + new File(mavenHome, "bin/m2.conf").getAbsolutePath()); cli.addArgument("-Dmaven.home=" + mavenHome.getAbsolutePath()); cli.addArgument("-Dmaven.multiModuleProjectDirectory=" + multiModuleProjectDirectory.getAbsolutePath()); cli.addArgument("org.codehaus.plexus.classworlds.launcher.Launcher"); cli.addArguments(args.toArray(new String[args.size()])); if (extensions != null && !extensions.isEmpty()) { cli.addArgument("-Dmaven.ext.class.path=" + toPath(extensions)); } cli.addArguments(cliArgs); Map<String, String> env = new HashMap<>(); if (mavenHome != null) { env.put("M2_HOME", mavenHome.getAbsolutePath()); } if (envVars != null) { env.putAll(envVars); } if (envVars == null || envVars.get("JAVA_HOME") == null) { env.put("JAVA_HOME", System.getProperty("java.home")); } DefaultExecutor executor = new DefaultExecutor(); executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); executor.setWorkingDirectory(workingDirectory.getAbsoluteFile()); try (OutputStream log = new FileOutputStream(logFile)) { PrintStream out = new PrintStream(log); out.format("Maven Executor implementation: %s\n", getClass().getName()); out.format("Maven home: %s\n", mavenHome); out.format("Build work directory: %s\n", workingDirectory); out.format("Environment: %s\n", env); out.format("Command line: %s\n\n", cli.toString()); out.flush(); PumpStreamHandler streamHandler = new PumpStreamHandler(log); executor.setStreamHandler(streamHandler); return executor.execute(cli, env); // this throws ExecuteException if process return code != 0 } catch (ExecuteException e) { throw new LauncherException("Failed to run Maven: " + e.getMessage() + "\n" + cli, e); } }
From source file:it.drwolf.ridire.index.cwb.CWBFrequencyList.java
private String getFrequencyList(boolean deleteFLFile, List<String> semDescription, List<String> funDescription, int quantityP, String type, Integer threshold, boolean sorted) { CommandLine commandLine = CommandLine.parse(this.cwbscanExecutable); commandLine.addArgument("-q"); if (threshold != null && threshold > 0) { commandLine.addArgument("-f"); commandLine.addArgument(threshold + ""); }/* www . ja v a 2 s . c om*/ commandLine.addArgument("-r").addArgument(this.cqpRegistry); commandLine.addArgument("-C"); commandLine.addArgument(this.cqpCorpusName); if (type.equals("forma")) { commandLine.addArgument("word+0"); } else if (type.equals("PoS")) { commandLine.addArgument("pos+0"); } else if (type.equals("easypos")) { commandLine.addArgument("easypos+0"); } else if (type.equals("lemma")) { commandLine.addArgument("lemma+0"); } else if (type.equals("PoS-forma")) { commandLine.addArgument("pos+0"); commandLine.addArgument("word+0"); } else if (type.equals("PoS-lemma")) { commandLine.addArgument("pos+0"); commandLine.addArgument("lemma+0"); } String semFuncParam = ""; if (funDescription != null && funDescription.size() > 0 && funDescription.get(0) != null && funDescription.get(0).trim().length() > 0 || semDescription != null && semDescription.size() > 0 && semDescription.get(0) != null && semDescription.get(0).trim().length() > 0) { semFuncParam = "?"; if (funDescription != null && funDescription.size() > 0 && funDescription.get(0) != null && funDescription.get(0).trim().length() > 0) { String fd = StringUtils.join(funDescription, "\\|"); semFuncParam += "text_functional=/\\(" + fd + "\\)/ "; } if (semDescription != null && semDescription.size() > 0 && semDescription.get(0) != null && semDescription.get(0).trim().length() > 0) { String sd = StringUtils.join(semDescription, "\\|"); semFuncParam += "text_semantic=/\\(" + sd + "\\)/ "; } commandLine.addArgument(semFuncParam); } if (sorted) { commandLine.addArgument("|"); commandLine.addArgument("sort"); commandLine.addArgument("-nr"); commandLine.addArgument("-k"); commandLine.addArgument("1"); } if (quantityP > 0) { commandLine.addArgument("|"); commandLine.addArgument("head"); commandLine.addArgument("-" + quantityP); } File flTempFile = null; try { flTempFile = File.createTempFile("ridireFL", null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } commandLine.addArgument(" > "); commandLine.addArgument(flTempFile.getAbsolutePath()); String c = commandLine.toString(); try { File tempSh = File.createTempFile("ridireSH", ".sh"); FileUtils.writeStringToFile(tempSh, c); tempSh.setExecutable(true); commandLine = CommandLine.parse(tempSh.getAbsolutePath()); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(CWBFrequencyList.TIMEOUT); executor.setWatchdog(watchdog); ByteArrayOutputStream baosStdOut = new ByteArrayOutputStream(1024); ByteArrayOutputStream baosStdErr = new ByteArrayOutputStream(1024); ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(baosStdOut, baosStdErr, null); executor.setStreamHandler(executeStreamHandler); int exitValue = 0; exitValue = executor.execute(commandLine); FileUtils.deleteQuietly(tempSh); if (exitValue == 0) { StrTokenizer strTokenizer = new StrTokenizer(); this.frequencyList = new ArrayList<FrequencyItem>(); List<String> lines = FileUtils.readLines(flTempFile); for (String line : lines) { strTokenizer.reset(line); String[] tokens = strTokenizer.getTokenArray(); if (tokens.length == 2) { FrequencyItem frequencyItem = new FrequencyItem(tokens[1], Integer.parseInt(tokens[0].trim())); this.frequencyList.add(frequencyItem); } else if (tokens.length == 3) { FrequencyItem frequencyItem = new FrequencyItem(tokens[2], tokens[1], Integer.parseInt(tokens[0].trim())); this.frequencyList.add(frequencyItem); } } if (deleteFLFile) { FileUtils.deleteQuietly(flTempFile); } } } catch (ExecuteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flTempFile.getAbsolutePath(); }