List of usage examples for org.apache.commons.exec.environment EnvironmentUtils addVariableToEnvironment
public static void addVariableToEnvironment(final Map<String, String> environment, final String keyAndValue)
From source file:io.selendroid.io.ShellCommand.java
public static void execAsync(String display, CommandLine commandline) throws ShellCommandException { log.info("executing async command: " + commandline); DefaultExecutor exec = new DefaultExecutor(); ExecuteResultHandler handler = new DefaultExecuteResultHandler(); PumpStreamHandler streamHandler = new PumpStreamHandler(new PritingLogOutputStream()); exec.setStreamHandler(streamHandler); try {//from w w w. j a v a 2s . c o m if (display == null || display.isEmpty()) { exec.execute(commandline, handler); } else { Map env = EnvironmentUtils.getProcEnvironment(); EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display); exec.execute(commandline, env, handler); } } catch (Exception e) { throw new ShellCommandException("An error occured while executing shell command: " + commandline, e); } }
From source file:io.selendroid.standalone.io.ShellCommand.java
public static void execAsync(String display, CommandLine commandline) throws ShellCommandException { log.info("executing async command: " + commandline); DefaultExecutor exec = new DefaultExecutor(); ExecuteResultHandler handler = new DefaultExecuteResultHandler(); PumpStreamHandler streamHandler = new PumpStreamHandler(new PrintingLogOutputStream()); exec.setStreamHandler(streamHandler); try {/*from w w w . j av a 2 s .co m*/ if (display == null || display.isEmpty()) { exec.execute(commandline, handler); } else { Map env = EnvironmentUtils.getProcEnvironment(); EnvironmentUtils.addVariableToEnvironment(env, "DISPLAY=:" + display); exec.execute(commandline, env, handler); } } catch (Exception e) { log.log(Level.SEVERE, "Error executing command: " + commandline, e); throw new ShellCommandException("Error executing shell command: " + commandline, e); } }
From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.UnixUtils.java
public static void exec(final String command, final Map<String, String> environmentVariables, final OutputStream outputStream) throws IOException, ExecuteException { final Map<?, ?> executionEnvironment = EnvironmentUtils.getProcEnvironment(); for (final Entry<String, String> environmentVariable : environmentVariables.entrySet()) { final String keyAndValue = environmentVariable.getKey() + "=" + environmentVariable.getValue(); EnvironmentUtils.addVariableToEnvironment(executionEnvironment, keyAndValue); }/* w w w. j a v a2 s. co m*/ final PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); final CommandLine commandLine = new CommandLine("/bin/bash"); commandLine.addArguments(new String[] { "-c", command }, false); final DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); executor.execute(commandLine, executionEnvironment); }
From source file:com.boundlessgeo.wps.grass.GrassProcesses.java
/** * Define environment variable for independent grass operation. * see: http://grasswiki.osgeo.org/wiki/GRASS_and_Shell * @param geodb//from w ww .ja v a 2 s .c o m * @param location * @param mapset * @return * @throws IOException */ private static Map<String, String> customEnv(File geodb, File location, File mapset) throws IOException { Map<String, String> env = EnvironmentUtils.getProcEnvironment(); // GRASS ENV File GISBASE = new File(EXEC).getParentFile(); String GRASS_VERSION = "7.0.0"; EnvironmentUtils.addVariableToEnvironment(env, "GISBASE=" + GISBASE); EnvironmentUtils.addVariableToEnvironment(env, "GRASS_VERSION=" + GRASS_VERSION); File GISRC = new File(System.getProperty("user.home"), ".grassrc." + GRASS_VERSION + "." + location.getName()); try (BufferedWriter writer = new BufferedWriter(new FileWriter(GISRC))) { writer.write("GISDBASE: " + geodb); writer.newLine(); writer.write("LOCATION_NAME: " + location.getName()); writer.newLine(); writer.write("MAPSET: PERMANENT"); writer.newLine(); writer.write("GRASS_GUI: text"); writer.newLine(); } EnvironmentUtils.addVariableToEnvironment(env, "GISRC=" + GISRC); // SYSTEM ENV String bin = new File(GISBASE, "bin").getAbsolutePath(); String scripts = new File(GISBASE, "scripts").getAbsolutePath(); String lib = new File(GISBASE, "lib").getAbsolutePath(); if (SYSTEM == Env.WINDOWS) { String PATH = env.get("PATH") + pathSeparator + bin + pathSeparator + scripts + pathSeparator + lib; EnvironmentUtils.addVariableToEnvironment(env, "PATH=" + PATH); } else { String PATH = env.get("PATH") + pathSeparator + bin + pathSeparator + scripts; EnvironmentUtils.addVariableToEnvironment(env, "PATH=" + PATH); if (SYSTEM == Env.LINUX) { String LD_LIBRARY_PATH = (env.containsKey("LD_LIBRARY_PATH") ? env.get("LD_LIBRARY_PATH") + pathSeparator : "") + lib; EnvironmentUtils.addVariableToEnvironment(env, "LD_LIBRARY_PATH=" + LD_LIBRARY_PATH); } else if (SYSTEM == Env.MAC) { String DYLD_LIBRARY_PATH = (env.containsKey("DYLD_LIBRARY_PATH") ? env.get("DYLD_LIBRARY_PATH") + pathSeparator : "") + lib; EnvironmentUtils.addVariableToEnvironment(env, "DYLD_LIBRARY_PATH=" + DYLD_LIBRARY_PATH); } } return env; }
From source file:it.drwolf.ridire.index.cwb.CWBCollocatesExtractor.java
private File tabulate() throws IOException { EnvironmentUtils.addVariableToEnvironment(EnvironmentUtils.getProcEnvironment(), "LC_ALL=C"); File tmpAwk = File.createTempFile("ridireAWK", ".awk"); String awk = this.createAWKString(); FileUtils.writeStringToFile(tmpAwk, awk); File tmpTabulate = File.createTempFile("ridireTAB", ".tab"); String tabulate = this.createTabulateString(tmpAwk, tmpTabulate); File tempSh = File.createTempFile("ridireSH", ".sh"); FileUtils.writeStringToFile(tempSh, tabulate); tempSh.setExecutable(true);//from www . j av a 2 s.co m Executor executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(CWBCollocatesExtractor.CWB_COLLOCATES_EXTRACTOR_TIMEOUT); executor.setWatchdog(watchdog); CommandLine commandLine = new CommandLine(this.cqpExecutable); commandLine.addArgument("-f").addArgument(tempSh.getAbsolutePath()).addArgument("-D") .addArgument(this.cqpCorpusName).addArgument("-r").addArgument(this.cqpRegistry); executor.execute(commandLine); FileUtils.deleteQuietly(tmpAwk); FileUtils.deleteQuietly(tempSh); return tmpTabulate; }
From source file:it.drwolf.ridire.index.cwb.CWBPatternSearcher.java
private Integer getCQPQueryResultsSize(File queryFile, String cqpSizeQuery) throws ExecuteException, IOException { EnvironmentUtils.addVariableToEnvironment(EnvironmentUtils.getProcEnvironment(), "LC_ALL=C"); Executor executor = new DefaultExecutor(); File tempSize = File.createTempFile("ridireSZ", ".size"); File tempSh = File.createTempFile("ridireSH", ".sh"); CommandLine commandLine = new CommandLine(this.cqpExecutable); commandLine.addArgument("-f").addArgument(queryFile.getAbsolutePath()).addArgument("-D") .addArgument(this.cqpCorpusName).addArgument("-r").addArgument(this.cqpRegistry); String commLineString = commandLine.toString() + " > " + tempSize.getAbsolutePath(); FileUtils.writeStringToFile(tempSh, commLineString); tempSh.setExecutable(true);/* w ww . ja va 2s . c o m*/ executor = new DefaultExecutor(); executor.setExitValue(0); ExecuteWatchdog watchdog = new ExecuteWatchdog(CWBPatternSearcher.TIMEOUT); executor.setWatchdog(watchdog); commandLine = new CommandLine(tempSh.getAbsolutePath()); executor.execute(commandLine); Integer size = 0; List<String> lines = FileUtils.readLines(tempSize); if (lines.size() > 0) { size = Integer.parseInt(lines.get(0).trim()); } FileUtils.deleteQuietly(tempSh); FileUtils.deleteQuietly(tempSize); return size; }
From source file:org.apache.geode.examples.replicated.ReplicatedTest.java
@Before public void setup() throws IOException { // ignores test if running on windows assumeThat(System.getProperty("os.name").startsWith("Windows"), is(false)); locatorPort = getAvailablePort();//from www. jav a 2s .co m environment = EnvironmentUtils.getProcEnvironment(); EnvironmentUtils.addVariableToEnvironment(environment, GEODE_LOCATOR_PORT + locatorPort); logger.fine("Locator port: " + locatorPort); }
From source file:org.mule.test.infrastructure.process.MuleUtils.java
private static Map<String, String> addEnvProperties(String[] envVars) throws IOException { @SuppressWarnings("unchecked") Map<String, String> env = EnvironmentUtils.getProcEnvironment(); for (String envVar : envVars) { EnvironmentUtils.addVariableToEnvironment(env, envVar); }/*from w ww . j a v a2s. com*/ return env; }
From source file:sce.ProcessExecutor.java
public String executeProcess(String[] processParameters) throws JobExecutionException { try {//from w w w . ja v a 2 s . co m //Command to be executed CommandLine command = new CommandLine(processParameters[0]); String[] params = new String[processParameters.length - 1]; for (int i = 0; i < processParameters.length - 1; i++) { params[i] = processParameters[i + 1]; } //Adding its arguments command.addArguments(params); //set timeout in seconds ExecuteWatchdog watchDog = new ExecuteWatchdog( this.timeout == 0 ? ExecuteWatchdog.INFINITE_TIMEOUT : this.timeout * 1000); this.watchdog = watchDog; //Result Handler for executing the process in a Asynch way DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); //MyResultHandler resultHandler = new MyResultHandler(); //Using Std out for the output/error stream //ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); //This is used to end the process when the JVM exits ShutdownHookProcessDestroyer processDestroyer = new ShutdownHookProcessDestroyer(); //Our main command executor DefaultExecutor executor = new DefaultExecutor(); //Setting the properties executor.setStreamHandler(new PumpStreamHandler(null, null)); executor.setWatchdog(watchDog); //executor.setExitValue(1); // this has to be set if the java code contains System.exit(1) to avoid a FAILED status //Setting the working directory //Use of recursion along with the ls makes this a long running process //executor.setWorkingDirectory(new File("/home")); executor.setProcessDestroyer(processDestroyer); //if set, use the java environment variables when running the command if (!this.environment.equals("")) { Map<String, String> procEnv = EnvironmentUtils.getProcEnvironment(); EnvironmentUtils.addVariableToEnvironment(procEnv, this.environment); //Executing the command executor.execute(command, procEnv, resultHandler); } else { //Executing the command executor.execute(command, resultHandler); } //The below section depends on your need //Anything after this will be executed only when the command completes the execution resultHandler.waitFor(); /*int exitValue = resultHandler.getExitValue(); System.out.println(exitValue); if (executor.isFailure(exitValue)) { System.out.println("Execution failed"); } else { System.out.println("Execution Successful"); } System.out.println(outputStream.toString());*/ //return outputStream.toString(); if (watchdog.killedProcess()) { throw new JobExecutionException("Job Interrupted", new InterruptedException()); } if (executor.isFailure(resultHandler.getExitValue())) { ExecuteException ex = resultHandler.getException(); throw new JobExecutionException(ex.getMessage(), ex); } return "1"; } catch (ExecuteException ex) { throw new JobExecutionException(ex.getMessage(), ex); } catch (IOException | InterruptedException | JobExecutionException ex) { throw new JobExecutionException(ex.getMessage(), ex); } }