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