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