001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    
019    import java.io.IOException;
020    import java.io.InputStreamReader;
021    
022    import java.util.Enumeration;
023    import java.util.Properties;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class SystemEnv {
029    
030            public static Properties getProperties() {
031                    Properties props = new Properties();
032    
033                    try {
034                            Runtime runtime = Runtime.getRuntime();
035                            Process process = null;
036    
037                            String osName = System.getProperty("os.name").toLowerCase();
038    
039                            if (osName.indexOf("windows ") > -1) {
040                                    if (osName.indexOf("windows 9") > -1) {
041                                            process = runtime.exec("command.com /c set");
042                                    }
043                                    else {
044                                            process = runtime.exec("cmd.exe /c set");
045                                    }
046                            }
047                            else {
048                                    process = runtime.exec("env");
049                            }
050    
051                            UnsyncBufferedReader unsyncBufferedReader =
052                                    new UnsyncBufferedReader(
053                                            new InputStreamReader(process.getInputStream()));
054    
055                            String line;
056    
057                            while ((line = unsyncBufferedReader.readLine()) != null) {
058                                    int pos = line.indexOf(CharPool.EQUAL);
059    
060                                    if (pos != -1) {
061                                            String key = line.substring(0, pos);
062                                            String value = line.substring(pos + 1);
063    
064                                            props.setProperty(key, value);
065                                    }
066                            }
067                    }
068                    catch (IOException ioe) {
069                            ioe.printStackTrace();
070                    }
071    
072                    return props;
073            }
074    
075            public static void setProperties(Properties props) {
076                    Properties envProps = getProperties();
077    
078                    Enumeration<String> enu = (Enumeration<String>)envProps.propertyNames();
079    
080                    while (enu.hasMoreElements()) {
081                            String key = enu.nextElement();
082    
083                            props.setProperty("env." + key, (String)envProps.get(key));
084                    }
085            }
086    
087    }