001
014
015 package com.liferay.portal.kernel.util;
016
017 import java.io.IOException;
018 import java.io.InputStream;
019
020 import java.net.URL;
021
022 import java.util.ArrayList;
023 import java.util.Enumeration;
024 import java.util.List;
025 import java.util.Map;
026 import java.util.Map.Entry;
027 import java.util.Properties;
028 import java.util.concurrent.ConcurrentHashMap;
029
030
036 public class SystemProperties {
037
038
041 @Deprecated
042 public static final String SYSTEM_PROPERTIES_FINAL =
043 "system.properties.final";
044
045
048 @Deprecated
049 public static final String SYSTEM_PROPERTIES_LOAD =
050 "system.properties.load";
051
052 public static final String SYSTEM_PROPERTIES_QUIET =
053 "system.properties.quiet";
054
055 public static final String SYSTEM_PROPERTIES_SET = "system.properties.set";
056
057 public static final String SYSTEM_PROPERTIES_SET_OVERRIDE =
058 "system.properties.set.override";
059
060 public static final String TMP_DIR = "java.io.tmpdir";
061
062 public static String get(String key) {
063 String value = _properties.get(key);
064
065 if (value == null) {
066 value = System.getProperty(key);
067 }
068
069 return value;
070 }
071
072
075 @Deprecated
076 public static String[] getArray(String key) {
077 String value = get(key);
078
079 if (value == null) {
080 return new String[0];
081 }
082 else {
083 return StringUtil.split(value);
084 }
085 }
086
087 public static Properties getProperties() {
088 return PropertiesUtil.fromMap(_properties);
089 }
090
091
094 @Deprecated
095 public static void reload() {
096 if (_loaded) {
097 return;
098 }
099
100 Properties properties = new Properties();
101
102 Thread currentThread = Thread.currentThread();
103
104 ClassLoader classLoader = currentThread.getContextClassLoader();
105
106 boolean systemPropertiesQuiet = GetterUtil.getBoolean(
107 System.getProperty(SYSTEM_PROPERTIES_QUIET));
108
109
110
111 try {
112 URL url = classLoader.getResource("system.properties");
113
114 if (url != null) {
115 InputStream inputStream = url.openStream();
116
117 properties.load(inputStream);
118
119 inputStream.close();
120
121 if (!systemPropertiesQuiet) {
122 System.out.println("Loading " + url);
123 }
124 }
125 }
126 catch (Exception e) {
127 e.printStackTrace();
128 }
129
130
131
132 try {
133 URL url = classLoader.getResource("system-ext.properties");
134
135 if (url != null) {
136 _loaded = true;
137
138 InputStream inputStream = url.openStream();
139
140 properties.load(inputStream);
141
142 inputStream.close();
143
144 if (!systemPropertiesQuiet) {
145 System.out.println("Loading " + url);
146 }
147 }
148 }
149 catch (Exception e) {
150 e.printStackTrace();
151 }
152
153
154
155 SystemEnv.setProperties(properties);
156
157
158
159 boolean systemPropertiesLoad = GetterUtil.getBoolean(
160 System.getProperty(SYSTEM_PROPERTIES_LOAD), true);
161
162 boolean systemPropertiesFinal = GetterUtil.getBoolean(
163 System.getProperty(SYSTEM_PROPERTIES_FINAL), true);
164
165 if (systemPropertiesLoad) {
166 Enumeration<String> enu =
167 (Enumeration<String>)properties.propertyNames();
168
169 while (enu.hasMoreElements()) {
170 String key = enu.nextElement();
171
172 if (systemPropertiesFinal ||
173 Validator.isNull(System.getProperty(key))) {
174
175 System.setProperty(key, properties.getProperty(key));
176 }
177 }
178 }
179
180 _properties = new ConcurrentHashMap<String, String>();
181
182
183
184
185 PropertiesUtil.fromProperties(properties, _properties);
186 }
187
188 public static void set(String key, String value) {
189 System.setProperty(key, value);
190
191 _properties.put(key, value);
192 }
193
194
197 @Deprecated
198 private static boolean _loaded;
199 private static Map<String, String> _properties;
200
201 static {
202 Properties properties = new Properties();
203
204 Thread currentThread = Thread.currentThread();
205
206 ClassLoader classLoader = currentThread.getContextClassLoader();
207
208 List<URL> urls = null;
209
210 if (!GetterUtil.getBoolean(
211 System.getProperty(SYSTEM_PROPERTIES_QUIET))) {
212
213 urls = new ArrayList<URL>();
214 }
215
216
217
218 try {
219 URL url = classLoader.getResource("system.properties");
220
221 if (url != null) {
222 InputStream inputStream = url.openStream();
223
224 properties.load(inputStream);
225
226 inputStream.close();
227
228 if (urls != null) {
229 urls.add(url);
230 }
231 }
232 }
233 catch (IOException ioe) {
234 throw new ExceptionInInitializerError(ioe);
235 }
236
237
238
239 try {
240 URL url = classLoader.getResource("system-ext.properties");
241
242 if (url != null) {
243 InputStream inputStream = url.openStream();
244
245 properties.load(inputStream);
246
247 inputStream.close();
248
249 if (urls != null) {
250 urls.add(url);
251 }
252 }
253 }
254 catch (IOException ioe) {
255 throw new ExceptionInInitializerError(ioe);
256 }
257
258
259
260 SystemEnv.setProperties(properties);
261
262
263
264 if (GetterUtil.getBoolean(
265 System.getProperty(SYSTEM_PROPERTIES_SET), true)) {
266
267 boolean systemPropertiesSetOverride = GetterUtil.getBoolean(
268 System.getProperty(SYSTEM_PROPERTIES_SET_OVERRIDE), true);
269
270 for (Entry<Object, Object> entry : properties.entrySet()) {
271 String key = String.valueOf(entry.getKey());
272
273 if (systemPropertiesSetOverride ||
274 Validator.isNull(System.getProperty(key))) {
275
276 System.setProperty(key, String.valueOf(entry.getValue()));
277 }
278 }
279 }
280
281 _properties = new ConcurrentHashMap<String, String>();
282
283
284
285
286 PropertiesUtil.fromProperties(properties, _properties);
287
288 if (urls != null) {
289 for (URL url : urls) {
290 System.out.println("Loading " + url);
291 }
292 }
293 }
294
295 }