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