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