List of usage examples for org.apache.commons.exec DefaultExecutor setWatchdog
public void setWatchdog(final ExecuteWatchdog watchDog)
From source file:eu.learnpad.verification.plugin.pn.modelcheckers.LOLA.java
public static String sync_getVerificationOutput(String lolaBinPath, String modelToVerify, String propertyToVerify, boolean useCoverabilitySearch, final int timeoutInSeconds) throws Exception { int cores = Runtime.getRuntime().availableProcessors(); String filePath = System.getProperty("java.io.tmpdir") + "/" + java.util.UUID.randomUUID() + ".lola"; IOUtils.writeFile(modelToVerify.getBytes(), filePath, false); //IOUtils.writeFile(propertyToVerify.getBytes(), filePath+".ctl", false); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); CommandLine cmdLine = new CommandLine(lolaBinPath); cmdLine.addArgument("--nolog"); //cmdLine.addArgument("--formula="+filePath+".ctl", false); cmdLine.addArgument("--formula=" + propertyToVerify, false); cmdLine.addArgument("--threads=" + cores); if (useCoverabilitySearch) cmdLine.addArgument("--search=cover"); cmdLine.addArgument("-p"); //cmdLine.addArgument("--path=\""+filePath+".out\"", false); //cmdLine.addArgument("--state=\""+filePath+".out\"", false); cmdLine.addArgument(filePath, false); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); ExecuteWatchdog watchdog = new ExecuteWatchdog(1000 * timeoutInSeconds); exec.setWatchdog(watchdog); exec.setStreamHandler(streamHandler); exec.setExitValues(new int[] { 0, 1, 2, 139, 35584 }); int exitVal = exec.execute(cmdLine); String output = outputStream.toString(); if (watchdog.killedProcess()) throw new Exception("ERROR: Timeout occurred. LOLA has reached the execution time limit of " + timeoutInSeconds + " seconds, so it has been aborted.\nPartial Output:\n" + output); if (exitVal != 0 || output.equals("") || output.contains("aborting [#")) throw new Exception("ERROR: LOLA internal error\nExit code:" + exitVal + "\nExec: " + cmdLine.toString() + "\nOutput:\n" + output); new File(filePath).delete(); return output; }
From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java
/** * Runs a command line task and returns the screen output or throws and * error if something bad happened/*from ww w. j a v a 2 s .co m*/ * * @param command Command to run * * @return Screen output * * @throws Exception */ public static String execToString(String command) throws Exception { // Prepare the command line CommandLine commandline = CommandLine.parse(command); // Prepare the output stream ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); // Prepare the executor DefaultExecutor exec = new DefaultExecutor(); exec.setExitValues(null); exec.setStreamHandler(streamHandler); exec.setWatchdog(new ExecuteWatchdog(5000)); // Execute the command try { exec.execute(commandline); return (outputStream.toString()); } catch (Exception e) { throw new Exception(String.format("%s - %s", outputStream.toString(), e.getMessage())); } }
From source file:com.jaeksoft.searchlib.util.ExecuteUtils.java
final public static int command(File workingDirectory, String cmd, String classpath, boolean setJavaTempDir, OutputStream outputStream, OutputStream errorStream, Long timeOut, String... arguments) throws ExecuteException, IOException { Map<String, String> envMap = null; if (classpath != null) { envMap = new HashMap<String, String>(); envMap.put("CLASSPATH", classpath); }//from w w w.ja v a2 s .co m CommandLine commandLine = new CommandLine(cmd); if (setJavaTempDir) if (!StringUtils.isEmpty(SystemUtils.JAVA_IO_TMPDIR)) commandLine.addArgument(StringUtils.fastConcat("-Djava.io.tmpdir=", SystemUtils.JAVA_IO_TMPDIR), false); if (arguments != null) for (String argument : arguments) commandLine.addArgument(argument); DefaultExecutor executor = new DefaultExecutor(); if (workingDirectory != null) executor.setWorkingDirectory(workingDirectory); if (outputStream != null) { PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream); executor.setStreamHandler(pumpStreamHandler); } if (timeOut != null) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeOut); executor.setWatchdog(watchdog); } return envMap != null ? executor.execute(commandLine, envMap) : executor.execute(commandLine); }
From source file:com.boundlessgeo.wps.grass.GrassProcesses.java
/** * Define a GISBASE/LOCATION_NAME for the provided dem. * * @param operation Name used for the location on disk * @param dem File used to establish CRS and Bounds for the location * @return//from w ww. ja va 2s.c o m * @throws Exception */ static File location(CoordinateReferenceSystem crs) throws Exception { String code = CRS.toSRS(crs, true); File geodb = new File(System.getProperty("user.home"), "grassdata"); File location = Files.createTempDirectory(geodb.toPath(), code).toFile(); KVP kvp = new KVP("geodb", geodb, "location", location); // grass70 + ' -c epsg:' + myepsg + ' -e ' + location_path CommandLine cmd = new CommandLine(EXEC); cmd.addArgument("-c"); cmd.addArgument("epsg:" + code); cmd.addArgument("-e"); cmd.addArgument("${location}"); cmd.setSubstitutionMap(kvp); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); executor.setWatchdog(new ExecuteWatchdog(60000)); executor.setStreamHandler(new PumpStreamHandler(System.out)); int exitValue = executor.execute(cmd); return location; }
From source file:io.manasobi.utils.CmdUtils.java
/** * (commandLine) ? ?? ? ? ?//from w ww . j av a 2 s . c o m * * @param commandLine * @param argument ? * @param timeout * * @return int ? */ public static int execute(CommandLine commandLine, String argument, long timeout) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(baos); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); // ? if (StringUtils.isNotEmpty(argument)) { commandLine.addArguments(argument); } // if (timeout > 0) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchdog); } // ?? ? executor.setExitValue(SUCCESS_RETURN_CODE); try { DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); executor.execute(commandLine, resultHandler); resultHandler.waitFor(); return resultHandler.getExitValue(); } catch (Exception e) { throw new CmdUtilsException(e.getMessage()); } }
From source file:com.boundlessgeo.wps.grass.GrassProcesses.java
@DescribeProcess(title = "r.viewshed", description = "Computes the viewshed of a point on an elevation raster map.") @DescribeResult(description = "area visible from provided location") public static GridCoverage2D viewshed( @DescribeParameter(name = "dem", description = "digitial elevation model") GridCoverage2D dem, @DescribeParameter(name = "x", description = "x location in map units") double x, @DescribeParameter(name = "y", description = "y location in map units") double y) throws Exception { String COMMAND = "viewshed"; //Stage files in a temporary location File geodb = Files.createTempDirectory("grassdata").toFile(); geodb.deleteOnExit();/*from w w w .j a va 2 s .co m*/ File location = new File(geodb, COMMAND + Long.toString(random.nextLong()) + "location"); // stage dem file File file = new File(geodb, "dem.tif"); //The file must exist for FileImageOutputStreamExtImplSpi to create the output stream if (!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } final GeoTiffFormat format = new GeoTiffFormat(); GridCoverageWriter writer = format.getWriter(file); writer.write(dem, null); LOGGER.info("Staging file:" + file); // use file to create location with (returns PERMANENT mapset) File mapset = location(location, file); DefaultExecutor executor = new DefaultExecutor(); executor.setWatchdog(new ExecuteWatchdog(60000)); executor.setStreamHandler(new PumpStreamHandler(System.out)); executor.setWorkingDirectory(mapset); Map<String, String> env = customEnv(geodb, location, mapset); // EXPORT IMPORT DEM // r.in.gdal input=~/grassdata/viewshed/PERMANENT/dem.tif output=dem --overwrite File r_in_gdal = bin("r.in.gdal"); CommandLine cmd = new CommandLine(r_in_gdal); cmd.addArgument("input=${file}"); cmd.addArgument("output=dem"); cmd.addArgument("--overwrite"); cmd.setSubstitutionMap(new KVP("file", file)); try { LOGGER.info(cmd.toString()); executor.setExitValue(0); int exitValue = executor.execute(cmd, env); } catch (ExecuteException fail) { LOGGER.warning(r_in_gdal.getName() + ":" + fail.getLocalizedMessage()); throw fail; } // EXECUTE VIEWSHED File r_viewshed = bin("r.viewshed"); cmd = new CommandLine(r_viewshed); cmd.addArgument("input=dem"); cmd.addArgument("output=viewshed"); cmd.addArgument("coordinates=${x},${y}"); cmd.addArgument("--overwrite"); cmd.setSubstitutionMap(new KVP("x", x, "y", y)); try { LOGGER.info(cmd.toString()); executor.setExitValue(0); int exitValue = executor.execute(cmd, env); } catch (ExecuteException fail) { LOGGER.warning(r_viewshed.getName() + ":" + fail.getLocalizedMessage()); throw fail; } // EXECUTE EXPORT VIEWSHED // r.out.gdal --overwrite input=viewshed@PERMANENT output=/Users/jody/grassdata/viewshed/viewshed.tif format=GTiff File viewshed = new File(location, "viewshed.tif"); File r_out_gdal = bin("r.out.gdal"); cmd = new CommandLine(r_out_gdal); cmd.addArgument("input=viewshed"); cmd.addArgument("output=${viewshed}"); cmd.addArgument("--overwrite"); cmd.addArgument("format=GTiff"); cmd.setSubstitutionMap(new KVP("viewshed", viewshed)); try { LOGGER.info(cmd.toString()); executor.setExitValue(0); int exitValue = executor.execute(cmd, env); } catch (ExecuteException fail) { LOGGER.warning(r_out_gdal.getName() + ":" + fail.getLocalizedMessage()); throw fail; } // STAGE RESULT if (!viewshed.exists()) { throw new IOException("Generated viweshed.tif not found"); } GeoTiffReader reader = format.getReader(viewshed); GridCoverage2D coverage = reader.read(null); cleanup(new File(env.get("GISRC"))); return coverage; }
From source file:com.boundlessgeo.wps.grass.GrassProcesses.java
@DescribeProcess(title = "GRASS Version", description = "Retreive the version of GRASS used for computation") @DescribeResult(description = "Version") public static String version() { if (EXEC == null) { return "unavailable"; }//from ww w . j av a 2 s .c om CommandLine cmd = new CommandLine(EXEC); cmd.addArgument("-v"); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outputStream)); LOGGER.info("exec: " + cmd.toString()); int exitValue = executor.execute(cmd); return outputStream.toString(); } catch (ExecuteException huh) { return "exit code: " + huh.getExitValue() + " (" + huh.getMessage() + ")"; } catch (IOException e) { return "unavailable: " + e.getClass().getSimpleName() + ":" + e.getMessage(); } }
From source file:com.boundlessgeo.wps.grass.GrassProcesses.java
/** * Define a GISBASE/LOCATION_NAME/PERMANENT for the provided dem. * * The dem is staged in GISBASE/dem.tif and then moved to * GISBASE/LOCATION_NAME/PERMANENT/dem.tif * * @param operation/* w ww. j a v a2 s . c om*/ * Name used for the location on disk * @param dem * File used to establish CRS and Bounds for the location * @return Array of files consisting of {GISBASE, LOCATION, MAPSET, dem.tif} * @throws Exception */ static File location(File location, File raster) throws Exception { // grass70 + ' -c ' + myfile + ' -e ' + location_path CommandLine cmd = new CommandLine(EXEC); cmd.addArgument("-c"); cmd.addArgument("${raster}"); cmd.addArgument("-e"); cmd.addArgument("${location}"); cmd.setSubstitutionMap(new KVP("raster", raster, "location", location)); LOGGER.info(cmd.toString()); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); executor.setStreamHandler(new PumpStreamHandler(System.out)); LOGGER.info(cmd.toString()); try { int exitValue = executor.execute(cmd); } catch (ExecuteException fail) { LOGGER.warning("grass70:" + fail.getLocalizedMessage()); throw fail; } File mapset = new File(location, "PERMANENT"); if (!mapset.exists()) { throw new IllegalStateException("Did not create mapset " + mapset); } return location; }
From source file:com.boundlessgeo.wps.grass.GrassProcesses.java
/** * Define a GISBASE/LOCATION_NAME/PERMANENT for the provided dem. * * The dem is staged in GISBASE/dem.tif and then moved to * GISBASE/LOCATION_NAME/PERMANENT/dem.tif * * @param operation/*from ww w. j a v a 2 s . co m*/ * Name used for the location on disk * @param dem * File used to establish CRS and Bounds for the location * @return Array of files consisting of {GISBASE, LOCATION, MAPSET, dem.tif} * @throws Exception */ static File[] location(String operation, GridCoverage2D dem) throws Exception { File geodb = new File(System.getProperty("user.home"), "grassdata"); //File location = Files.createTempDirectory(geodb.toPath(),operation).toFile(); File location = new File(geodb, operation); File mapset = new File(location, "PERMANENT"); File file = new File(geodb, "dem.tif"); final GeoTiffFormat format = new GeoTiffFormat(); GridCoverageWriter writer = format.getWriter(file); writer.write(dem, null); System.out.println("Staging file:" + file); // grass70 + ' -c ' + myfile + ' -e ' + location_path CommandLine cmd = new CommandLine(EXEC); cmd.addArgument("-c"); cmd.addArgument("${file}"); cmd.addArgument("-e"); cmd.addArgument("${location}"); cmd.setSubstitutionMap(new KVP("file", file, "location", location)); LOGGER.info(cmd.toString()); DefaultExecutor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(60000); executor.setWatchdog(watchdog); executor.setStreamHandler(new PumpStreamHandler(System.out)); int exitValue = executor.execute(cmd); File origional = file; file = new File(mapset, file.getName()); Files.move(origional.toPath(), file.toPath()); return new File[] { geodb, location, mapset, file }; }
From source file:com.codeabovelab.dm.common.utils.ProcessUtils.java
public static int executeCommand(String command, ExecuteWatchdog watchdog, OutputStream outputStream, OutputStream errorStream, InputStream inputStream, Map<String, String> env) { CommandLine cmdLine = CommandLine.parse(command); DefaultExecutor executor = new DefaultExecutor(); if (outputStream == null) { outputStream = new LogOutputStream() { @Override/*from w w w . jav a2 s. com*/ protected void processLine(String s, int i) { log.error(s); } }; } if (errorStream == null) { errorStream = new LogOutputStream() { @Override protected void processLine(String s, int i) { log.error(s); } }; } executor.setStreamHandler(new PumpStreamHandler(outputStream, errorStream, inputStream)); executor.setExitValues(new int[] { 0, 1 }); if (watchdog != null) { executor.setWatchdog(watchdog); } int exitValue; try { exitValue = executor.execute(cmdLine, env); } catch (IOException e) { exitValue = 1; LOGGER.error("error executing command", e); } return exitValue; }