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 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 public PropertiesUtil(final Properties props) {
43 this.props = props;
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57 static Properties loadClose(final InputStream in, final Object source) {
58 final Properties props = new Properties();
59 if (null != in) {
60 try {
61 props.load(in);
62 } catch (final IOException e) {
63 LOGGER.error("Unable to read {}", source, e);
64 } finally {
65 try {
66 in.close();
67 } catch (final IOException e) {
68 LOGGER.error("Unable to close {}", source, e);
69 }
70 }
71 }
72 return props;
73 }
74
75 public PropertiesUtil(final String propsLocn) {
76 final ClassLoader loader = ProviderUtil.findClassLoader();
77 @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
78 final
79 Properties properties = new Properties();
80 try {
81 final Enumeration<URL> enumeration = loader.getResources(propsLocn);
82 while (enumeration.hasMoreElements()) {
83 final URL url = enumeration.nextElement();
84 final InputStream in = url.openStream();
85 try {
86 properties.load(in);
87 } catch (final IOException ioe) {
88 LOGGER.error("Unable to read {}", url.toString());
89 } finally {
90 try {
91 in.close();
92 } catch (final IOException ioe) {
93 LOGGER.error("Unable to close {}", url.toString(), ioe);
94 }
95 }
96
97 }
98
99 } catch (final IOException ioe) {
100 LOGGER.error("Unable to access {}", propsLocn, ioe);
101 }
102 this.props = properties;
103 }
104
105 public static PropertiesUtil getProperties() {
106 return LOG4J_PROPERTIES;
107 }
108
109 public String getStringProperty(final String name) {
110 String prop = null;
111 try {
112 prop = System.getProperty(name);
113 } catch (final SecurityException ignored) {
114
115 }
116 return prop == null ? props.getProperty(name) : prop;
117 }
118
119
120 public int getIntegerProperty(final String name, final int defaultValue) {
121 String prop = null;
122 try {
123 prop = System.getProperty(name);
124 } catch (final SecurityException ignored) {
125
126 }
127 if (prop == null) {
128 prop = props.getProperty(name);
129 }
130 if (prop != null) {
131 try {
132 return Integer.parseInt(prop);
133 } catch (final Exception ex) {
134 return defaultValue;
135 }
136 }
137 return defaultValue;
138 }
139
140
141 public long getLongProperty(final String name, final long defaultValue) {
142 String prop = null;
143 try {
144 prop = System.getProperty(name);
145 } catch (final SecurityException ignored) {
146
147 }
148 if (prop == null) {
149 prop = props.getProperty(name);
150 }
151 if (prop != null) {
152 try {
153 return Long.parseLong(prop);
154 } catch (final Exception ex) {
155 return defaultValue;
156 }
157 }
158 return defaultValue;
159 }
160
161 public String getStringProperty(final String name, final String defaultValue) {
162 final String prop = getStringProperty(name);
163 return (prop == null) ? defaultValue : prop;
164 }
165
166 public boolean getBooleanProperty(final String name) {
167 return getBooleanProperty(name, false);
168 }
169
170 public boolean getBooleanProperty(final String name, final boolean defaultValue) {
171 final String prop = getStringProperty(name);
172 return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
173 }
174
175
176
177
178
179 public static Properties getSystemProperties() {
180 try {
181 return new Properties(System.getProperties());
182 } catch (final SecurityException ex) {
183 LOGGER.error("Unable to access system properties.", ex);
184
185 return new Properties();
186 }
187 }
188 }