List of usage examples for org.apache.commons.exec ExecuteWatchdog ExecuteWatchdog
public ExecuteWatchdog(final long timeout)
From source file:it.drwolf.ridire.session.async.Mapper.java
@SuppressWarnings("unchecked") private StringWithEncoding transformPDF2HTML(File resourceFile, EntityManager entityManager) throws IOException, InterruptedException { String workingDirName = System.getProperty("java.io.tmpdir"); String userDir = System.getProperty("user.dir"); byte[] buf = new byte[Mapper.BUFLENGTH]; int count = 0; GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(resourceFile)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((count = gzis.read(buf)) != -1) { baos.write(buf, 0, count);// w w w.j ava 2 s .co m } gzis.close(); baos.close(); byte[] byteArray = baos.toByteArray(); String uuid = UUID.randomUUID().toString(); String pdfFileName = uuid + ".pdf"; String htmlFileName = uuid + ".html"; File tmpDir = new File(workingDirName); String htmlFileNameCompletePath = workingDirName + JobMapperMonitor.FILE_SEPARATOR + htmlFileName; File fileToConvert = new File(tmpDir, pdfFileName); FileUtils.writeByteArrayToFile(fileToConvert, byteArray); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); CommandParameter cp = entityManager.find(CommandParameter.class, CommandParameter.PDFTOHTML_EXECUTABLE_KEY); CommandLine commandLine = CommandLine.parse(cp.getCommandValue()); commandLine.addArgument("-c"); commandLine.addArgument("-i"); commandLine.addArgument(fileToConvert.getAbsolutePath()); commandLine.addArgument(htmlFileNameCompletePath); executor.setExitValue(0); executor.execute(commandLine); try { FileUtils.moveFileToDirectory( new File(userDir + JobMapperMonitor.FILE_SEPARATOR + uuid + "-outline.html"), tmpDir, false); } catch (IOException e) { } cp = entityManager.find(CommandParameter.class, CommandParameter.PDFCLEANER_EXECUTABLE_KEY); commandLine = CommandLine .parse("java -Xmx128m -jar -Djava.io.tmpdir=" + this.tempDir + " " + cp.getCommandValue()); commandLine.addArgument(htmlFileNameCompletePath); commandLine.addArgument("39"); commandLine.addArgument("6"); commandLine.addArgument("5"); executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(Mapper.PDFCLEANER_TIMEOUT); executor.setWatchdog(watchdog); ByteArrayOutputStream baosStdOut = new ByteArrayOutputStream(1024); ExecuteStreamHandler executeStreamHandler = new PumpStreamHandler(baosStdOut, null, null); executor.setStreamHandler(executeStreamHandler); int exitValue = executor.execute(commandLine); String htmlString = null; if (exitValue == 0) { htmlString = baosStdOut.toString(); } FileUtils.deleteQuietly(new File(htmlFileNameCompletePath)); PrefixFileFilter pff = new PrefixFileFilter(uuid); for (File f : FileUtils.listFiles(tmpDir, pff, null)) { FileUtils.deleteQuietly(f); } if (htmlString != null) { htmlString = htmlString.replaceAll(" ", " "); htmlString = htmlString.replaceAll("<br.*?>", " "); CharsetDetector charsetDetector = new CharsetDetector(); charsetDetector.setText(htmlString.getBytes()); String encoding = charsetDetector.detect().getName(); return new StringWithEncoding(htmlString, encoding); } return null; }
From source file:it.drwolf.ridire.index.cwb.CWBPatternSearcher.java
@SuppressWarnings("unchecked") public void test() { String query = "set AutoShow off;\nset ProgressBar off;\nset PrettyPrint off;\nset Context 5 words;\nset LeftKWICDelim '--%%%--';\nset RightKWICDelim '--%%%--';\nshow -cpos;\nA=\"e\";\ncat A 0 9 > \"/home/drwolf/ridirecleaner_tmp/res.tbl\";"; File temp = null;//from ww w . j a v a 2 s . c o m try { temp = File.createTempFile("ridireQ", ".query"); FileUtils.writeStringToFile(temp, query); Executor executor = new DefaultExecutor(); CommandLine commandLine = new CommandLine("/usr/local/cwb-3.4.3/bin/cqp"); commandLine.addArgument("-f").addArgument(temp.getAbsolutePath()).addArgument("-D") .addArgument("RIDIRE2").addArgument("-r").addArgument("/usr/local/share/cwb/registry/"); executor.execute(commandLine); File resFile = new File("/home/drwolf/ridirecleaner_tmp/res.tbl"); List<String> lines = FileUtils.readLines(resFile); this.resultsSimple = new ArrayList<CWBResult>(); for (String l : lines) { String[] res = l.split("--%%%--"); CWBResult item = new CWBResult(res[0], res[1], res[2], "", null, null); this.resultsSimple.add(item); } this.entityManager.createNativeQuery("drop table if exists pippo").executeUpdate(); this.entityManager.createNativeQuery( "CREATE TABLE `pippo` (`text_id` varchar(40) DEFAULT NULL,`beginPosition` int(11) DEFAULT NULL,`endPosition` int(11) DEFAULT NULL,`refnumber` bigint(20) NOT NULL,`dist` smallint(6) NOT NULL,`word` varchar(40) NOT NULL,`lemma` varchar(40) NOT NULL, `pos` varchar(40) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8") .executeUpdate(); File tmpAwk = File.createTempFile("ridireAWK", ".awk"); String awk = "BEGIN{ OFS = FS = \"\t\" } { print $3, $1, $2, NR-1, -5, $4 } { print $3, $1, $2, NR-1, -4, $5 } { print $3, $1, $2, NR-1, -3, $6 } { print $3, $1, $2, NR-1, -2, $7 } { print $3, $1, $2, NR-1, -1, $8 } { print $3, $1, $2, NR-1, 1, $9 } { print $3, $1, $2, NR-1, 2, $10 } { print $3, $1, $2, NR-1, 3, $11 } { print $3, $1, $2, NR-1, 4, $12 } { print $3, $1, $2, NR-1, 5, $13 } "; FileUtils.writeStringToFile(tmpAwk, awk); File tmpTabulate = File.createTempFile("ridireTAB", ".tab"); String tabulate = "set AutoShow off;\nset ProgressBar off;\nset PrettyPrint off;\nset Context 5 words;\nset LeftKWICDelim '--%%%--';\nset RightKWICDelim '--%%%--';\nshow -cpos;\nA=\"e\";\ntabulate A match, matchend, match text_id, match[-5] word, match[-4] word, match[-3] word, match[-2] word, match[-1] word, matchend[1] word, matchend[2] word, matchend[3] word, matchend[4] word, matchend[5] word " + "> \"| awk -f '" + tmpAwk.getAbsolutePath() + "' > '" + tmpTabulate.getAbsolutePath() + "'\";"; File tempSh = File.createTempFile("ridireSH", ".sh"); FileUtils.writeStringToFile(tempSh, tabulate); tempSh.setExecutable(true); executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(CWBPatternSearcher.TIMEOUT); executor.setWatchdog(watchdog); commandLine = new CommandLine("/usr/local/cwb-3.4.3/bin/cqp"); commandLine.addArgument("-f").addArgument(tempSh.getAbsolutePath()).addArgument("-D") .addArgument("RIDIRE2").addArgument("-r").addArgument("/usr/local/share/cwb/registry/"); executor.execute(commandLine); FileUtils.deleteQuietly(tempSh); this.entityManager .createNativeQuery( "LOAD DATA LOCAL INFILE '" + tmpTabulate.getAbsolutePath() + "' INTO TABLE " + "pippo") .executeUpdate(); long n = ((Number) this.entityManager.createNativeQuery("select sum(freq) as somma from freq_forma_all") .getSingleResult()).longValue(); long r1 = ((Number) this.entityManager .createNativeQuery("select count(*) from pippo where dist between -3 and 3").getSingleResult()) .longValue(); String nativeQuery = "select pippo.word, count(pippo.word) as observed," + " (" + r1 + " * (freq_forma_all.freq) / " + n + ") as expected, sign(COUNT(pippo.word) - (" + r1 + " * (freq_forma_all.freq) / " + n + ")) * 2 * ( IF(COUNT(pippo.word) > 0, COUNT(pippo.word) * log(COUNT(pippo.word) / (" + r1 + " * (freq_forma_all.freq) / " + n + ")), 0) + IF((" + r1 + " - COUNT(pippo.word)) > 0, (" + r1 + " - COUNT(pippo.word)) * log((" + r1 + " - COUNT(pippo.word)) / (" + r1 + " * (" + n + " - (freq_forma_all.freq)) / " + n + ")), 0) + IF(((freq_forma_all.freq) - COUNT(pippo.word)) > 0, ((freq_forma_all.freq) - COUNT(pippo.word)) * log(((freq_forma_all.freq) - COUNT(pippo.word)) / (" + (n - r1) + " * (freq_forma_all.freq) / " + n + ")), 0) + IF((" + (n - r1) + " - ((freq_forma_all.freq) - COUNT(pippo.word))) > 0, (" + (n - r1) + " - ((freq_forma_all.freq) - COUNT(pippo.word))) * log((" + (n - r1) + " - ((freq_forma_all.freq) - COUNT(pippo.word))) / (" + (n - r1) + " * (" + n + " - (freq_forma_all.freq)) / " + n + ")), 0) ) as significance, freq_forma_all.freq, count(distinct(text_id)) as text_id_count from pippo, freq_forma_all where pippo.word = freq_forma_all.item and dist between -3 and 3 and freq_forma_all.freq >= 1 group by pippo.word having observed >= 1 order by significance desc LIMIT 0, 50 "; List<Object[]> res = this.entityManager.createNativeQuery(nativeQuery).getResultList(); for (Object[] r : res) { System.out.println(r[0] + "\t" + r[3]); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:it.drwolf.ridire.index.cwb.CWBConcordancer.java
@SuppressWarnings("unchecked") public void test() { String query = "set AutoShow off;\nset ProgressBar off;\nset PrettyPrint off;\nset Context 5 words;\nset LeftKWICDelim '--%%%--';\nset RightKWICDelim '--%%%--';\nshow -cpos;\nA=\"e\";\ncat A 0 9 > \"/home/drwolf/ridirecleaner_tmp/res.tbl\";"; File temp = null;/* w ww . ja va 2 s .co m*/ try { temp = File.createTempFile("ridireQ", ".query"); FileUtils.writeStringToFile(temp, query); Executor executor = new DefaultExecutor(); CommandLine commandLine = new CommandLine("/usr/local/cwb-3.4.3/bin/cqp"); commandLine.addArgument("-f").addArgument(temp.getAbsolutePath()).addArgument("-D") .addArgument("RIDIRE2").addArgument("-r").addArgument("/usr/local/share/cwb/registry/"); executor.execute(commandLine); File resFile = new File("/home/drwolf/ridirecleaner_tmp/res.tbl"); List<String> lines = FileUtils.readLines(resFile); this.resultsSimple = new ArrayList<CWBResult>(); for (String l : lines) { String[] res = l.split("--%%%--"); CWBResult item = new CWBResult(res[0], res[1], res[2], "", null, null); this.resultsSimple.add(item); } this.entityManager.createNativeQuery("drop table if exists pippo").executeUpdate(); this.entityManager.createNativeQuery( "CREATE TABLE `pippo` (`text_id` varchar(40) DEFAULT NULL,`beginPosition` int(11) DEFAULT NULL,`endPosition` int(11) DEFAULT NULL,`refnumber` bigint(20) NOT NULL,`dist` smallint(6) NOT NULL,`word` varchar(40) NOT NULL,`lemma` varchar(40) NOT NULL, `pos` varchar(40) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=utf8") .executeUpdate(); File tmpAwk = File.createTempFile("ridireAWK", ".awk"); String awk = "BEGIN{ OFS = FS = \"\t\" } { print $3, $1, $2, NR-1, -5, $4 } { print $3, $1, $2, NR-1, -4, $5 } { print $3, $1, $2, NR-1, -3, $6 } { print $3, $1, $2, NR-1, -2, $7 } { print $3, $1, $2, NR-1, -1, $8 } { print $3, $1, $2, NR-1, 1, $9 } { print $3, $1, $2, NR-1, 2, $10 } { print $3, $1, $2, NR-1, 3, $11 } { print $3, $1, $2, NR-1, 4, $12 } { print $3, $1, $2, NR-1, 5, $13 } "; FileUtils.writeStringToFile(tmpAwk, awk); File tmpTabulate = File.createTempFile("ridireTAB", ".tab"); String tabulate = "set AutoShow off;\nset ProgressBar off;\nset PrettyPrint off;\nset Context 5 words;\nset LeftKWICDelim '--%%%--';\nset RightKWICDelim '--%%%--';\nshow -cpos;\nA=\"e\";\ntabulate A match, matchend, match text_id, match[-5] word, match[-4] word, match[-3] word, match[-2] word, match[-1] word, matchend[1] word, matchend[2] word, matchend[3] word, matchend[4] word, matchend[5] word " + "> \"| awk -f '" + tmpAwk.getAbsolutePath() + "' > '" + tmpTabulate.getAbsolutePath() + "'\";"; File tempSh = File.createTempFile("ridireSH", ".sh"); FileUtils.writeStringToFile(tempSh, tabulate); tempSh.setExecutable(true); executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(CWBConcordancer.TIMEOUT); executor.setWatchdog(watchdog); commandLine = new CommandLine("/usr/local/cwb-3.4.3/bin/cqp"); commandLine.addArgument("-f").addArgument(tempSh.getAbsolutePath()).addArgument("-D") .addArgument("RIDIRE2").addArgument("-r").addArgument("/usr/local/share/cwb/registry/"); executor.execute(commandLine); FileUtils.deleteQuietly(tempSh); this.entityManager .createNativeQuery( "LOAD DATA LOCAL INFILE '" + tmpTabulate.getAbsolutePath() + "' INTO TABLE " + "pippo") .executeUpdate(); long n = ((Number) this.entityManager.createNativeQuery("select sum(freq) as somma from freq_forma_all") .getSingleResult()).longValue(); long r1 = ((Number) this.entityManager .createNativeQuery("select count(*) from pippo where dist between -3 and 3").getSingleResult()) .longValue(); String nativeQuery = "select pippo.word, count(pippo.word) as observed," + " (" + r1 + " * (freq_forma_all.freq) / " + n + ") as expected, sign(COUNT(pippo.word) - (" + r1 + " * (freq_forma_all.freq) / " + n + ")) * 2 * ( IF(COUNT(pippo.word) > 0, COUNT(pippo.word) * log(COUNT(pippo.word) / (" + r1 + " * (freq_forma_all.freq) / " + n + ")), 0) + IF((" + r1 + " - COUNT(pippo.word)) > 0, (" + r1 + " - COUNT(pippo.word)) * log((" + r1 + " - COUNT(pippo.word)) / (" + r1 + " * (" + n + " - (freq_forma_all.freq)) / " + n + ")), 0) + IF(((freq_forma_all.freq) - COUNT(pippo.word)) > 0, ((freq_forma_all.freq) - COUNT(pippo.word)) * log(((freq_forma_all.freq) - COUNT(pippo.word)) / (" + (n - r1) + " * (freq_forma_all.freq) / " + n + ")), 0) + IF((" + (n - r1) + " - ((freq_forma_all.freq) - COUNT(pippo.word))) > 0, (" + (n - r1) + " - ((freq_forma_all.freq) - COUNT(pippo.word))) * log((" + (n - r1) + " - ((freq_forma_all.freq) - COUNT(pippo.word))) / (" + (n - r1) + " * (" + n + " - (freq_forma_all.freq)) / " + n + ")), 0) ) as significance, freq_forma_all.freq, count(distinct(text_id)) as text_id_count from pippo, freq_forma_all where pippo.word = freq_forma_all.item and dist between -3 and 3 and freq_forma_all.freq >= 1 group by pippo.word having observed >= 1 order by significance desc LIMIT 0, 50 "; List<Object[]> res = this.entityManager.createNativeQuery(nativeQuery).getResultList(); for (Object[] r : res) { System.out.println(r[0] + "\t" + r[3]); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:gov.nasa.jpl.magicdraw.projectUsageIntegrity.graph.SSCAEProjectUsageGraph.java
public BufferedImageFile convertDOTFile(@Nonnull File pugDOT, @Nonnull DOTImageFormat dotImageFormat) throws IIOException, IOException, InterruptedException { String dotCommand = ProjectUsageIntegrityPlugin.getInstance().getDOTexecutablePath(); if (null == dotCommand) return null; File pugTemp = pugDOT.getParentFile(); File pugImage = new File(pugTemp.getAbsoluteFile() + File.separator + project.getID() + "." + DOTImageFormatName.get(dotImageFormat)); if (pugImage.exists()) { pluginLog.info(String.format("%s - convertDOTFile - deleting previous image for '%s' : '%s'", pluginName, project.getName(), pugImage.getName())); pugImage.delete();//from ww w . j a v a 2 s .co m } CommandLine cmdLine = new CommandLine(dotCommand); cmdLine.addArgument("-Tpng"); cmdLine.addArgument("-o"); cmdLine.addArgument(pugImage.getName()); cmdLine.addArgument(pugDOT.getName()); pluginLog.info(String.format("%s - convertDOTgraph - converting gv to image for '%s'", pluginName, project.getName())); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000); // consider '0' exit value as success. Executor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWatchdog(watchdog); executor.setWorkingDirectory(pugTemp); executor.execute(cmdLine, resultHandler); resultHandler.waitFor(); if (!executor.isFailure(resultHandler.getExitValue())) { pluginLog.info(String.format("%s - convertDOTgraph - reading image for '%s' from: '%s'", pluginName, project.getName(), pugImage.getName())); BufferedImageFile imageFile = new BufferedImageFile(pugImage); pluginLog.info( String.format("%s - convertDOTgraph - got image for '%s'", pluginName, project.getName())); return imageFile; } return null; }
From source file:gov.nasa.jpl.magicdraw.projectUsageIntegrity.graph.SSCAEProjectUsageGraph.java
/** * @param pugDOT gv file//from w w w . jav a 2 s.c o m * @return true if the graphviz application was opened successfully for the gv file. * @throws IIOException * @throws IOException * @throws InterruptedException */ public boolean openDOTFileWithGraphViz(@Nonnull File pugDOT) throws IIOException, IOException, InterruptedException { String graphvizApp = ProjectUsageIntegrityPlugin.getInstance().getGraphvizApplicationPath(); if (null == graphvizApp) return false; File pugTemp = pugDOT.getParentFile(); CommandLine cmdLine; switch (SSCAEProjectUsageIntegrityOptions.getCurrentPlatform()) { case LINUX: cmdLine = new CommandLine(graphvizApp); break; case MACOSX: cmdLine = new CommandLine("/usr/bin/open"); cmdLine.addArgument("-a"); cmdLine.addArgument(graphvizApp); break; case WINDOWS: cmdLine = new CommandLine("cmd"); cmdLine.addArgument("/c"); cmdLine.addArgument("start"); cmdLine.addArgument(graphvizApp); break; default: return false; } cmdLine.addArgument(pugDOT.getName()); pluginLog.info(String.format("%s - openDOTFileWithGraphViz - opening DOT file for project: '%s'", pluginName, project.getName())); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000); // consider '0' exit value as success. Executor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWatchdog(watchdog); executor.setWorkingDirectory(pugTemp); executor.execute(cmdLine, resultHandler); resultHandler.waitFor(); if (executor.isFailure(resultHandler.getExitValue())) { pluginLog.error(String.format( "%s - openDOTFileWithGraphViz - error while opening DOT file for project '%s' from: '%s'", pluginName, project.getName(), pugDOT.getAbsolutePath()), resultHandler.getException()); return false; } pluginLog.info(String.format("%s - openDOTFileWithGraphViz - opened DOT file for project '%s' from: '%s'", pluginName, project.getName(), pugDOT.getAbsolutePath())); return true; }
From source file:net.spinetrak.rpitft.command.Command.java
Result execute(final Stream stream_) { final CommandLine commandline = CommandLine.parse(_script); final DefaultExecutor exec = new DefaultExecutor(); final ExecuteWatchdog watchdog = new ExecuteWatchdog(500); exec.setWatchdog(watchdog);/* ww w . ja v a2s. co m*/ final PumpStreamHandler streamHandler = new PumpStreamHandler(stream_.getStream()); exec.setStreamHandler(streamHandler); int result = -1; try { result = exec.execute(commandline); } catch (final IOException ex_) { //LOGGER.error(ex_.getMessage()); } return new Result(stream_, result); }
From source file:org.apache.bigtop.itest.hive.HiveHelper.java
public static Map<String, String> execCommand(CommandLine commandline, Map<String, String> envVars) { System.out.println("Executing command:"); System.out.println(commandline.toString()); Map<String, String> env = null; Map<String, String> entry = new HashMap<String, String>(); try {/*from w w w . ja v a2s. co m*/ env = EnvironmentUtils.getProcEnvironment(); } catch (IOException e1) { // TODO Auto-generated catch block LOG.debug("Failed to get process environment: " + e1.getMessage()); e1.printStackTrace(); } if (envVars != null) { for (String key : envVars.keySet()) { env.put(key, envVars.get(key)); } } DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 10000); Executor executor = new DefaultExecutor(); executor.setExitValue(1); executor.setWatchdog(watchdog); executor.setStreamHandler(streamHandler); try { executor.execute(commandline, env, resultHandler); } catch (ExecuteException e) { // TODO Auto-generated catch block LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue())); LOG.debug("outputStream: " + outputStream.toString()); entry.put("exitValue", String.valueOf(resultHandler.getExitValue())); entry.put("outputStream", outputStream.toString() + e.getMessage()); e.printStackTrace(); return entry; } catch (IOException e) { // TODO Auto-generated catch block LOG.debug("Failed to execute command with exit value: " + String.valueOf(resultHandler.getExitValue())); LOG.debug("outputStream: " + outputStream.toString()); entry.put("exitValue", String.valueOf(resultHandler.getExitValue())); entry.put("outputStream", outputStream.toString() + e.getMessage()); e.printStackTrace(); return entry; } try { resultHandler.waitFor(); /*System.out.println("Command output: "+outputStream.toString());*/ entry.put("exitValue", String.valueOf(resultHandler.getExitValue())); entry.put("outputStream", outputStream.toString()); return entry; } catch (InterruptedException e) { // TODO Auto-generated catch block /*System.out.println("Command output: "+outputStream.toString());*/ LOG.debug("exitValue: " + String.valueOf(resultHandler.getExitValue())); LOG.debug("outputStream: " + outputStream.toString()); entry.put("exitValue", String.valueOf(resultHandler.getExitValue())); entry.put("outputStream", outputStream.toString()); e.printStackTrace(); return entry; } }
From source file:org.apache.camel.component.exec.impl.DefaultExecCommandExecutor.java
protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null);//from ww w.j a va2 s. c om if (execCommand.getWorkingDir() != null) { executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile()); } if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) { executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout())); } executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); return executor; }
From source file:org.apache.falcon.regression.core.util.ExecUtil.java
public static ExecResult executeCommand(CommandLine commandLine) { LOGGER.info("Command to be executed: " + commandLine); DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(5 * 1000)); //timeout of 5 seconds final ByteArrayOutputStream outStream = new ByteArrayOutputStream(); final ByteArrayOutputStream errStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outStream, errStream)); int exitVal = 1; String exception = ""; try {//from ww w .jav a 2s . co m exitVal = executor.execute(commandLine); } catch (IOException e) { LOGGER.warn("Caught exception: " + e); exception = e.toString(); } final String output = outStream.toString(); String errors = errStream.toString(); errors = errors.isEmpty() ? exception : errors; LOGGER.info("exitVal: " + exitVal); LOGGER.info("output: " + output); LOGGER.info("errors: " + errors); return new ExecResult(commandLine, exitVal, output.trim(), errors.trim()); }
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);//from w w w . j ava 2 s .c o m // 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); 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; }