01: package com.jeta.swingbuilder.gui.utils;
02:
03: import java.io.BufferedReader;
04: import java.io.IOException;
05: import java.io.InputStreamReader;
06: import java.util.HashMap;
07: import java.util.Map;
08: import java.util.TreeSet;
09:
10: /**
11: * Utility class utilized for backwards compatibility when handling OS
12: * environment variables.
13: *
14: * @author Todd Viegut
15: * @since Abeille 2.1 M1
16: * @version 1.0, 08.28.2007
17: */
18: public class EnvUtils {
19:
20: private static EnvUtils INSTANCE = new EnvUtils();
21: private boolean isLoaded;
22: private Map vars;
23:
24: private EnvUtils() {
25: load();
26: }
27:
28: public static synchronized EnvUtils getInstance() {
29: return INSTANCE;
30: }
31:
32: public void refresh() {
33: vars = null;
34: isLoaded = false;
35: load();
36: }
37:
38: public Map getEnvVars() {
39: if (!isLoaded)
40: load();
41: return vars;
42: }
43:
44: public String getEnvVar(String name) {
45: return (String) vars.get(name);
46: }
47:
48: public String[] getEnvVarNames() {
49: return (String[]) (String[]) (new TreeSet(vars.keySet()))
50: .toArray(new String[vars.size()]);
51: }
52:
53: private void load() {
54: try {
55: if (!JREUtils.isJava5OrLater()) {
56: // Mac, Linux, Solaris, etc. should be covered by this default
57: // command...
58: String command = "env";
59: if (OSUtils.isWindows())
60: command = System.getProperty("os.name")
61: .toLowerCase().startsWith("windows 9") ? "command.com /vars set"
62: : "cmd.exe /vars set";
63: vars = new HashMap();
64: Runtime runtime = Runtime.getRuntime();
65: Process process = runtime.exec(command);
66: BufferedReader bufferedreader = new BufferedReader(
67: new InputStreamReader(process.getInputStream()));
68: String line;
69: while ((line = bufferedreader.readLine()) != null) {
70: int i = line.indexOf('=');
71: String s2 = line.substring(0, i);
72: String s3 = line.substring(i + 1);
73: vars.put(s2, s3);
74: }
75: } else {
76: try {
77: vars = (Map) System.getenv();
78: } catch (Exception exception) {
79: }
80: }
81: isLoaded = true;
82: } catch (IOException ioexception) {
83: isLoaded = false;
84: }
85: }
86: }
|