KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antmod > util > Os


1 package org.antmod.util;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Properties JavaDoc;
5 import java.util.Vector JavaDoc;
6 import java.util.logging.Logger JavaDoc;
7
8 import org.apache.tools.ant.taskdefs.Execute;
9
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 JavaDoc LOGGER = Logger.getLogger(Os.class.getName());
17     
18     private final static Properties JavaDoc ENV_VARS = new Properties JavaDoc();
19     static {
20         loadEnvironment(ENV_VARS);
21     }
22
23     /** never to be instantiated */
24     private Os() {
25     }
26
27     /**
28      * Get the value of the given environment variable.
29      * @param varName The name of the environment variable, case sensitive.
30      * @return The value of the environment variable (if any).
31      */

32     public static String JavaDoc getEnvironmentVariable(String JavaDoc varName) {
33         return ENV_VARS.getProperty(varName);
34     }
35
36
37     /**
38      * Load the environment values using Ant.
39      */

40     private static void loadEnvironment(Properties JavaDoc environmentVariables) {
41         Vector JavaDoc osEnv = Execute.getProcEnvironment();
42         for (Enumeration JavaDoc e = osEnv.elements(); e.hasMoreElements();) {
43             String JavaDoc entry = (String JavaDoc) 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.substring(pos + 1));
49             }
50         }
51     }
52 }
53
Popular Tags