View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.content2bean.impl;
35  
36  import info.magnolia.cms.core.SystemProperty;
37  import info.magnolia.content2bean.Content2BeanTransformer;
38  import info.magnolia.content2bean.PropertyTypeDescriptor;
39  import info.magnolia.content2bean.TypeDescriptor;
40  import info.magnolia.content2bean.TypeMapping;
41  import info.magnolia.objectfactory.ClassFactory;
42  import info.magnolia.objectfactory.Classes;
43  
44  import java.beans.PropertyDescriptor;
45  import java.lang.reflect.Method;
46  import java.util.Collection;
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  import javax.inject.Singleton;
51  
52  import org.apache.commons.beanutils.PropertyUtils;
53  import org.apache.commons.lang.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  
58  /**
59   * Basic type mapping implementation.
60   */
61  @Singleton
62  public class TypeMappingImpl implements TypeMapping {
63  
64      private static Logger log = LoggerFactory.getLogger(TypeMappingImpl.class);
65  
66      /**
67       * Property types already resolved.
68       */
69      private final Map<String, PropertyTypeDescriptor> propertyTypes = new HashMap<String, PropertyTypeDescriptor>();
70  
71      /**
72       * Descriptors for types.
73       **/
74      private final Map<Class<?>, TypeDescriptor> types = new HashMap<Class<?>, TypeDescriptor>();
75  
76      /**
77       * Get a adder method. Transforms name to singular.
78       */
79      public Method getAddMethod(Class<?> type, String name, int numberOfParameters) {
80          name = StringUtils.capitalize(name);
81          Method method = getExactMethod(type, "add" + name, numberOfParameters);
82          if (method == null) {
83              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "s"), numberOfParameters);
84          }
85  
86          if (method == null) {
87              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "es"), numberOfParameters);
88          }
89  
90          if (method == null) {
91              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "ren"), numberOfParameters);
92          }
93  
94          if (method == null) {
95              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "ies") + "y", numberOfParameters);
96          }
97          return method;
98      }
99  
100     /**
101      * Cache the already resolved types.
102      *
103      */
104     @Override
105     public PropertyTypeDescriptor getPropertyTypeDescriptor(Class<?> beanClass, String propName) {
106         PropertyTypeDescriptor dscr;
107         String key = beanClass.getName() + "." + propName;
108 
109         dscr = propertyTypes.get(key);
110         if(dscr != null){
111             return dscr;
112         }
113 
114         //TypeMapping defaultMapping = TypeMapping.Factory.getDefaultMapping();
115         // TODO - is this used - or is the comparison correct ?
116 //        if (this != defaultMapping) {
117 //            dscr = defaultMapping.getPropertyTypeDescriptor(beanClass, propName);
118 //            if (dscr.getType()  != null) {
119 //                return dscr;
120 //            }
121 //        }
122 
123         dscr = new PropertyTypeDescriptor();
124         dscr.setName(propName);
125 
126         PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
127         for (int i = 0; i < descriptors.length; i++) {
128             PropertyDescriptor descriptor = descriptors[i];
129             if (descriptor.getName().equals(propName)) {
130                 Class<?> propertytype = descriptor.getPropertyType(); // may be null for indexed properties
131                 if (propertytype != null) {
132                     dscr.setType(getTypeDescriptor(propertytype));
133                 }
134                 break;
135             }
136         }
137 
138         if(dscr.getType() != null){
139             if(dscr.isMap() || dscr.isCollection()){
140                 int numberOfParameters = dscr.isMap() ? 2 : 1;
141                 Method method = getAddMethod(beanClass, propName, numberOfParameters);
142                 if(method != null){
143                     dscr.setAddMethod(method);
144                     if(dscr.isMap()){
145                         dscr.setCollectionKeyType(getTypeDescriptor(method.getParameterTypes()[0]));
146                         dscr.setCollectionEntryType(getTypeDescriptor(method.getParameterTypes()[1]));
147                     }
148                     else{
149                         dscr.setCollectionEntryType(getTypeDescriptor(method.getParameterTypes()[0]));
150                     }
151                 }
152             }
153         }
154 
155         // remember me
156         propertyTypes.put(key, dscr);
157 
158         return dscr;
159     }
160 
161     @Override
162     public void addPropertyTypeDescriptor(Class<?> beanClass, String propName, PropertyTypeDescriptor dscr) {
163         propertyTypes.put(beanClass.getName() + "." + propName, dscr);
164     }
165 
166     @Override
167     public void addTypeDescriptor(Class<?> beanClass, TypeDescriptor dscr) {
168         types.put(beanClass, dscr);
169     }
170 
171     @Override
172     public TypeDescriptor getTypeDescriptor(Class<?> beanClass) {
173         TypeDescriptor dscr = types.get(beanClass);
174         // eh, we know about this type, don't bother resolving any further.
175         if(dscr != null){
176             return dscr;
177         }
178         dscr = new TypeDescriptor();
179         dscr.setType(beanClass);
180         dscr.setMap(Map.class.isAssignableFrom(beanClass));
181         dscr.setCollection(beanClass.isArray() || Collection.class.isAssignableFrom(beanClass));
182         types.put(beanClass, dscr);
183 
184         if (!beanClass.isArray() && !beanClass.isPrimitive()) { // don't bother looking for a transformer if the property is an array or a primitive type
185             Content2BeanTransformer transformer = null; // TODO ? transformerProvider.getTransformerFor(beanClass);
186             try {
187                 transformer = findTransformerByNamingConvention(beanClass);
188                 if (transformer == null) {
189                     transformer = findTransformerViaProperty(beanClass);
190                 }
191             } catch (Exception e) {
192                 // this is fine because having a transformer class is optional
193                 log.debug("No custom transformer class {}Transformer class found", beanClass.getName());
194             }
195             dscr.setTransformer(transformer);
196         }
197         return dscr;
198     }
199 
200     /**
201      * @deprecated since 4.5, transformers should be explicitly registered via the module descriptor.
202      */
203     protected Content2BeanTransformer findTransformerByNamingConvention(Class<?> beanClass) {
204         final String transformerClassName = beanClass.getName() + "Transformer";
205         try {
206             return instantiateTransformer(beanClass, transformerClassName);
207         } catch (ClassNotFoundException e) {
208             log.debug("No transformer found by naming convention for {} (attempted to load {})", beanClass, transformerClassName);
209             return null;
210         }
211     }
212 
213     /**
214      * This was originally implemented by {@link info.magnolia.content2bean.impl.PropertiesBasedTypeMapping}.
215      * @deprecated since 4.5, transformers should be explicitly registered via the module descriptor.
216      */
217     protected Content2BeanTransformer findTransformerViaProperty(Class<?> beanClass) throws ClassNotFoundException {
218         final String property = SystemProperty.getProperty(beanClass.getName() + ".transformer");
219         if (property != null) {
220             return instantiateTransformer(beanClass,property);
221         }
222         return null;
223     }
224 
225     protected Content2BeanTransformer instantiateTransformer(Class<?> beanClass, String transformerClassName) throws ClassNotFoundException {
226         final ClassFactory classFactory = Classes.getClassFactory();
227         final Class<Content2BeanTransformer> transformerClass = classFactory.forName(transformerClassName);
228 
229         if (Content2BeanTransformer.class.isAssignableFrom(transformerClass)) {
230             try {
231                 log.debug("Found a custom transformer [{" + transformerClass + "}] for [{" + beanClass + "}]");
232                 // TODO use components ?
233                 return classFactory.newInstance(transformerClass);
234             } catch (Exception e) {
235                 log.error("Can't instantiate custom transformer [{" + transformerClass + "}] for [{" + beanClass + "}]", e);
236             }
237         }
238         return null;
239     }
240 
241     /**
242      * Find a method.
243      */
244     protected Method getExactMethod(Class<?> type, String name, int numberOfParameters) {
245         Method[] methods = type.getMethods();
246         for (int i = 0; i < methods.length; i++) {
247             Method method = methods[i];
248             if (method.getName().equals(name)) {
249                 // TODO - CAUTION: in case there's several methods with the same name and the same numberOfParameters
250                 // this method might pick the "wrong" one. We should think about adding a check and throw an exceptions
251                 // if there's more than one match!
252                 if (method.getParameterTypes().length == numberOfParameters) {
253                     return method;
254                 }
255             }
256         }
257         return null;
258     }
259 
260 }