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.Enumeration;
23 import java.util.Properties;
24
25 import org.apache.logging.log4j.Logger;
26 import org.apache.logging.log4j.status.StatusLogger;
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 static final Logger LOGGER = StatusLogger.getLogger();
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
59
60
61
62
63 static Properties loadClose(final InputStream in, final Object source) {
64 final Properties props = new Properties();
65 if (null != in) {
66 try {
67 props.load(in);
68 } catch (final IOException e) {
69 LOGGER.error("Unable to read {}", source, e);
70 } finally {
71 try {
72 in.close();
73 } catch (final IOException e) {
74 LOGGER.error("Unable to close {}", source, e);
75 }
76 }
77 }
78 return props;
79 }
80
81
82
83
84
85
86
87
88 public PropertiesUtil(final String propsLocn) {
89 final ClassLoader loader = LoaderUtil.getThreadContextClassLoader();
90 @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
91 final
92 Properties properties = new Properties();
93 try {
94 final Enumeration<URL> enumeration = loader.getResources(propsLocn);
95 while (enumeration.hasMoreElements()) {
96 final URL url = enumeration.nextElement();
97 final InputStream in = url.openStream();
98 try {
99 properties.load(in);
100 } catch (final IOException ioe) {
101 LOGGER.error("Unable to read {}", url.toString());
102 } finally {
103 try {
104 in.close();
105 } catch (final IOException ioe) {
106 LOGGER.error("Unable to close {}", url.toString(), ioe);
107 }
108 }
109
110 }
111
112 } catch (final IOException ioe) {
113 LOGGER.error("Unable to access {}", propsLocn, ioe);
114 }
115 this.props = properties;
116 }
117
118
119
120
121
122
123 public static PropertiesUtil getProperties() {
124 return LOG4J_PROPERTIES;
125 }
126
127
128
129
130
131
132
133 public String getStringProperty(final String name) {
134 String prop = null;
135 try {
136 prop = System.getProperty(name);
137 } catch (final SecurityException ignored) {
138
139 }
140 return prop == null ? props.getProperty(name) : prop;
141 }
142
143
144
145
146
147
148
149
150
151 public int getIntegerProperty(final String name, final int defaultValue) {
152 String prop = null;
153 try {
154 prop = System.getProperty(name);
155 } catch (final SecurityException ignored) {
156
157 }
158 if (prop == null) {
159 prop = props.getProperty(name);
160 }
161 if (prop != null) {
162 try {
163 return Integer.parseInt(prop);
164 } catch (final Exception ex) {
165 return defaultValue;
166 }
167 }
168 return defaultValue;
169 }
170
171
172
173
174
175
176
177
178
179 public long getLongProperty(final String name, final long defaultValue) {
180 String prop = null;
181 try {
182 prop = System.getProperty(name);
183 } catch (final SecurityException ignored) {
184
185 }
186 if (prop == null) {
187 prop = props.getProperty(name);
188 }
189 if (prop != null) {
190 try {
191 return Long.parseLong(prop);
192 } catch (final Exception ex) {
193 return defaultValue;
194 }
195 }
196 return defaultValue;
197 }
198
199
200
201
202
203
204
205
206 public String getStringProperty(final String name, final String defaultValue) {
207 final String prop = getStringProperty(name);
208 return (prop == null) ? defaultValue : prop;
209 }
210
211
212
213
214
215
216
217
218
219 public boolean getBooleanProperty(final String name) {
220 return getBooleanProperty(name, false);
221 }
222
223
224
225
226
227
228
229
230 public boolean getBooleanProperty(final String name, final boolean defaultValue) {
231 final String prop = getStringProperty(name);
232 return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
233 }
234
235
236
237
238
239 public static Properties getSystemProperties() {
240 try {
241 return new Properties(System.getProperties());
242 } catch (final SecurityException ex) {
243 LOGGER.error("Unable to access system properties.", ex);
244
245 return new Properties();
246 }
247 }
248 }