View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Helper methods for dealing with Properties.
40   *
41   * @since 1.0.0
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      // do not instantiate
51    }
52  
53    /**
54     * Filter properties by key part.
55     *
56     * @param properties to filter
57     * @param searchString to filter by
58     * @return only properties containing the search string in their key
59     * @since 3.0.0
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     * Filter properties by key prefix.
67     *
68     * @param properties to filter
69     * @param prefix to filter by
70     * @return only properties with a key starting with the prefix string
71     * @since 3.0.0
72     */
73    public static Properties getAllPropertiesWithPrefix(Properties properties, String prefix) {
74      return getFilteredProperties(properties, Pattern.compile(prefix + REGEX_WILDCARD));
75    }
76  
77    /**
78     * Filter properties by regular expression.
79     *
80     * @param properties to filter
81     * @param filter regex pattern to filter by
82     * @return only properties with a key matching the regular expression
83     * @since 3.0.0
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    * Load properties from file.
104    *
105    * @param properties to fill from file
106    * @param filePath to properties file
107    * @return properties from file
108    * @since 3.0.0
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    * Load properties from file.
137    *
138    * @param filePath to properties file
139    * @return properties from file
140    * @since 3.0.0
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 }