1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.io.BufferedReader;
23  import java.io.IOException;
24  import java.io.InputStreamReader;
25  
26  import java.util.Enumeration;
27  import java.util.Properties;
28  
29  /**
30   * <a href="SystemEnv.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public class SystemEnv {
36  
37      public static Properties getProperties() {
38          Properties props = new Properties();
39  
40          try {
41              Runtime runtime = Runtime.getRuntime();
42              Process process = null;
43  
44              String osName = System.getProperty("os.name").toLowerCase();
45  
46              if (osName.indexOf("windows ") > -1) {
47                  if (osName.indexOf("windows 9") > -1) {
48                      process = runtime.exec("command.com /c set");
49                  }
50                  else {
51                      process = runtime.exec("cmd.exe /c set");
52                  }
53              }
54              else {
55                  process = runtime.exec("env");
56              }
57  
58              BufferedReader br = new BufferedReader(
59                  new InputStreamReader(process.getInputStream()));
60  
61              String line;
62  
63              while ((line = br.readLine()) != null) {
64                  int pos = line.indexOf(StringPool.EQUAL);
65  
66                  if (pos != -1) {
67                      String key = line.substring(0, pos);
68                      String value = line.substring(pos + 1);
69  
70                      props.setProperty(key, value);
71                  }
72              }
73          }
74          catch (IOException ioe) {
75              ioe.printStackTrace();
76          }
77  
78          return props;
79      }
80  
81      public static void setProperties(Properties props) {
82          Properties envProps = getProperties();
83  
84          Enumeration<String> enu = (Enumeration<String>)envProps.propertyNames();
85  
86          while (enu.hasMoreElements()) {
87              String key = enu.nextElement();
88  
89              props.setProperty("env." + key, (String)envProps.get(key));
90          }
91      }
92  
93  }