001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util.externalprocess;
011:
012: import java.io.ByteArrayOutputStream;
013: import java.io.IOException;
014: import java.util.Properties;
015: import java.util.StringTokenizer;
016: import java.util.Vector;
017:
018: /** Reader for importing the OS Environment properties into java.
019: * java.lang.System.getProperties() has only some environment info
020: *
021: * @todo more OS support
022: *
023: * @author Nico Klasens (Finalist IT Group)
024: * @version $Id: EnvironmentReader.java,v 1.6 2007/03/02 21:05:15 nklasens Exp $
025: * @since MMBase-1.6
026: */
027: public class EnvironmentReader {
028: private static Properties envVars = null;
029: private static Vector<String> rawVars = null;
030:
031: /**
032: * Get value of environment properties
033: * @return Properties environment
034: */
035: public static Properties getEnvVars() {
036:
037: if (null != envVars)
038: return envVars;
039:
040: envVars = new Properties();
041: rawVars = new Vector<String>(32);
042: String lineSeparator = System.getProperty("line.separator");
043:
044: String command = getEnvCommand();
045:
046: CommandLauncher launcher = new CommandLauncher(
047: "EnvironmentReader");
048: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
049: try {
050: launcher.execute(command);
051: launcher.waitAndRead(outputStream, null);
052:
053: String envStr = outputStream.toString();
054: StringTokenizer strTokens = new StringTokenizer(envStr,
055: lineSeparator);
056:
057: String line;
058: while (strTokens.hasMoreTokens()) {
059: line = strTokens.nextToken();
060:
061: rawVars.add(line);
062: int idx = line.indexOf('=');
063: if (idx != -1) {
064: String key = line.substring(0, idx);
065: String value = line.substring(idx + 1);
066: envVars.setProperty(key, value);
067: } else {
068: envVars.setProperty(line, "");
069: }
070: }
071: } catch (ProcessException e) {
072: } finally {
073: try {
074: if (outputStream != null) {
075: outputStream.close();
076: }
077: } catch (IOException e) {
078: //ignore
079: }
080: }
081: rawVars.trimToSize();
082: return envVars;
083: }
084:
085: /**
086: * Command string to get OS Environment properties
087: * @return String
088: */
089: public static String getEnvCommand() {
090: String OS = System.getProperty("os.name").toLowerCase();
091: if (OS.indexOf("windows 9") > -1) {
092: return "command.com /c set";
093: } else {
094: if ((OS.indexOf("nt") > -1)
095: || (OS.indexOf("windows 2000") > -1)
096: || (OS.indexOf("windows xp") > -1)) {
097: return "cmd.exe /c set";
098: } else {
099: throw new UnsupportedOperationException(
100: "OS not supported yet");
101: }
102: }
103: }
104:
105: /**
106: * Get value of environment property
107: * @param key property name
108: * @return String value of environment property
109: */
110: public static String getEnvVar(String key) {
111: Properties p = getEnvVars();
112: return p.getProperty(key);
113: }
114:
115: /**
116: * getRawEnvVars returns an array of lines read from the shell.
117: * @return String[] lines of the shell
118: */
119: public static String[] getRawEnvVars() {
120: getEnvVars();
121: return rawVars.toArray(new String[0]);
122: }
123: }
|