View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * A direct implementation of {@link PropertyConfiguration}
24   */
25  public class SimplePropertyConfiguration implements PropertyConfiguration
26  {
27  
28      private List references = new ArrayList();
29      private Properties nameMappings = new Properties();
30      private Map valueMappings = new HashMap();
31      private Set collections = new HashSet();
32      private Map ignored = new HashMap();
33      private boolean ignoreAll = false;
34  
35      public void addReference(String propertyName)
36      {
37          references.add(dropRef(propertyName));
38      }
39  
40      public void addMapping(String propertyName, Map mappings)
41      {
42          valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings));
43      }
44  
45      public void addMapping(String propertyName, String mappings)
46      {
47          valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings));
48      }
49  
50      public void addMapping(String propertyName, ValueMap mappings)
51      {
52          valueMappings.put(propertyName, new NamedValueMap(propertyName, mappings));
53      }
54  
55      public void addAlias(String alias, String propertyName)
56      {
57          nameMappings.put(alias, propertyName);
58      }
59  
60      public void addCollection(String propertyName)
61      {
62          collections.add(dropRef(propertyName));
63      }
64  
65      public void addIgnored(String propertyName)
66      {
67          ignored.put(dropRef(propertyName), Boolean.TRUE);
68      }
69  
70      public void removeIgnored(String propertyName)
71      {
72          ignored.put(dropRef(propertyName), Boolean.FALSE);
73      }
74  
75      public void setIgnoredDefault(boolean ignoreAll)
76      {
77          this.ignoreAll = ignoreAll;
78      }
79  
80      public String getAttributeMapping(String alias)
81      {
82          return getAttributeMapping(alias, alias);
83      }
84  
85      public String getAttributeAlias(String name)
86      {
87          for (Iterator iterator = nameMappings.entrySet().iterator(); iterator.hasNext();)
88          {
89              Map.Entry entry = (Map.Entry)iterator.next();
90              if(entry.getValue().equals(name))
91              {
92                  return entry.getKey().toString();
93              }
94          }
95          return name;
96      }
97  
98      public String getAttributeMapping(String alias, String deflt)
99      {
100         return nameMappings.getProperty(alias, deflt);
101     }
102 
103     public boolean isCollection(String propertyName)
104     {
105         return collections.contains(dropRef(propertyName));
106     }
107 
108     public boolean isIgnored(String propertyName)
109     {
110         String name = dropRef(propertyName);
111         if (ignored.containsKey(name))
112         {
113             return ((Boolean) ignored.get(name)).booleanValue();
114         }
115         else
116         {
117             return ignoreAll;
118         }
119     }
120 
121     /**
122      * A property can be explicitly registered as a bean reference via registerBeanReference()
123      * or it can simply use the "-ref" suffix.
124      * @param attributeName true if the name appears to correspond to a reference
125      */
126     public boolean isReference(String attributeName)
127     {
128         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         return new SinglePropertyWrapper(name, this);
137     }
138 
139      /**
140      * Extract a JavaBean property name from the supplied attribute name.
141      * <p>The default implementation uses the {@link org.springframework.core.Conventions#attributeNameToPropertyName(String)}
142      * method to perform the extraction.
143      * <p>The name returned must obey the standard JavaBean property name
144      * conventions. For example for a class with a setter method
145      * '<code>setBingoHallFavourite(String)</code>', the name returned had
146      * better be '<code>bingoHallFavourite</code>' (with that exact casing).
147      *
148      * @param oldName the attribute name taken straight from the XML element being parsed; will never be <code>null</code>
149      * @return the extracted JavaBean property name; must never be <code>null</code>
150      */
151     public String translateName(String oldName)
152     {
153         // Remove the bean reference suffix if any.
154         String name = dropRef(oldName);
155         // Map to the real property name if necessary.
156         name = getAttributeMapping(name);
157         // JavaBeans property convention.
158         name = Conventions.attributeNameToPropertyName(name);
159         if (!StringUtils.hasText(name))
160         {
161             throw new IllegalStateException("Illegal property name for " + oldName + ": cannot be null or empty.");
162         }
163         return name;
164     }
165 
166     protected String dropRef(String name)
167     {
168         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         NamedValueMap vm = (NamedValueMap) valueMappings.get(name);
176         if (vm != null)
177         {
178             return vm.getValue(value);
179         }
180         else
181         {
182             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         {
194             this.propertyName = propertyName;
195             valueMap = new MapValueMap(mappingsString);
196         }
197 
198         public NamedValueMap(String propertyName, Map valueMap)
199         {
200             this.propertyName = propertyName;
201             this.valueMap = new MapValueMap(valueMap);
202         }
203 
204         public NamedValueMap(String propertyName, ValueMap valueMap)
205         {
206             this.propertyName = propertyName;
207             this.valueMap = valueMap;
208         }
209 
210         public String getPropertyName()
211         {
212             return propertyName;
213         }
214 
215         public Object getValue(String key)
216         {
217             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         {
228             this.map = map;
229         }
230 
231         public MapValueMap(String definition)
232         {
233             map = new HashMap();
234 
235             String[] values = StringUtils.tokenizeToStringArray(definition, ",");
236             for (int i = 0; i < values.length; i++)
237             {
238                 String value = values[i];
239                 int x = value.indexOf("=");
240                 if (x == -1)
241                 {
242                     throw new IllegalArgumentException("Mappings string not properly defined: " + definition);
243                 }
244                 map.put(value.substring(0, x), value.substring(x+1));
245             }
246 
247         }
248 
249         public Object rewrite(String value)
250         {
251             Object result = map.get(value);
252             if (null == result)
253             {
254                 return value;
255             }
256             else
257             {
258                 return result.toString();
259             }
260         }
261 
262     }
263     
264     public static class IndentityMapValueMap extends MapValueMap
265     {
266 
267         public IndentityMapValueMap(Map map)
268         {
269             super(map);
270         }
271 
272         @Override
273         public Object rewrite(String value)
274         {
275             Object result = map.get(value);
276             return result;
277         }
278     }
279 }