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