01: package org.antmod.util;
02:
03: import java.util.Enumeration;
04: import java.util.Properties;
05: import java.util.Vector;
06: import java.util.logging.Logger;
07:
08: import org.apache.tools.ant.taskdefs.Execute;
09:
10: /**
11: * Access to some operating system specific things, like environment variables.
12: *
13: * @author Klaas Waslander
14: */
15: public final class Os {
16: private final static Logger LOGGER = Logger.getLogger(Os.class
17: .getName());
18:
19: private final static Properties ENV_VARS = new Properties();
20: static {
21: loadEnvironment(ENV_VARS);
22: }
23:
24: /** never to be instantiated */
25: private Os() {
26: }
27:
28: /**
29: * Get the value of the given environment variable.
30: * @param varName The name of the environment variable, case sensitive.
31: * @return The value of the environment variable (if any).
32: */
33: public static String getEnvironmentVariable(String varName) {
34: return ENV_VARS.getProperty(varName);
35: }
36:
37: /**
38: * Load the environment values using Ant.
39: */
40: private static void loadEnvironment(Properties environmentVariables) {
41: Vector osEnv = Execute.getProcEnvironment();
42: for (Enumeration e = osEnv.elements(); e.hasMoreElements();) {
43: String entry = (String) e.nextElement();
44: int pos = entry.indexOf('=');
45: if (pos == -1) {
46: LOGGER.warning("Ignoring: " + entry);
47: } else {
48: environmentVariables.put(entry.substring(0, pos), entry
49: .substring(pos + 1));
50: }
51: }
52: }
53: }
|