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.Properties;
25
26 import org.apache.logging.log4j.Logger;
27 import org.apache.logging.log4j.status.StatusLogger;
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 static final Logger LOGGER = StatusLogger.getLogger();
41
42 private final Properties props;
43
44
45
46
47
48
49 public PropertiesUtil(final Properties props) {
50 this.props = props;
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64 static Properties loadClose(final InputStream in, final Object source) {
65 final Properties props = new Properties();
66 if (null != in) {
67 try {
68 props.load(in);
69 } catch (final IOException e) {
70 LOGGER.error("Unable to read {}", source, e);
71 } finally {
72 try {
73 in.close();
74 } catch (final IOException e) {
75 LOGGER.error("Unable to close {}", source, e);
76 }
77 }
78 }
79 return props;
80 }
81
82
83
84
85
86
87
88
89 public PropertiesUtil(final String propertiesFileName) {
90 @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
91 final Properties properties = new Properties();
92 for (final URL url : LoaderUtil.findResources(propertiesFileName)) {
93 InputStream in = null;
94 try {
95 in = url.openStream();
96 properties.load(in);
97 } catch (final IOException ioe) {
98 LOGGER.error("Unable to read {}", url.toString(), ioe);
99 } finally {
100 if (in != null) {
101 try {
102 in.close();
103 } catch (final IOException ioe) {
104 LOGGER.error("Unable to close {}", url.toString(), ioe);
105 }
106 }
107 }
108 }
109 this.props = properties;
110 }
111
112
113
114
115
116
117 public static PropertiesUtil getProperties() {
118 return LOG4J_PROPERTIES;
119 }
120
121
122
123
124
125
126
127 public String getStringProperty(final String name) {
128 String prop = null;
129 try {
130 prop = System.getProperty(name);
131 } catch (final SecurityException ignored) {
132
133 }
134 return prop == null ? props.getProperty(name) : prop;
135 }
136
137
138
139
140
141
142
143
144
145 public int getIntegerProperty(final String name, final int defaultValue) {
146 String prop = null;
147 try {
148 prop = System.getProperty(name);
149 } catch (final SecurityException ignored) {
150
151 }
152 if (prop == null) {
153 prop = props.getProperty(name);
154 }
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
173 public long getLongProperty(final String name, final long defaultValue) {
174 String prop = null;
175 try {
176 prop = System.getProperty(name);
177 } catch (final SecurityException ignored) {
178
179 }
180 if (prop == null) {
181 prop = props.getProperty(name);
182 }
183 if (prop != null) {
184 try {
185 return Long.parseLong(prop);
186 } catch (final Exception ignored) {
187 return defaultValue;
188 }
189 }
190 return defaultValue;
191 }
192
193
194
195
196
197
198
199
200 public String getStringProperty(final String name, final String defaultValue) {
201 final String prop = getStringProperty(name);
202 return (prop == null) ? defaultValue : prop;
203 }
204
205
206
207
208
209
210
211
212
213 public boolean getBooleanProperty(final String name) {
214 return getBooleanProperty(name, false);
215 }
216
217
218
219
220
221
222
223
224 public boolean getBooleanProperty(final String name, final boolean defaultValue) {
225 final String prop = getStringProperty(name);
226 return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
227 }
228
229
230
231
232
233 public static Properties getSystemProperties() {
234 try {
235 return new Properties(System.getProperties());
236 } catch (final SecurityException ex) {
237 LOGGER.error("Unable to access system properties.", ex);
238
239 return new Properties();
240 }
241 }
242
243
244
245
246
247
248
249
250 public static Properties extractSubset(Properties properties, String prefix) {
251 Properties subset = new Properties();
252
253 if (prefix == null || prefix.length() == 0) {
254 return subset;
255 }
256
257 String prefixToMatch = prefix.charAt(prefix.length() - 1) != '.' ? prefix + '.' : prefix;
258
259 List<String> keys = new ArrayList<>();
260
261 for (String key : properties.stringPropertyNames()) {
262 if (key.startsWith(prefixToMatch)) {
263 subset.setProperty(key.substring(prefixToMatch.length()), properties.getProperty(key));
264 keys.add(key);
265 }
266 }
267 for (String key : keys) {
268 properties.remove(key);
269 }
270
271 return subset;
272 }
273 }