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