1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.util.Map;
23 import java.util.Properties;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /***
29 * Property utilities.
30 * @author Greg Luck
31 * @version $Id: PropertyUtil.java 2154 2010-04-06 02:45:52Z cdennis $
32 */
33 public final class PropertyUtil {
34
35 private static final Logger LOG = LoggerFactory.getLogger(PropertyUtil.class.getName());
36
37 private static final String DEFAULT_PROPERTY_SEPARATOR = ",";
38
39 /***
40 * Utility class therefore no constructor.
41 */
42 private PropertyUtil() {
43
44 }
45
46 /***
47 * @return null if their is no property for the key, or their are no properties
48 */
49 public static String extractAndLogProperty(String name, Properties properties) {
50 if (properties == null || properties.size() == 0) {
51 return null;
52 }
53 String foundValue = (String) properties.get(name);
54 if (foundValue != null) {
55 foundValue = foundValue.trim();
56 }
57 if (LOG.isDebugEnabled()) {
58 LOG.debug(new StringBuilder().append("Value found for ").append(name).append(": ").append(foundValue).toString());
59 }
60 return foundValue;
61 }
62
63 /***
64 * @return null if their is no property for the key, or their are no properties
65 */
66 public static String extractAndLogProperty(String name, Map properties) {
67 if (properties == null || properties.size() == 0) {
68 return null;
69 }
70 String foundValue = (String) properties.get(name);
71 if (foundValue != null) {
72 foundValue = foundValue.trim();
73 }
74 if (LOG.isDebugEnabled()) {
75 LOG.debug(new StringBuilder().append("Value found for ").append(name).append(": ").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 }