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