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.util.Properties;
22
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.status.StatusLogger;
25
26
27
28
29 public class PropertiesUtil {
30
31 private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil("log4j2.component.properties");
32
33 private static final Logger LOGGER = StatusLogger.getLogger();
34
35 private final Properties props;
36
37 public PropertiesUtil(final Properties props) {
38 this.props = props;
39 }
40
41
42
43
44
45
46
47
48
49
50
51
52 static Properties loadClose(InputStream in, Object source) {
53 Properties props = new Properties();
54 if (null != in) {
55 try {
56 props.load(in);
57 } catch (final IOException e) {
58 LOGGER.error("Unable to read " + source, e);
59 } finally {
60 try {
61 in.close();
62 } catch (final IOException e) {
63 LOGGER.error("Unable to close " + source, e);
64 }
65 }
66 }
67 return props;
68 }
69
70 public PropertiesUtil(final String propsLocn) {
71 final ClassLoader loader = ProviderUtil.findClassLoader();
72 final InputStream in = loader.getResourceAsStream(propsLocn);
73 this.props = loadClose(in, propsLocn);
74 }
75
76 public static PropertiesUtil getProperties() {
77 return LOG4J_PROPERTIES;
78 }
79
80 public String getStringProperty(final String name) {
81 String prop = null;
82 try {
83 prop = System.getProperty(name);
84 } catch (final SecurityException e) {
85
86 }
87 return prop == null ? props.getProperty(name) : prop;
88 }
89
90
91 public int getIntegerProperty(final String name, final int defaultValue) {
92 String prop = null;
93 try {
94 prop = System.getProperty(name);
95 } catch (final SecurityException e) {
96
97 }
98 if (prop == null) {
99 prop = props.getProperty(name);
100 }
101 if (prop != null) {
102 try {
103 return Integer.parseInt(prop);
104 } catch (final Exception ex) {
105 return defaultValue;
106 }
107 }
108 return defaultValue;
109 }
110
111
112 public long getLongProperty(final String name, final long defaultValue) {
113 String prop = null;
114 try {
115 prop = System.getProperty(name);
116 } catch (final SecurityException e) {
117
118 }
119 if (prop == null) {
120 prop = props.getProperty(name);
121 }
122 if (prop != null) {
123 try {
124 return Long.parseLong(prop);
125 } catch (final Exception ex) {
126 return defaultValue;
127 }
128 }
129 return defaultValue;
130 }
131
132 public String getStringProperty(final String name, final String defaultValue) {
133 final String prop = getStringProperty(name);
134 return (prop == null) ? defaultValue : prop;
135 }
136
137 public boolean getBooleanProperty(final String name) {
138 return getBooleanProperty(name, false);
139 }
140
141 public boolean getBooleanProperty(final String name, final boolean defaultValue) {
142 final String prop = getStringProperty(name);
143 return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
144 }
145
146
147
148
149
150 public static Properties getSystemProperties() {
151 try {
152 return new Properties(System.getProperties());
153 } catch (final SecurityException ex) {
154 StatusLogger.getLogger().error("Unable to access system properties.");
155
156 return new Properties();
157 }
158 }
159 }