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