2
* Copyright 2003-2010 Terracotta, Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package net.sf.ehcache.util;
20
import java.io.ByteArrayInputStream;
21
import java.io.IOException;
23
import java.util.Properties;
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
31
* @version $Id: PropertyUtil.java 2154 2010-04-06 02:45:52Z cdennis $
33
public final class PropertyUtil {
35
private static final Logger LOG = LoggerFactory.getLogger(PropertyUtil.class.getName());
37
private static final String DEFAULT_PROPERTY_SEPARATOR = ",";
40
* Utility class therefore no constructor.
42
private PropertyUtil() {
47
* @return null if their is no property for the key, or their are no properties
49
public static String extractAndLogProperty(String name, Properties properties) {
50
if (properties == null || properties.size() == 0) {
53
String foundValue = (String) properties.get(name);
54
if (foundValue != null) {
55
foundValue = foundValue.trim();
57
if (LOG.isDebugEnabled()) {
58
LOG.debug(new StringBuilder().append("Value found for ").append(name).append(": ").append(foundValue).toString());
64
* @return null if their is no property for the key, or their are no properties
66
public static String extractAndLogProperty(String name, Map properties) {
67
if (properties == null || properties.size() == 0) {
70
String foundValue = (String) properties.get(name);
71
if (foundValue != null) {
72
foundValue = foundValue.trim();
74
if (LOG.isDebugEnabled()) {
75
LOG.debug(new StringBuilder().append("Value found for ").append(name).append(": ").append(foundValue).toString());
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
85
public static Properties parseProperties(String propertiesString, String propertySeparator) {
86
String propertySeparatorLocal = propertySeparator;
87
if (propertiesString == null) {
88
LOG.debug("propertiesString is null.");
91
if (propertySeparator == null) {
92
propertySeparatorLocal = DEFAULT_PROPERTY_SEPARATOR;
94
Properties properties = new Properties();
95
String propertyLines = propertiesString.trim();
96
propertyLines = propertyLines.replaceAll(propertySeparatorLocal, "\n");
98
properties.load(new ByteArrayInputStream(propertyLines.getBytes()));
99
} catch (IOException e) {
100
LOG.error("Cannot load properties from " + propertiesString);
106
* Null safe, parser of boolean from a String
108
* @return true if non null and case insensitively matches true
110
public static boolean parseBoolean(String value) {
111
return ((value != null) && value.equalsIgnoreCase("true"));