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