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