1
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
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(CharPool.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 }