KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > externalprocess > EnvironmentReader


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.externalprocess;
11
12 import java.io.ByteArrayOutputStream JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.Properties JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 /** Reader for importing the OS Environment properties into java.
19  * java.lang.System.getProperties() has only some environment info
20  *
21  * @todo more OS support
22  *
23  * @author Nico Klasens (Finalist IT Group)
24  * @version $Id: EnvironmentReader.java,v 1.4 2003/09/01 13:29:47 pierre Exp $
25  * @since MMBase-1.6
26  */

27 public class EnvironmentReader {
28     private static Properties JavaDoc envVars = null;
29     private static Vector JavaDoc rawVars = null;
30
31     /**
32      * Get value of environment properties
33      * @return Properties environment
34      */

35     public static Properties JavaDoc getEnvVars() {
36
37         if (null != envVars)
38             return envVars;
39
40         envVars = new Properties JavaDoc();
41         rawVars = new Vector JavaDoc(32);
42         String JavaDoc lineSeparator = System.getProperty("line.separator");
43
44         String JavaDoc command = getEnvCommand();
45
46         CommandLauncher launcher = new CommandLauncher("EnvironmentReader");
47         ByteArrayOutputStream JavaDoc outputStream = new ByteArrayOutputStream JavaDoc();
48         try {
49             launcher.execute(command);
50             launcher.waitAndRead(outputStream, null);
51
52             String JavaDoc envStr = outputStream.toString();
53             StringTokenizer JavaDoc strTokens = new StringTokenizer JavaDoc(envStr, lineSeparator);
54
55             String JavaDoc line;
56             while (strTokens.hasMoreTokens()) {
57                 line = strTokens.nextToken();
58
59                 rawVars.add(line);
60                 int idx = line.indexOf('=');
61                 if (idx != -1) {
62                     String JavaDoc key = line.substring(0, idx);
63                     String JavaDoc value = line.substring(idx + 1);
64                     envVars.setProperty(key, value);
65                 } else {
66                     envVars.setProperty(line, "");
67                 }
68             }
69         } catch (ProcessException e) {} finally {
70             try {
71                 if (outputStream != null) {
72                     outputStream.close();
73                 }
74             } catch (IOException JavaDoc e) {}
75         }
76         rawVars.trimToSize();
77         return envVars;
78     }
79
80     /**
81      * Command string to get OS Environment properties
82      * @return String
83      */

84     public static String JavaDoc getEnvCommand() {
85         String JavaDoc OS = System.getProperty("os.name").toLowerCase();
86         if (OS.indexOf("windows 9") > -1) {
87             return "command.com /c set";
88         } else {
89             if ((OS.indexOf("nt") > -1) || (OS.indexOf("windows 2000") > -1) || (OS.indexOf("windows xp") > -1)) {
90                 return "cmd.exe /c set";
91             } else {
92                 throw new UnsupportedOperationException JavaDoc("OS not supported yet");
93             }
94         }
95     }
96
97     /**
98      * Get value of environment property
99      * @param key property name
100      * @return String value of environment property
101      */

102     public static String JavaDoc getEnvVar(String JavaDoc key) {
103         Properties JavaDoc p = getEnvVars();
104         return p.getProperty(key);
105     }
106
107     /**
108      * getRawEnvVars returns an array of lines read from the shell.
109      * @return String[] lines of the shell
110      */

111     public static String JavaDoc[] getRawEnvVars() {
112         getEnvVars();
113         return (String JavaDoc[])rawVars.toArray(new String JavaDoc[0]);
114     }
115 }
116
Popular Tags