View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  package org.esigate.util;
16  
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.Properties;
20  
21  import org.esigate.ConfigurationException;
22  
23  /**
24   * Utility methods for loading configuration parameters.
25   * 
26   * @author Francois-Xavier Bonnet
27   * @author Nicolas Richeton
28   * 
29   */
30  public final class PropertiesUtil {
31  
32      private PropertiesUtil() {
33  
34      }
35  
36      /**
37       * Retrieves a property containing a comma separated list of values, trim them and return them as a Collection of
38       * String.
39       * 
40       * @param properties
41       * @param propertyName
42       * @param defaultValue
43       * @return the values
44       */
45      public static Collection<String> getPropertyValue(Properties properties, String propertyName,
46              Collection<String> defaultValue) {
47          Collection<String> result = defaultValue;
48          String propertyValue = properties.getProperty(propertyName);
49          if (propertyValue != null) {
50              result = toCollection(propertyValue);
51              if (result.contains("*") && result.size() > 1) {
52                  throw new ConfigurationException(propertyName + " must be a comma-separated list or *");
53              }
54          }
55          return result;
56      }
57  
58      /**
59       * Return the provided comma-separated String as a collection. Order is maintained.
60       * 
61       * @param list
62       * @return Ordered collection
63       */
64      static Collection<String> toCollection(String list) {
65          Collection<String> result = new ArrayList<>();
66          if (list != null) {
67              String[] values = list.split(",");
68              for (String value : values) {
69                  String trimmed = value.trim();
70                  if (!trimmed.isEmpty()) {
71                      result.add(trimmed);
72                  }
73              }
74          }
75          return result;
76      }
77  
78      public static int getPropertyValue(Properties props, String name, int defaultValue) {
79          String value = props.getProperty(name);
80          int result = defaultValue;
81          if (value != null) {
82              result = Integer.parseInt(value);
83          }
84          return result;
85      }
86  
87      public static boolean getPropertyValue(Properties props, String name, boolean defaultValue) {
88          String value = props.getProperty(name);
89          boolean result = defaultValue;
90          if (value != null) {
91              result = Boolean.parseBoolean(value);
92          }
93          return result;
94      }
95  
96      public static float getPropertyValue(Properties properties, String name, float defaultValue) {
97          String value = properties.getProperty(name);
98          float result = defaultValue;
99          if (value != null) {
100             result = Float.parseFloat(value);
101         }
102         return result;
103     }
104 
105     public static long getPropertyValue(Properties properties, String name, long defaultValue) {
106         String value = properties.getProperty(name);
107         long result = defaultValue;
108         if (value != null) {
109             result = Long.parseLong(value);
110         }
111         return result;
112     }
113 
114 }