001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer(s): Francois Waeselynck
022: * Contributor(s): Benoit Pelletier
023: *
024: * --------------------------------------------------------------------------
025: * $Id: $
026: * --------------------------------------------------------------------------
027: */package org.objectweb.common;
028:
029: import java.io.BufferedReader;
030: import java.io.IOException;
031: import java.io.InputStreamReader; //import java.util.Iterator;
032: import java.util.Properties; //import java.util.Map.Entry;
033: import org.objectweb.jonas.common.Log;
034: import org.objectweb.util.monolog.api.BasicLevel;
035: import org.objectweb.util.monolog.api.Logger;
036:
037: /**
038: * This class provides some utilities for the windows environment
039: */
040: public class WinSysEnv {
041:
042: /**
043: * Maintains an image of the environment
044: */
045: private static Properties env = null;
046:
047: /**
048: * Logger
049: */
050: private static Logger logger = Log.getLogger(WinSysEnv.class
051: .getName());
052:
053: /**
054: * Get a variable from the windows env
055: * @param var variable name
056: * @return the variable value
057: */
058: public static String get(String var) {
059: if (env == null)
060: load();
061: String value = null;
062: if (env != null)
063: value = (String) env.getProperty(var);
064: return value;
065: }
066:
067: /**
068: * Get the windows env
069: * @return properties
070: */
071: public static Properties getEnv() {
072: if (env == null)
073: load();
074: return env;
075: }
076:
077: /**
078: * Get the windows environment
079: *
080: */
081: private static void load() {
082:
083: if (env == null) {
084: Properties props = new Properties();
085: Runtime rt = Runtime.getRuntime();
086: Process proc = null;
087: try {
088: proc = rt.exec("cmd /c SET");
089: BufferedReader r = new BufferedReader(
090: new InputStreamReader(proc.getInputStream()));
091: String ln = null;
092: while ((ln = r.readLine()) != null) {
093: //System.out.println(ln);
094: int ix = ln.indexOf("=");
095: props.setProperty(ln.substring(0, ix), ln
096: .substring(ix + 1));
097: }
098: if (proc != null)
099: proc.waitFor();
100: } catch (IOException e1) {
101: logger.log(BasicLevel.ERROR,
102: "Cannot get System environment : " + e1);
103: } catch (InterruptedException e) {
104: // Do nothing
105: }
106: env = props;
107: }
108: }
109:
110: /**
111: * main
112: * For test purposes
113: * @param args
114: */
115: /*
116: public static void main(String[] args) {
117:
118: Properties props = System.getProperties();
119:
120: // props.list(System.out);
121:
122: Iterator it = props.entrySet().iterator();
123: while (it.hasNext()) {
124: Entry p = (Entry) it.next();
125: System.out.println(p.getKey() + "=" + p.getValue());
126:
127: }
128: System.out.println("\n=========\nWinSysEnv\n=========");
129: System.out.println("SystemRoot="+WinSysEnv.get("SystemRoot"));
130: props = WinSysEnv.getEnv();
131: it = props.entrySet().iterator();
132: while (it.hasNext()) {
133: Entry p = (Entry) it.next();
134: System.out.println(p.getKey() + "=" + p.getValue());
135: }
136: }
137: */
138:
139: }
|