List of usage examples for org.apache.commons.exec DefaultExecutor setWatchdog
public void setWatchdog(final ExecuteWatchdog watchDog)
From source file:fr.gouv.culture.vitam.utils.Executor.java
/** * Execute an external command/* w ww. j ava2 s . co m*/ * @param cmd * @param tempDelay * @param correctValues * @param showOutput * @param realCommand * @return correctValues if ok, < 0 if an execution error occurs, or other error values */ public static int exec(List<String> cmd, long tempDelay, int[] correctValues, boolean showOutput, String realCommand) { // Create command with parameters CommandLine commandLine = new CommandLine(cmd.get(0)); for (int i = 1; i < cmd.size(); i++) { commandLine.addArgument(cmd.get(i)); } DefaultExecutor defaultExecutor = new DefaultExecutor(); ByteArrayOutputStream outputStream; outputStream = new ByteArrayOutputStream(); PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream); defaultExecutor.setStreamHandler(pumpStreamHandler); defaultExecutor.setExitValues(correctValues); AtomicBoolean isFinished = new AtomicBoolean(false); ExecuteWatchdog watchdog = null; Timer timer = null; if (tempDelay > 0) { // If delay (max time), then setup Watchdog timer = new Timer(true); watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT); defaultExecutor.setWatchdog(watchdog); CheckEndOfExecute endOfExecute = new CheckEndOfExecute(isFinished, watchdog, realCommand); timer.schedule(endOfExecute, tempDelay); } int status = -1; try { // Execute the command status = defaultExecutor.execute(commandLine); } catch (ExecuteException e) { if (e.getExitValue() == -559038737) { // Cannot run immediately so retry once try { Thread.sleep(100); } catch (InterruptedException e1) { } try { status = defaultExecutor.execute(commandLine); } catch (ExecuteException e1) { pumpStreamHandler.stop(); System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString()); status = -2; try { outputStream.close(); } catch (IOException e2) { } return status; } catch (IOException e1) { pumpStreamHandler.stop(); System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString()); status = -2; try { outputStream.close(); } catch (IOException e2) { } return status; } } else { pumpStreamHandler.stop(); System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString()); status = -2; try { outputStream.close(); } catch (IOException e2) { } return status; } } catch (IOException e) { pumpStreamHandler.stop(); System.err.println(StaticValues.LBL.error_error.get() + "Exception: " + e.getMessage() + " Exec in error with " + commandLine.toString() + "\n\t" + outputStream.toString()); status = -2; try { outputStream.close(); } catch (IOException e2) { } return status; } finally { isFinished.set(true); if (timer != null) { timer.cancel(); } try { Thread.sleep(200); } catch (InterruptedException e1) { } } pumpStreamHandler.stop(); if (defaultExecutor.isFailure(status) && watchdog != null) { if (watchdog.killedProcess()) { // kill by the watchdoc (time out) if (showOutput) { System.err.println(StaticValues.LBL.error_error.get() + "Exec is in Time Out"); } } status = -3; try { outputStream.close(); } catch (IOException e2) { } } else { if (showOutput) { System.out.println("Exec: " + outputStream.toString()); } try { outputStream.close(); } catch (IOException e2) { } } return status; }
From source file:com.sonar.scanner.api.it.tools.CommandExecutor.java
public int execute(String[] args, Map<String, String> env, @Nullable Path workingDir) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }/*from w w w.j a v a 2 s .c om*/ watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args, false); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); return exec.execute(cmd, env); }
From source file:it.sonarlint.cli.tools.CommandExecutor.java
public int execute(String[] args, @Nullable Path workingDir, Map<String, String> addEnv) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }/* w ww . java 2s . c o m*/ ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); Map<String, String> env = new HashMap<>(System.getenv()); env.putAll(addEnv); return exec.execute(cmd, env); }
From source file:its.tools.CommandExecutor.java
public void execute(String[] args, @Nullable Path workingDir) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }/* w ww . jav a 2 s . com*/ watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); exec.execute(cmd, new ResultHander()); }
From source file:io.github.binout.wordpress2html.writer.Html2AsciidocConverter.java
private void execute(CommandLine cmdLine) throws IOException { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0);/*from www . ja v a 2s.c o m*/ ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); int exitValue = executor.execute(cmdLine); if (exitValue != 0) { throw new RuntimeException("Pandoc is not installed !"); } }
From source file:it.unisi.bdm.crawler.PhantomjsBrowser.java
@Override /**// w ww . j av a 2s . c o m * Invokes PhantomJS in order to download the page associated to `url`. * Please note that since the browser sometimes hangs, it's execution is * monitored by a wathdog that kills it after this.timeout ms. * * @param url * @return Page * @throws BrowserTimeoutException If PhantomJS hangs. */ public Page getPage(String url) throws BrowserTimeoutException { ByteArrayOutputStream stdout = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdout); String line = "library/phantomjs library/crawler.js " + url; CommandLine cmdLine = CommandLine.parse(line); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchdog = new ExecuteWatchdog(this.timeout); executor.setWatchdog(watchdog); executor.setStreamHandler(psh); try { executor.execute(cmdLine); } catch (Exception e) { throw new BrowserTimeoutException(); } String json = stdout.toString(); return new Gson().fromJson(json, Page.class); }
From source file:de.akquinet.innovation.play.maven.Play2CompilationMojo.java
public void execute() throws MojoExecutionException { String line = getPlay2().getAbsolutePath(); CommandLine cmdLine = CommandLine.parse(line); cmdLine.addArgument("compile"); DefaultExecutor executor = new DefaultExecutor(); if (timeout > 0) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); }// w ww. j a v a 2 s . c o m executor.setExitValue(0); executor.setWorkingDirectory(project.getBasedir()); try { executor.execute(cmdLine, getEnvironment()); } catch (IOException e) { throw new MojoExecutionException("Error during compilation", e); } }
From source file:com.meltmedia.cadmium.blackbox.test.CliCommitCloneTest.java
/** * <p>Runs the Cadmium CLI command commit from Apache Commons Exec library. Then uses {@link CadmiumAssertions#assertContentDeployed(String, java.io.File, String)} to assert that the content deployed properly.</p> * <p>To override the site url, pass <code>-Dcom.meltmedia.cadmium.test.site.url=[URL]</code> to the jvm that this test case will be running in. The site url defaults to <code>http://localhost</code> if not set.</p> * @throws IOException See {@link DefaultExecutor#execute(CommandLine)} for more information. * @throws ExecuteException See {@link DefaultExecutor#execute(CommandLine)} for more information. * @throws InterruptedException /*from w w w . j av a 2 s.c o m*/ * * @see <a href="http://commons.apache.org/exec">Apache Commons Exec</a> */ @Test public void testCommit() throws ExecuteException, IOException { String deployUrl = System.getProperty("com.meltmedia.cadmium.test.site.url", DEPLOY_URL); CommandLine commitCmd = CommandLine .parse("cadmium commit -m \"Testing commit command\" " + TEST_CONTENT_LOCATION + " " + deployUrl); DefaultExecutor exec = new DefaultExecutor(); exec.setExitValue(0); ExecuteWatchdog watchDog = new ExecuteWatchdog(60000); exec.setWatchdog(watchDog); int exitValue = exec.execute(commitCmd); assertEquals("Commit command returned with an error status.", exitValue, 0); assertContentDeployed("Failed to commit content to remote site.", new File(TEST_CONTENT_LOCATION), DEPLOY_URL); }
From source file:com.rest4j.generator.PHPGeneratorTest.java
@Test public void testPHPClient() throws Exception { gen.setStylesheet("com/rest4j/client/php.xslt"); new File("target/php").mkdir(); gen.setApiXmlUrl(getClass().getResource("doc-generator-graph.xml")); gen.setOutputDir("target/php"); gen.addParam(new TemplateParam("common-params", "access-token")); gen.addParam(new TemplateParam("prefix", "Test")); gen.addParam(new TemplateParam("additional-client-code", "// ADDITIONAL CODE")); gen.generate();/* w w w.j a v a2 s. c o m*/ // check the syntax CommandLine cmdLine = CommandLine.parse("php apiclient.php"); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(new File("target/php")); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); int exitValue = executor.execute(cmdLine); assertFalse(executor.isFailure(exitValue)); // check existence of Parameter Object class String client = IOUtils.toString(new File("target/php/apiclient.php").toURI()); assertTrue(client, client.contains("class TestPatchBRequest")); assertTrue(client.contains("ADDITIONAL CODE")); }
From source file:edu.buffalo.fusim.ReadSimulator.java
public void run(String artBinPath, File fusionFile, String outputPrefix, int readLength, int meanFragSize, int readCoverage, boolean pairedEnd) { // Example art call: // art_illumina -i fusion.txt -o testsim -l 75 -f 10 -p -m 400 -s 10 CommandLine cmdLine = new CommandLine(artBinPath); // the filename of input DNA reference cmdLine.addArgument("-i"); cmdLine.addArgument("${file}"); // the prefix of output files cmdLine.addArgument("-o"); cmdLine.addArgument("${outputPrefix}"); // the length of reads to be simulated cmdLine.addArgument("-l"); cmdLine.addArgument("" + readLength); // the fold of read coverage to be simulated cmdLine.addArgument("-f"); cmdLine.addArgument("" + readCoverage); if (pairedEnd) { // indicate a paired-end read simulation cmdLine.addArgument("-p"); // the mean size of DNA fragments for paired-end simulations cmdLine.addArgument("-m"); cmdLine.addArgument("" + meanFragSize); // the standard deviation of DNA fragment size for paired-end simulations. cmdLine.addArgument("-s"); cmdLine.addArgument("10"); }/*from w w w. j av a 2 s . c o m*/ // quite - turn off end of run summary cmdLine.addArgument("-q"); Map map = new HashMap(); map.put("file", fusionFile); map.put("outputPrefix", outputPrefix); cmdLine.setSubstitutionMap(map); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); // Timeout after 5 minutes ExecuteWatchdog watchdog = new ExecuteWatchdog(300000); executor.setWatchdog(watchdog); try { int exitValue = executor.execute(cmdLine); } catch (Exception e) { logger.fatal("Failed to execute ART for simulating Illumina reads: " + e.getMessage()); } }