001: /**
002: * Copyright 2003-2007 Luck Consulting Pty Ltd
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package net.sf.ehcache.util;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.commons.logging.LogFactory;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.IOException;
022: import java.util.Properties;
023: import java.util.Map;
024:
025: /**
026: * Property utilities.
027: * @author Greg Luck
028: * @version $Id: PropertyUtil.java 519 2007-07-27 07:11:45Z gregluck $
029: */
030: public final class PropertyUtil {
031:
032: private static final Log LOG = LogFactory.getLog(PropertyUtil.class
033: .getName());
034: private static final String DEFAULT_PROPERTY_SEPARATOR = ",";
035:
036: /**
037: * Utility class therefore no constructor.
038: */
039: private PropertyUtil() {
040: //noop
041: }
042:
043: /**
044: * @return null if their is no property for the key, or their are no properties
045: */
046: public static String extractAndLogProperty(String name,
047: Properties properties) {
048: if (properties == null || properties.size() == 0) {
049: return null;
050: }
051: String foundValue = (String) properties.get(name);
052: if (foundValue != null) {
053: foundValue = foundValue.trim();
054: }
055: if (LOG.isDebugEnabled()) {
056: LOG.debug(new StringBuffer().append("Value found for ")
057: .append(name).append(": ").append(foundValue)
058: .toString());
059: }
060: return foundValue;
061: }
062:
063: /**
064: * @return null if their is no property for the key, or their are no properties
065: */
066: public static String extractAndLogProperty(String name,
067: Map properties) {
068: if (properties == null || properties.size() == 0) {
069: return null;
070: }
071: String foundValue = (String) properties.get(name);
072: if (foundValue != null) {
073: foundValue = foundValue.trim();
074: }
075: if (LOG.isDebugEnabled()) {
076: LOG.debug(new StringBuffer().append("Value found for ")
077: .append(name).append(": ").append(foundValue)
078: .toString());
079: }
080: return foundValue;
081: }
082:
083: /**
084: * Parse properties supplied as a comma separated list into a <code>Properties</code> object
085: * @param propertiesString a comma separated list such as <code>"propertyA=s, propertyB=t"</code>
086: * @return a newly constructed properties object
087: */
088: public static Properties parseProperties(String propertiesString,
089: String propertySeparator) {
090: String propertySeparatorLocal = propertySeparator;
091: if (propertiesString == null) {
092: LOG.debug("propertiesString is null.");
093: return null;
094: }
095: if (propertySeparator == null) {
096: propertySeparatorLocal = DEFAULT_PROPERTY_SEPARATOR;
097: }
098: Properties properties = new Properties();
099: String propertyLines = propertiesString.trim();
100: propertyLines = propertyLines.replaceAll(
101: propertySeparatorLocal, "\n");
102: try {
103: properties.load(new ByteArrayInputStream(propertyLines
104: .getBytes()));
105: } catch (IOException e) {
106: LOG
107: .error("Cannot load properties from "
108: + propertiesString);
109: }
110: return properties;
111: }
112:
113: /**
114: * Null safe, parser of boolean from a String
115: * @param value
116: * @return true if non null and case insensitively matches true
117: */
118: public static boolean parseBoolean(String value) {
119: return ((value != null) && value.equalsIgnoreCase("true"));
120: }
121: }
|