1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.spring.parsers.assembly.configuration; |
8 | |
|
9 | |
import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser; |
10 | |
|
11 | |
import java.util.ArrayList; |
12 | |
import java.util.HashMap; |
13 | |
import java.util.HashSet; |
14 | |
import java.util.Iterator; |
15 | |
import java.util.List; |
16 | |
import java.util.Map; |
17 | |
import java.util.Properties; |
18 | |
import java.util.Set; |
19 | |
|
20 | |
import org.springframework.util.StringUtils; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 0 | public class SimplePropertyConfiguration implements PropertyConfiguration |
26 | |
{ |
27 | |
|
28 | 0 | private List references = new ArrayList(); |
29 | 0 | private Properties nameMappings = new Properties(); |
30 | 0 | private Map valueMappings = new HashMap(); |
31 | 0 | private Set collections = new HashSet(); |
32 | 0 | private Map ignored = new HashMap(); |
33 | 0 | private boolean ignoreAll = false; |
34 | |
|
35 | |
public void addReference(String propertyName) |
36 | |
{ |
37 | 0 | references.add(dropRef(propertyName)); |
38 | 0 | } |
39 | |
|
40 | |
public void addMapping(String propertyName, Map mappings) |
41 | |
{ |
42 | 0 | valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings)); |
43 | 0 | } |
44 | |
|
45 | |
public void addMapping(String propertyName, String mappings) |
46 | |
{ |
47 | 0 | valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings)); |
48 | 0 | } |
49 | |
|
50 | |
public void addMapping(String propertyName, ValueMap mappings) |
51 | |
{ |
52 | 0 | valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings)); |
53 | 0 | } |
54 | |
|
55 | |
public void addAlias(String alias, String propertyName) |
56 | |
{ |
57 | 0 | nameMappings.put(alias, propertyName); |
58 | 0 | } |
59 | |
|
60 | |
public void addCollection(String propertyName) |
61 | |
{ |
62 | 0 | collections.add(dropRef(propertyName)); |
63 | 0 | } |
64 | |
|
65 | |
public void addIgnored(String propertyName) |
66 | |
{ |
67 | 0 | ignored.put(dropRef(propertyName), Boolean.TRUE); |
68 | 0 | } |
69 | |
|
70 | |
public void removeIgnored(String propertyName) |
71 | |
{ |
72 | 0 | ignored.put(dropRef(propertyName), Boolean.FALSE); |
73 | 0 | } |
74 | |
|
75 | |
public void setIgnoredDefault(boolean ignoreAll) |
76 | |
{ |
77 | 0 | this.ignoreAll = ignoreAll; |
78 | 0 | } |
79 | |
|
80 | |
public String getAttributeMapping(String alias) |
81 | |
{ |
82 | 0 | return getAttributeMapping(alias, alias); |
83 | |
} |
84 | |
|
85 | |
public String getAttributeAlias(String name) |
86 | |
{ |
87 | 0 | for (Iterator iterator = nameMappings.entrySet().iterator(); iterator.hasNext();) |
88 | |
{ |
89 | 0 | Map.Entry entry = (Map.Entry)iterator.next(); |
90 | 0 | if(entry.getValue().equals(name)) |
91 | |
{ |
92 | 0 | return entry.getKey().toString(); |
93 | |
} |
94 | 0 | } |
95 | 0 | return name; |
96 | |
} |
97 | |
|
98 | |
public String getAttributeMapping(String alias, String deflt) |
99 | |
{ |
100 | 0 | return nameMappings.getProperty(alias, deflt); |
101 | |
} |
102 | |
|
103 | |
public boolean isCollection(String propertyName) |
104 | |
{ |
105 | 0 | return collections.contains(dropRef(propertyName)); |
106 | |
} |
107 | |
|
108 | |
public boolean isIgnored(String propertyName) |
109 | |
{ |
110 | 0 | String name = dropRef(propertyName); |
111 | 0 | if (ignored.containsKey(name)) |
112 | |
{ |
113 | 0 | return ((Boolean) ignored.get(name)).booleanValue(); |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | 0 | return ignoreAll; |
118 | |
} |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public boolean isReference(String attributeName) |
127 | |
{ |
128 | 0 | return (references.contains(dropRef(attributeName)) |
129 | |
|| attributeName.endsWith(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF_SUFFIX) |
130 | |
|| attributeName.endsWith(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REFS_SUFFIX) |
131 | |
|| attributeName.equals(AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF)); |
132 | |
} |
133 | |
|
134 | |
public SingleProperty getSingleProperty(String name) |
135 | |
{ |
136 | 0 | return new SinglePropertyWrapper(name, this); |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public String translateName(String oldName) |
152 | |
{ |
153 | |
|
154 | 0 | String name = dropRef(oldName); |
155 | |
|
156 | 0 | name = getAttributeMapping(name); |
157 | |
|
158 | 0 | name = Conventions.attributeNameToPropertyName(name); |
159 | 0 | if (!StringUtils.hasText(name)) |
160 | |
{ |
161 | 0 | throw new IllegalStateException("Illegal property name for " + oldName + ": cannot be null or empty."); |
162 | |
} |
163 | 0 | return name; |
164 | |
} |
165 | |
|
166 | |
protected String dropRef(String name) |
167 | |
{ |
168 | 0 | return org.mule.util.StringUtils.chomp( |
169 | |
org.mule.util.StringUtils.chomp(name, AbstractMuleBeanDefinitionParser.ATTRIBUTE_REF_SUFFIX), |
170 | |
AbstractMuleBeanDefinitionParser.ATTRIBUTE_REFS_SUFFIX); |
171 | |
} |
172 | |
|
173 | |
public Object translateValue(String name, String value) |
174 | |
{ |
175 | 0 | NamedValueMap vm = (NamedValueMap) valueMappings.get(name); |
176 | 0 | if (vm != null) |
177 | |
{ |
178 | 0 | return vm.getValue(value); |
179 | |
} |
180 | |
else |
181 | |
{ |
182 | 0 | return value; |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
|
187 | |
public static class NamedValueMap |
188 | |
{ |
189 | |
private String propertyName; |
190 | |
private ValueMap valueMap; |
191 | |
|
192 | |
public NamedValueMap(String propertyName, String mappingsString) |
193 | 0 | { |
194 | 0 | this.propertyName = propertyName; |
195 | 0 | valueMap = new MapValueMap(mappingsString); |
196 | 0 | } |
197 | |
|
198 | |
public NamedValueMap(String propertyName, Map valueMap) |
199 | 0 | { |
200 | 0 | this.propertyName = propertyName; |
201 | 0 | this.valueMap = new MapValueMap(valueMap); |
202 | 0 | } |
203 | |
|
204 | |
public NamedValueMap(String propertyName, ValueMap valueMap) |
205 | 0 | { |
206 | 0 | this.propertyName = propertyName; |
207 | 0 | this.valueMap = valueMap; |
208 | 0 | } |
209 | |
|
210 | |
public String getPropertyName() |
211 | |
{ |
212 | 0 | return propertyName; |
213 | |
} |
214 | |
|
215 | |
public Object getValue(String key) |
216 | |
{ |
217 | 0 | return valueMap.rewrite(key); |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
public static class MapValueMap implements ValueMap |
222 | |
{ |
223 | |
|
224 | |
protected Map map; |
225 | |
|
226 | |
public MapValueMap(Map map) |
227 | 0 | { |
228 | 0 | this.map = map; |
229 | 0 | } |
230 | |
|
231 | |
public MapValueMap(String definition) |
232 | 0 | { |
233 | 0 | map = new HashMap(); |
234 | |
|
235 | 0 | String[] values = StringUtils.tokenizeToStringArray(definition, ","); |
236 | 0 | for (int i = 0; i < values.length; i++) |
237 | |
{ |
238 | 0 | String value = values[i]; |
239 | 0 | int x = value.indexOf("="); |
240 | 0 | if (x == -1) |
241 | |
{ |
242 | 0 | throw new IllegalArgumentException("Mappings string not properly defined: " + definition); |
243 | |
} |
244 | 0 | map.put(value.substring(0, x), value.substring(x+1)); |
245 | |
} |
246 | |
|
247 | 0 | } |
248 | |
|
249 | |
public Object rewrite(String value) |
250 | |
{ |
251 | 0 | Object result = map.get(value); |
252 | 0 | if (null == result) |
253 | |
{ |
254 | 0 | return value; |
255 | |
} |
256 | |
else |
257 | |
{ |
258 | 0 | return result.toString(); |
259 | |
} |
260 | |
} |
261 | |
|
262 | |
} |
263 | |
|
264 | 0 | public static class IndentityMapValueMap extends MapValueMap |
265 | |
{ |
266 | |
|
267 | |
public IndentityMapValueMap(Map map) |
268 | |
{ |
269 | 0 | super(map); |
270 | 0 | } |
271 | |
|
272 | |
@Override |
273 | |
public Object rewrite(String value) |
274 | |
{ |
275 | 0 | Object result = map.get(value); |
276 | 0 | return result; |
277 | |
} |
278 | |
} |
279 | |
} |