View Javadoc

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