1
2
3
4
5
6
7
8
9
10
11 package org.mule.util;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.config.i18n.Message;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
25
26
27
28
29
30
31 public final class PropertiesUtils
32 {
33
34 private static final List maskedProperties = new CopyOnWriteArrayList();
35
36 static
37 {
38
39
40 registerMaskedPropertyName("password");
41 }
42
43
44 protected PropertiesUtils ()
45 {
46
47 }
48
49
50
51
52
53
54
55
56 public static void registerMaskedPropertyName(String name)
57 {
58 if (StringUtils.isNotEmpty(name))
59 {
60 maskedProperties.add(name);
61 }
62 else
63 {
64 throw new IllegalArgumentException("Cannot mask empty property name.");
65 }
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public static String maskedPropertyValue(Map.Entry property)
79 {
80 if (property == null)
81 {
82 return null;
83 }
84
85 Object key = property.getKey();
86 Object value = property.getValue();
87
88 if (key == null || value == null)
89 {
90 return null;
91 }
92
93 if (maskedProperties.contains(key))
94 {
95 return ("*****");
96 }
97 else
98 {
99 return value.toString();
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 public static synchronized Properties loadProperties(String fileName, final Class callingClass)
113 throws IOException
114 {
115 InputStream is = IOUtils.getResourceAsStream(fileName, callingClass,
116
117 if (is == null)
118 {
119 Message error = CoreMessages.cannotLoadFromClasspath(fileName);
120 throw new IOException(error.toString());
121 }
122
123 try
124 {
125 Properties props = new Properties();
126 props.load(is);
127 return props;
128 }
129 finally
130 {
131 is.close();
132 }
133 }
134
135 public static String removeXmlNamespacePrefix(String eleName)
136 {
137 int i = eleName.indexOf(':');
138 return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length()));
139 }
140
141 public static String removeNamespacePrefix(String eleName)
142 {
143 int i = eleName.lastIndexOf('.');
144 return (i == -1 ? eleName : eleName.substring(i + 1, eleName.length()));
145 }
146
147 public static Map removeNamespaces(Map properties)
148 {
149 HashMap props = new HashMap(properties.size());
150 Map.Entry entry;
151 for (Iterator iter = properties.entrySet().iterator(); iter.hasNext();)
152 {
153 entry = (Map.Entry) iter.next();
154 props.put(removeNamespacePrefix((String) entry.getKey()), entry.getValue());
155
156 }
157 return props;
158 }
159
160
161
162
163
164
165
166
167
168
169 public static void getPropertiesWithPrefix(Map props, String prefix, Map newProps)
170 {
171 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
172 {
173 Map.Entry entry = (Map.Entry) iterator.next();
174 Object key = entry.getKey();
175 if (key.toString().startsWith(prefix))
176 {
177 newProps.put(key, entry.getValue());
178 }
179 }
180 }
181
182 public static Map getPropertiesWithoutPrefix(Map props, String prefix)
183 {
184 Map newProps = new HashMap();
185 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
186 {
187 Map.Entry entry = (Map.Entry) iterator.next();
188 Object key = entry.getKey();
189 if (!key.toString().startsWith(prefix))
190 {
191 newProps.put(key, entry.getValue());
192 }
193 }
194 return newProps;
195 }
196
197 public static Properties getPropertiesFromQueryString(String query)
198 {
199 Properties props = new Properties();
200
201 if (query == null)
202 {
203 return props;
204 }
205
206 query = new StringBuffer(query.length() + 1).append('&').append(query).toString();
207
208 int x = 0;
209 while ((x = addProperty(query, x, props)) != -1)
210 {
211
212 }
213
214 return props;
215 }
216
217 private static int addProperty(String query, int start, Properties properties)
218 {
219 int i = query.indexOf('&', start);
220 int i2 = query.indexOf('&', i + 1);
221 String pair;
222 if (i > -1 && i2 > -1)
223 {
224 pair = query.substring(i + 1, i2);
225 }
226 else if (i > -1)
227 {
228 pair = query.substring(i + 1);
229 }
230 else
231 {
232 return -1;
233 }
234 int eq = pair.indexOf('=');
235
236 if (eq <= 0)
237 {
238 String key = pair;
239 String value = StringUtils.EMPTY;
240 properties.setProperty(key, value);
241 }
242 else
243 {
244 String key = pair.substring(0, eq);
245 String value = (eq == pair.length() ? StringUtils.EMPTY : pair.substring(eq + 1));
246 properties.setProperty(key, value);
247 }
248 return i2;
249 }
250
251
252
253
254 public static String propertiesToString(Map props, boolean newline)
255 {
256 return MapUtils.toString(props, newline);
257 }
258
259 }