1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.PropertiesUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.SystemEnv;
29 import com.liferay.portal.kernel.util.Validator;
30
31 import java.io.InputStream;
32
33 import java.net.URL;
34
35 import java.util.Enumeration;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.concurrent.ConcurrentHashMap;
39
40
48 public class SystemProperties {
49
50 public static final String SYSTEM_PROPERTIES_LOAD =
51 "system.properties.load";
52
53 public static final String SYSTEM_PROPERTIES_FINAL =
54 "system.properties.final";
55
56 public static final String TMP_DIR = "java.io.tmpdir";
57
58 public static String get(String key) {
59 String value = _instance._props.get(key);
60
61 if (value == null) {
62 value = System.getProperty(key);
63 }
64
65 return value;
66 }
67
68 public static void set(String key, String value) {
69 System.setProperty(key, value);
70
71 _instance._props.put(key, value);
72 }
73
74 public static String[] getArray(String key) {
75 String value = get(key);
76
77 if (value == null) {
78 return new String[0];
79 }
80 else {
81 return StringUtil.split(value);
82 }
83 }
84
85 public static Properties getProperties() {
86 return PropertiesUtil.fromMap(_instance._props);
87 }
88
89 private SystemProperties() {
90 Properties p = new Properties();
91
92 ClassLoader classLoader = getClass().getClassLoader();
93
94
96 try {
97 URL url = classLoader.getResource("system.properties");
98
99 if (url != null) {
100 InputStream is = url.openStream();
101
102 p.load(is);
103
104 is.close();
105
106 System.out.println("Loading " + url);
107 }
108 }
109 catch (Exception e) {
110 e.printStackTrace();
111 }
112
113
115 try {
116 URL url = classLoader.getResource("system-ext.properties");
117
118 if (url != null) {
119 InputStream is = url.openStream();
120
121 p.load(is);
122
123 is.close();
124
125 System.out.println("Loading " + url);
126 }
127 }
128 catch (Exception e) {
129 e.printStackTrace();
130 }
131
132
134 SystemEnv.setProperties(p);
135
136
138 boolean systemPropertiesLoad = GetterUtil.getBoolean(
139 System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
140
141 boolean systemPropertiesFinal = GetterUtil.getBoolean(
142 System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
143
144 if (systemPropertiesLoad) {
145 Enumeration<String> enu = (Enumeration<String>)p.propertyNames();
146
147 while (enu.hasMoreElements()) {
148 String key = enu.nextElement();
149
150 if (systemPropertiesFinal ||
151 Validator.isNull(System.getProperty(key))) {
152
153 System.setProperty(key, p.getProperty(key));
154 }
155 }
156 }
157
158 _props = new ConcurrentHashMap<String, String>();
159
160
163 PropertiesUtil.fromProperties(p, _props);
164 }
165
166 private static SystemProperties _instance = new SystemProperties();
167
168 private Map<String, String> _props;
169
170 }