View Javadoc

1   /**
2    *  Copyright 2003-2006 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.util.Properties;
23  import java.io.ByteArrayInputStream;
24  import java.io.IOException;
25  
26  /**
27   * Property utilities.
28   * @author Greg Luck
29   * @version $Id: PropertyUtil.java 52 2006-04-24 14:50:03Z gregluck $
30   */
31  public final class PropertyUtil {
32  
33      private static final Log LOG = LogFactory.getLog(PropertyUtil.class.getName());
34  
35      /**
36       * Utility class therefore no constructor.
37       */
38      private PropertyUtil() {
39          //noop
40      }
41  
42      /**
43       * @return null if their is no property for the key, or their are no properties
44       */
45      public static String extractAndLogProperty(String name, Properties properties) {
46          if (properties == null || properties.size() == 0) {
47              return null;
48          }
49          String foundValue = (String) properties.get(name);
50          if (foundValue != null) {
51              foundValue = foundValue.trim();
52          }
53          if (LOG.isDebugEnabled()) {
54              LOG.debug(new StringBuffer().append("Value found for ").append(name).append(": ")
55                      .append(foundValue).toString());
56          }
57          return foundValue;
58      }
59  
60      /**
61       * Parse properties supplied as a comma separated list into a <code>Properties</code> object
62       * @param propertiesString a comma separated list such as <code>"propertyA=s, propertyB=t"</code>
63       * @return a newly constructed properties object
64       */
65      public static Properties parseProperties(String propertiesString) {
66          if (propertiesString == null) {
67              LOG.debug("propertiesString is null.");
68              return null;
69          }
70          Properties properties = new Properties();
71          String propertyLines = propertiesString.trim();
72          propertyLines = propertyLines.replace(',', '\n');
73          try {
74              properties.load(new ByteArrayInputStream(propertyLines.getBytes()));
75          } catch (IOException e) {
76              LOG.error("Cannot load properties from " + propertiesString);
77          }
78          return properties;
79      }
80  }