1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.qa.glnm.configuration;
21
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.Reader;
28 import java.nio.charset.Charset;
29 import java.util.Enumeration;
30 import java.util.Map.Entry;
31 import java.util.Properties;
32 import java.util.regex.Pattern;
33
34 import org.apache.commons.io.input.ReaderInputStream;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43 public final class PropertiesUtil {
44
45 private static final Charset CHARSET_UTF8 = Charset.forName("utf-8");
46 private static final Logger LOG = LoggerFactory.getLogger(PropertiesUtil.class);
47 private static final String REGEX_WILDCARD = ".*";
48
49 private PropertiesUtil() {
50
51 }
52
53
54
55
56
57
58
59
60
61 public static Properties getAllPropertiesContaining(Properties properties, String searchString) {
62 return getFilteredProperties(properties, Pattern.compile(REGEX_WILDCARD + searchString + REGEX_WILDCARD));
63 }
64
65
66
67
68
69
70
71
72
73 public static Properties getAllPropertiesWithPrefix(Properties properties, String prefix) {
74 return getFilteredProperties(properties, Pattern.compile(prefix + REGEX_WILDCARD));
75 }
76
77
78
79
80
81
82
83
84
85 public static Properties getFilteredProperties(Properties properties, Pattern filter) {
86 Properties filteredProperties = new Properties();
87 for (Entry<Object, Object> property : properties.entrySet()) {
88 Object keyObject = property.getKey();
89 if (keyObject instanceof String) {
90 String key = (String)keyObject;
91 if (filter.matcher(key).matches()) {
92 filteredProperties.put(key, property.getValue());
93 }
94 }
95 else {
96 LOG.debug("Key was not a String when filtering: '" + keyObject + "'");
97 }
98 }
99 return filteredProperties;
100 }
101
102
103
104
105
106
107
108
109
110 public static Properties loadProperties(Properties properties, String filePath) {
111 try {
112 InputStream stream = getInputStream(filePath);
113 if (stream != null) {
114 properties.load(stream);
115 if (LOG.isTraceEnabled()) {
116 Enumeration<?> propertyNames = properties.propertyNames();
117 while (propertyNames.hasMoreElements()) {
118 Object key = propertyNames.nextElement();
119 if (LOG.isTraceEnabled()) {
120 LOG.trace("from properties file: " + key);
121 }
122 }
123 }
124 }
125 else if (LOG.isDebugEnabled()) {
126 LOG.debug("did not find properties at '" + filePath + "'");
127 }
128 }
129 catch (IOException ex) {
130 LOG.warn("Could not initialize properties: '" + filePath + "'", ex);
131 }
132 return properties;
133 }
134
135
136
137
138
139
140
141
142 public static Properties loadProperties(String filePath) {
143 return loadProperties(new Properties(), filePath);
144 }
145
146 private static InputStream getInputStream(String filePath) throws FileNotFoundException {
147 File propertiesFile = new File(filePath);
148 InputStream stream;
149 if (propertiesFile.exists() && propertiesFile.isFile()) {
150 if (LOG.isDebugEnabled()) {
151 LOG.debug("initializing properties from file '" + filePath + "'");
152 }
153 Reader reader = new FileReader(propertiesFile);
154 stream = new ReaderInputStream(reader, CHARSET_UTF8);
155 }
156 else {
157 if (LOG.isDebugEnabled()) {
158 LOG.debug("initializing properties from resource '" + filePath + "'");
159 }
160 stream = PropertiesUtil.class.getResourceAsStream(filePath);
161 }
162 if (stream != null) {
163 if (LOG.isInfoEnabled()) {
164 LOG.info("got input stream for " + filePath);
165 }
166 }
167 else {
168 if (LOG.isInfoEnabled()) {
169 LOG.info("no input stream for " + filePath);
170 }
171 }
172 return stream;
173 }
174
175 }