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(InputStream in, Object source) {
58 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 Properties properties = new Properties();
79 try {
80 Enumeration<URL> enumeration = loader.getResources(propsLocn);
81 while (enumeration.hasMoreElements()) {
82 final URL url = enumeration.nextElement();
83 final InputStream in = url.openStream();
84 try {
85 properties.load(in);
86 } catch (IOException ioe) {
87 LOGGER.error("Unable to read {}", url.toString());
88 } finally {
89 try {
90 in.close();
91 } catch (IOException ioe) {
92 LOGGER.error("Unable to close {}", url.toString(), ioe);
93 }
94 }
95
96 }
97
98 } catch (IOException ioe) {
99 LOGGER.error("Unable to access {}", propsLocn, ioe);
100 }
101 this.props = properties;
102 }
103
104 public static PropertiesUtil getProperties() {
105 return LOG4J_PROPERTIES;
106 }
107
108 public String getStringProperty(final String name) {
109 String prop = null;
110 try {
111 prop = System.getProperty(name);
112 } catch (final SecurityException ignored) {
113
114 }
115 return prop == null ? props.getProperty(name) : prop;
116 }
117
118
119 public int getIntegerProperty(final String name, final int defaultValue) {
120 String prop = null;
121 try {
122 prop = System.getProperty(name);
123 } catch (final SecurityException ignored) {
124
125 }
126 if (prop == null) {
127 prop = props.getProperty(name);
128 }
129 if (prop != null) {
130 try {
131 return Integer.parseInt(prop);
132 } catch (final Exception ex) {
133 return defaultValue;
134 }
135 }
136 return defaultValue;
137 }
138
139
140 public long getLongProperty(final String name, final long 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 Long.parseLong(prop);
153 } catch (final Exception ex) {
154 return defaultValue;
155 }
156 }
157 return defaultValue;
158 }
159
160 public String getStringProperty(final String name, final String defaultValue) {
161 final String prop = getStringProperty(name);
162 return (prop == null) ? defaultValue : prop;
163 }
164
165 public boolean getBooleanProperty(final String name) {
166 return getBooleanProperty(name, false);
167 }
168
169 public boolean getBooleanProperty(final String name, final boolean defaultValue) {
170 final String prop = getStringProperty(name);
171 return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
172 }
173
174
175
176
177
178 public static Properties getSystemProperties() {
179 try {
180 return new Properties(System.getProperties());
181 } catch (final SecurityException ex) {
182 LOGGER.error("Unable to access system properties.", ex);
183
184 return new Properties();
185 }
186 }
187 }