1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
18  
19  import java.io.IOException;
20  import java.io.InputStreamReader;
21  
22  import java.util.Enumeration;
23  import java.util.Properties;
24  
25  /**
26   * <a href="SystemEnv.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class SystemEnv {
31  
32      public static Properties getProperties() {
33          Properties props = new Properties();
34  
35          try {
36              Runtime runtime = Runtime.getRuntime();
37              Process process = null;
38  
39              String osName = System.getProperty("os.name").toLowerCase();
40  
41              if (osName.indexOf("windows ") > -1) {
42                  if (osName.indexOf("windows 9") > -1) {
43                      process = runtime.exec("command.com /c set");
44                  }
45                  else {
46                      process = runtime.exec("cmd.exe /c set");
47                  }
48              }
49              else {
50                  process = runtime.exec("env");
51              }
52  
53              UnsyncBufferedReader unsyncBufferedReader =
54                  new UnsyncBufferedReader(
55                      new InputStreamReader(process.getInputStream()));
56  
57              String line;
58  
59              while ((line = unsyncBufferedReader.readLine()) != null) {
60                  int pos = line.indexOf(StringPool.EQUAL);
61  
62                  if (pos != -1) {
63                      String key = line.substring(0, pos);
64                      String value = line.substring(pos + 1);
65  
66                      props.setProperty(key, value);
67                  }
68              }
69          }
70          catch (IOException ioe) {
71              ioe.printStackTrace();
72          }
73  
74          return props;
75      }
76  
77      public static void setProperties(Properties props) {
78          Properties envProps = getProperties();
79  
80          Enumeration<String> enu = (Enumeration<String>)envProps.propertyNames();
81  
82          while (enu.hasMoreElements()) {
83              String key = enu.nextElement();
84  
85              props.setProperty("env." + key, (String)envProps.get(key));
86          }
87      }
88  
89  }