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