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 public class SimplePropertyConfiguration implements PropertyConfiguration
30 {
31
32 private List references = new ArrayList();
33 private Properties nameMappings = new Properties();
34 private Map valueMappings = new HashMap();
35 private Set collections = new HashSet();
36 private Map ignored = new HashMap();
37 private boolean ignoreAll = false;
38
39 public void addReference(String propertyName)
40 {
41 references.add(dropRef(propertyName));
42 }
43
44 public void addMapping(String propertyName, Map mappings)
45 {
46 valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings));
47 }
48
49 public void addMapping(String propertyName, String mappings)
50 {
51 valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings));
52 }
53
54 public void addMapping(String propertyName, ValueMap mappings)
55 {
56 valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings));
57 }
58
59 public void addAlias(String alias, String propertyName)
60 {
61 nameMappings.put(alias, propertyName);
62 }
63
64 public void addCollection(String propertyName)
65 {
66 collections.add(dropRef(propertyName));
67 }
68
69 public void addIgnored(String propertyName)
70 {
71 ignored.put(dropRef(propertyName), Boolean.TRUE);
72 }
73
74 public void removeIgnored(String propertyName)
75 {
76 ignored.put(dropRef(propertyName), Boolean.FALSE);
77 }
78
79 public void setIgnoredDefault(boolean ignoreAll)
80 {
81 this.ignoreAll = ignoreAll;
82 }
83
84 public String getAttributeMapping(String alias)
85 {
86 return getAttributeMapping(alias, alias);
87 }
88
89 public String getAttributeAlias(String name)
90 {
91 for (Iterator iterator = nameMappings.entrySet().iterator(); iterator.hasNext();)
92 {
93 Map.Entry entry = (Map.Entry)iterator.next();
94 if(entry.getValue().equals(name))
95 {
96 return entry.getKey().toString();
97 }
98 }
99 return name;
100 }
101
102 public String getAttributeMapping(String alias, String deflt)
103 {
104 return nameMappings.getProperty(alias, deflt);
105 }
106
107 public boolean isCollection(String propertyName)
108 {
109 return collections.contains(dropRef(propertyName));
110 }
111
112 public boolean isIgnored(String propertyName)
113 {
114 String name = dropRef(propertyName);
115 if (ignored.containsKey(name))
116 {
117 return ((Boolean) ignored.get(name)).booleanValue();
118 }
119 else
120 {
121 return ignoreAll;
122 }
123 }
124
125
126
127
128
129
130 public boolean isReference(String attributeName)
131 {
132 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 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 String name = dropRef(oldName);
159
160 name = getAttributeMapping(name);
161
162 name = Conventions.attributeNameToPropertyName(name);
163 if (!StringUtils.hasText(name))
164 {
165 throw new IllegalStateException("Illegal property name for " + oldName + ": cannot be null or empty.");
166 }
167 return name;
168 }
169
170 protected String dropRef(String name)
171 {
172 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 NamedValueMap vm = (NamedValueMap) valueMappings.get(name);
180 if (vm != null)
181 {
182 return vm.getValue(value);
183 }
184 else
185 {
186 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 {
198 this.propertyName = propertyName;
199 valueMap = new MapValueMap(mappingsString);
200 }
201
202 public NamedValueMap(String propertyName, Map valueMap)
203 {
204 this.propertyName = propertyName;
205 this.valueMap = new MapValueMap(valueMap);
206 }
207
208 public NamedValueMap(String propertyName, ValueMap valueMap)
209 {
210 this.propertyName = propertyName;
211 this.valueMap = valueMap;
212 }
213
214 public String getPropertyName()
215 {
216 return propertyName;
217 }
218
219 public Object getValue(String key)
220 {
221 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 {
232 this.map = map;
233 }
234
235 public MapValueMap(String definition)
236 {
237 map = new HashMap();
238
239 String[] values = StringUtils.tokenizeToStringArray(definition, ",");
240 for (int i = 0; i < values.length; i++)
241 {
242 String value = values[i];
243 int x = value.indexOf("=");
244 if (x == -1)
245 {
246 throw new IllegalArgumentException("Mappings string not properly defined: " + definition);
247 }
248 map.put(value.substring(0, x), value.substring(x+1));
249 }
250
251 }
252
253 public Object rewrite(String value)
254 {
255 Object result = map.get(value);
256 if (null == result)
257 {
258 return value;
259 }
260 else
261 {
262 return result.toString();
263 }
264 }
265
266 }
267
268 public static class IndentityMapValueMap extends MapValueMap
269 {
270
271 public IndentityMapValueMap(Map map)
272 {
273 super(map);
274 }
275
276 @Override
277 public Object rewrite(String value)
278 {
279 Object result = map.get(value);
280 return result;
281 }
282 }
283 }