Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PropertiesConverter |
|
| 3.5;3.5 |
1 | /* | |
2 | * $Id: PropertiesConverter.java 19191 2010-08-25 21:05:23Z tcarlson $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | package org.mule.config.converters; | |
11 | ||
12 | import org.mule.api.MuleContext; | |
13 | import org.mule.api.expression.PropertyConverter; | |
14 | import org.mule.util.StringUtils; | |
15 | ||
16 | import java.util.Properties; | |
17 | import java.util.StringTokenizer; | |
18 | ||
19 | /** | |
20 | * Converts a comma-separated list of key/value pairs, e.g., | |
21 | * <code>"apple=green, banana=yellow"</code> into a {@link java.util.Properties} map. | |
22 | * Property placeholders can be used in these values: | |
23 | * <code>"apple=${apple.color}, banana=yellow"</code> | |
24 | */ | |
25 | 0 | public class PropertiesConverter implements PropertyConverter |
26 | { | |
27 | public static final String DELIM = ","; | |
28 | ||
29 | public Object convert(String properties, MuleContext context) | |
30 | { | |
31 | 0 | if (StringUtils.isNotBlank(properties)) |
32 | { | |
33 | 0 | Properties props = new Properties(); |
34 | ||
35 | 0 | StringTokenizer st = new StringTokenizer(properties, DELIM); |
36 | 0 | while (st.hasMoreTokens()) |
37 | { | |
38 | 0 | String key = st.nextToken().trim(); |
39 | 0 | int i = key.indexOf("="); |
40 | 0 | if(i < 1) { |
41 | 0 | throw new IllegalArgumentException("Property string is malformed: " + properties); |
42 | } | |
43 | 0 | String value = key.substring(i+1); |
44 | 0 | key = key.substring(0, i); |
45 | 0 | props.setProperty(key, value); |
46 | 0 | } |
47 | 0 | return props; |
48 | } | |
49 | 0 | return null; |
50 | } | |
51 | ||
52 | public Class getType() | |
53 | { | |
54 | 0 | return Properties.class; |
55 | } | |
56 | } |