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