1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
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
39
40 private PropertyUtil() {
41
42 }
43
44
45
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
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
82
83
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
107
108
109
110 public static boolean parseBoolean(String value) {
111 return ((value != null) && value.equalsIgnoreCase("true"));
112 }
113 }