1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.util;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.nio.charset.Charset;
23 import java.nio.charset.UnsupportedCharsetException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.concurrent.ConcurrentHashMap;
29
30
31
32
33
34
35
36
37 public final class PropertiesUtil {
38
39 private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil("log4j2.component.properties");
40
41 private final Properties props;
42
43
44
45
46
47
48 public PropertiesUtil(final Properties props) {
49 this.props = props;
50 }
51
52
53
54
55
56
57
58 public PropertiesUtil(final String propertiesFileName) {
59 final Properties properties = new Properties();
60 for (final URL url : LoaderUtil.findResources(propertiesFileName)) {
61 try (final InputStream in = url.openStream()) {
62 properties.load(in);
63 } catch (final IOException ioe) {
64 LowLevelLogUtil.logException("Unable to read " + url.toString(), ioe);
65 }
66 }
67 this.props = properties;
68 }
69
70
71
72
73
74
75
76
77 static Properties loadClose(final InputStream in, final Object source) {
78 final Properties props = new Properties();
79 if (null != in) {
80 try {
81 props.load(in);
82 } catch (final IOException e) {
83 LowLevelLogUtil.logException("Unable to read " + source, e);
84 } finally {
85 try {
86 in.close();
87 } catch (final IOException e) {
88 LowLevelLogUtil.logException("Unable to close " + source, e);
89 }
90 }
91 }
92 return props;
93 }
94
95
96
97
98
99
100 public static PropertiesUtil getProperties() {
101 return LOG4J_PROPERTIES;
102 }
103
104
105
106
107
108
109 public boolean hasProperty(final String name) {
110 return System.getProperties().containsKey(name) || props.containsKey(name);
111 }
112
113
114
115
116
117
118
119
120
121 public boolean getBooleanProperty(final String name) {
122 return getBooleanProperty(name, false);
123 }
124
125
126
127
128
129
130
131
132 public boolean getBooleanProperty(final String name, final boolean defaultValue) {
133 final String prop = getStringProperty(name);
134 return prop == null ? defaultValue : "true".equalsIgnoreCase(prop);
135 }
136
137
138
139
140
141
142
143
144
145 public boolean getBooleanProperty(final String name, final boolean defaultValueIfAbsent,
146 final boolean defaultValueIfPresent) {
147 final String prop = getStringProperty(name);
148 return prop == null ? defaultValueIfAbsent
149 : prop.isEmpty() ? defaultValueIfPresent : "true".equalsIgnoreCase(prop);
150 }
151
152
153
154
155
156
157
158 public Charset getCharsetProperty(final String name) {
159 return getCharsetProperty(name, Charset.defaultCharset());
160 }
161
162
163
164
165
166
167
168
169 public Charset getCharsetProperty(final String name, final Charset defaultValue) {
170 final String prop = getStringProperty(name);
171 try {
172 return prop == null ? defaultValue : Charset.forName(prop);
173 } catch (UnsupportedCharsetException e) {
174 LowLevelLogUtil.logException("Unable to get Charset '" + name + "', using default " + defaultValue, e);
175 return defaultValue;
176 }
177 }
178
179
180
181
182
183
184
185
186 public double getDoubleProperty(final String name, final double defaultValue) {
187 final String prop = getStringProperty(name);
188 if (prop != null) {
189 try {
190 return Double.parseDouble(prop);
191 } catch (final Exception ignored) {
192 return defaultValue;
193 }
194 }
195 return defaultValue;
196 }
197
198
199
200
201
202
203
204
205
206 public int getIntegerProperty(final String name, final int defaultValue) {
207 final String prop = getStringProperty(name);
208 if (prop != null) {
209 try {
210 return Integer.parseInt(prop);
211 } catch (final Exception ignored) {
212 return defaultValue;
213 }
214 }
215 return defaultValue;
216 }
217
218
219
220
221
222
223
224
225 public long getLongProperty(final String name, final long defaultValue) {
226 final String prop = getStringProperty(name);
227 if (prop != null) {
228 try {
229 return Long.parseLong(prop);
230 } catch (final Exception ignored) {
231 return defaultValue;
232 }
233 }
234 return defaultValue;
235 }
236
237
238
239
240
241
242
243 public String getStringProperty(final String name) {
244 String prop = null;
245 try {
246 prop = System.getProperty(name);
247 } catch (final SecurityException ignored) {
248
249 }
250 return prop == null ? props.getProperty(name) : prop;
251 }
252
253
254
255
256
257
258
259
260 public String getStringProperty(final String name, final String defaultValue) {
261 final String prop = getStringProperty(name);
262 return (prop == null) ? defaultValue : prop;
263 }
264
265
266
267
268
269
270 public static Properties getSystemProperties() {
271 try {
272 return new Properties(System.getProperties());
273 } catch (final SecurityException ex) {
274 LowLevelLogUtil.logException("Unable to access system properties.", ex);
275
276 return new Properties();
277 }
278 }
279
280
281
282
283
284
285
286
287
288 public static Properties extractSubset(final Properties properties, final String prefix) {
289 final Properties subset = new Properties();
290
291 if (prefix == null || prefix.length() == 0) {
292 return subset;
293 }
294
295 final String prefixToMatch = prefix.charAt(prefix.length() - 1) != '.' ? prefix + '.' : prefix;
296
297 final List<String> keys = new ArrayList<>();
298
299 for (final String key : properties.stringPropertyNames()) {
300 if (key.startsWith(prefixToMatch)) {
301 subset.setProperty(key.substring(prefixToMatch.length()), properties.getProperty(key));
302 keys.add(key);
303 }
304 }
305 for (final String key : keys) {
306 properties.remove(key);
307 }
308
309 return subset;
310 }
311
312
313
314
315
316
317
318
319
320 public static Map<String, Properties> partitionOnCommonPrefixes(final Properties properties) {
321 final Map<String, Properties> parts = new ConcurrentHashMap<>();
322 for (final String key : properties.stringPropertyNames()) {
323 final String prefix = key.substring(0, key.indexOf('.'));
324 if (!parts.containsKey(prefix)) {
325 parts.put(prefix, new Properties());
326 }
327 parts.get(prefix).setProperty(key.substring(key.indexOf('.') + 1), properties.getProperty(key));
328 }
329 return parts;
330 }
331
332
333
334
335
336 public boolean isOsWindows() {
337 return getStringProperty("os.name").startsWith("Windows");
338 }
339
340 }