View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.util;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.util.Properties;
25  import java.util.Map;
26  
27  /**
28   * Property utilities.
29   * @author Greg Luck
30   * @version $Id: PropertyUtil.java 512 2007-07-10 09:18:45Z gregluck $
31   */
32  public final class PropertyUtil {
33  
34      private static final Log LOG = LogFactory.getLog(PropertyUtil.class.getName());
35      private static final String DEFAULT_PROPERTY_SEPARATOR = ",";
36  
37      /**
38       * Utility class therefore no constructor.
39       */
40      private PropertyUtil() {
41          //noop
42      }
43  
44      /**
45       * @return null if their is no property for the key, or their are no properties
46       */
47      public static String extractAndLogProperty(String name, Properties properties) {
48          if (properties == null || properties.size() == 0) {
49              return null;
50          }
51          String foundValue = (String) properties.get(name);
52          if (foundValue != null) {
53              foundValue = foundValue.trim();
54          }
55          if (LOG.isDebugEnabled()) {
56              LOG.debug(new StringBuffer().append("Value found for ").append(name).append(": ")
57                      .append(foundValue).toString());
58          }
59          return foundValue;
60      }
61  
62      /**
63       * @return null if their is no property for the key, or their are no properties
64       */
65      public static String extractAndLogProperty(String name, Map properties) {
66          if (properties == null || properties.size() == 0) {
67              return null;
68          }
69          String foundValue = (String) properties.get(name);
70          if (foundValue != null) {
71              foundValue = foundValue.trim();
72          }
73          if (LOG.isDebugEnabled()) {
74              LOG.debug(new StringBuffer().append("Value found for ").append(name).append(": ")
75                      .append(foundValue).toString());
76          }
77          return foundValue;
78      }
79  
80      /**
81       * Parse properties supplied as a comma separated list into a <code>Properties</code> object
82       * @param propertiesString a comma separated list such as <code>"propertyA=s, propertyB=t"</code>
83       * @return a newly constructed properties object
84       */
85      public static Properties parseProperties(String propertiesString, String propertySeparator) {
86          String propertySeparatorLocal = propertySeparator;
87          if (propertiesString == null) {
88              LOG.debug("propertiesString is null.");
89              return null;
90          }
91          if (propertySeparator == null) {
92              propertySeparatorLocal = DEFAULT_PROPERTY_SEPARATOR;
93          }
94          Properties properties = new Properties();
95          String propertyLines = propertiesString.trim();
96          propertyLines = propertyLines.replaceAll(propertySeparatorLocal, "\n");
97          try {
98              properties.load(new ByteArrayInputStream(propertyLines.getBytes()));
99          } catch (IOException e) {
100             LOG.error("Cannot load properties from " + propertiesString);
101         }
102         return properties;
103     }
104 
105     /**
106      * Null safe, parser of boolean from a String
107      * @param value
108      * @return true if non null and case insensitively matches true
109      */
110     public static boolean parseBoolean(String value) {
111         return ((value != null) && value.equalsIgnoreCase("true"));
112     }
113 }