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   * @author philipp
61   * @version $Id$
62   *
63   */
64  @Singleton
65  public class TypeMappingImpl implements TypeMapping {
66  
67      private static Logger log = LoggerFactory.getLogger(TypeMappingImpl.class);
68  
69      /**
70       * Property types already resolved.
71       */
72      private final Map<String, PropertyTypeDescriptor> propertyTypes = new HashMap<String, PropertyTypeDescriptor>();
73  
74      /**
75       * Descriptors for types.
76       **/
77      private final Map<Class<?>, TypeDescriptor> types = new HashMap<Class<?>, TypeDescriptor>();
78  
79      /**
80       * Get a adder method. Transforms name to singular.
81       */
82      public Method getAddMethod(Class<?> type, String name, int numberOfParameters) {
83          name = StringUtils.capitalize(name);
84          Method method = getExactMethod(type, "add" + name, numberOfParameters);
85          if (method == null) {
86              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "s"), numberOfParameters);
87          }
88  
89          if (method == null) {
90              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "es"), numberOfParameters);
91          }
92  
93          if (method == null) {
94              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "ren"), numberOfParameters);
95          }
96  
97          if (method == null) {
98              method = getExactMethod(type, "add" + StringUtils.removeEnd(name, "ies") + "y", numberOfParameters);
99          }
100         return method;
101     }
102 
103     /**
104      * Cache the already resolved types.
105      *
106      */
107     @Override
108     public PropertyTypeDescriptor getPropertyTypeDescriptor(Class<?> beanClass, String propName) {
109         PropertyTypeDescriptor dscr;
110         String key = beanClass.getName() + "." + propName;
111 
112         dscr = propertyTypes.get(key);
113         if(dscr != null){
114             return dscr;
115         }
116 
117         //TypeMapping defaultMapping = TypeMapping.Factory.getDefaultMapping();
118         // TODO - is this used - or is the comparison correct ?
119 //        if (this != defaultMapping) {
120 //            dscr = defaultMapping.getPropertyTypeDescriptor(beanClass, propName);
121 //            if (dscr.getType()  != null) {
122 //                return dscr;
123 //            }
124 //        }
125 
126         dscr = new PropertyTypeDescriptor();
127         dscr.setName(propName);
128 
129         PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
130         for (int i = 0; i < descriptors.length; i++) {
131             PropertyDescriptor descriptor = descriptors[i];
132             if (descriptor.getName().equals(propName)) {
133                 Class<?> propertytype = descriptor.getPropertyType(); // may be null for indexed properties
134                 if (propertytype != null) {
135                     dscr.setType(getTypeDescriptor(propertytype));
136                 }
137                 break;
138             }
139         }
140 
141         if(dscr.getType() != null){
142             if(dscr.isMap() || dscr.isCollection()){
143                 int numberOfParameters = dscr.isMap() ? 2 : 1;
144                 Method method = getAddMethod(beanClass, propName, numberOfParameters);
145                 if(method != null){
146                     dscr.setAddMethod(method);
147                     if(dscr.isMap()){
148                         dscr.setCollectionKeyType(getTypeDescriptor(method.getParameterTypes()[0]));
149                         dscr.setCollectionEntryType(getTypeDescriptor(method.getParameterTypes()[1]));
150                     }
151                     else{
152                         dscr.setCollectionEntryType(getTypeDescriptor(method.getParameterTypes()[0]));
153                     }
154                 }
155             }
156         }
157 
158         // remember me
159         propertyTypes.put(key, dscr);
160 
161         return dscr;
162     }
163 
164     @Override
165     public void addPropertyTypeDescriptor(Class<?> beanClass, String propName, PropertyTypeDescriptor dscr) {
166         propertyTypes.put(beanClass.getName() + "." + propName, dscr);
167     }
168 
169     @Override
170     public void addTypeDescriptor(Class<?> beanClass, TypeDescriptor dscr) {
171         types.put(beanClass, dscr);
172     }
173 
174     @Override
175     public TypeDescriptor getTypeDescriptor(Class<?> beanClass) {
176         TypeDescriptor dscr = types.get(beanClass);
177         // eh, we know about this type, don't bother resolving any further.
178         if(dscr != null){
179             return dscr;
180         }
181         dscr = new TypeDescriptor();
182         dscr.setType(beanClass);
183         dscr.setMap(Map.class.isAssignableFrom(beanClass));
184         dscr.setCollection(beanClass.isArray() || Collection.class.isAssignableFrom(beanClass));
185         types.put(beanClass, dscr);
186 
187         if (!beanClass.isArray() && !beanClass.isPrimitive()) { // don't bother looking for a transformer if the property is an array or a primitive type
188             Content2BeanTransformer transformer = null; // TODO ? transformerProvider.getTransformerFor(beanClass);
189             try {
190                 if (transformer == null) {
191                     transformer = findTransformerByNamingConvention(beanClass);
192                 }
193                 if (transformer == null) {
194                     transformer = findTransformerViaProperty(beanClass);
195                 }
196             } catch (Exception e) {
197                 // this is fine because having a transformer class is optional
198                 log.debug("No custom transformer class {}Transformer class found", beanClass.getName());
199             }
200             dscr.setTransformer(transformer);
201         }
202         return dscr;
203     }
204 
205     /**
206      * @deprecated since 4.5, transformers should be explicitly registered via the module descriptor.
207      */
208     protected Content2BeanTransformer findTransformerByNamingConvention(Class<?> beanClass) {
209         final String transformerClassName = beanClass.getName() + "Transformer";
210         try {
211             return instantiateTransformer(beanClass, transformerClassName);
212         } catch (ClassNotFoundException e) {
213             log.debug("No transformer found by naming convention for {} (attempted to load {})", beanClass, transformerClassName);
214             return null;
215         }
216     }
217 
218     /**
219      * This was originally implemented by {@link info.magnolia.content2bean.impl.PropertiesBasedTypeMapping}.
220      * @deprecated since 4.5, transformers should be explicitly registered via the module descriptor.
221      */
222     protected Content2BeanTransformer findTransformerViaProperty(Class<?> beanClass) throws ClassNotFoundException {
223         final String property = SystemProperty.getProperty(beanClass.getName() + ".transformer");
224         if (property != null) {
225             return instantiateTransformer(beanClass,property);
226         }
227         return null;
228     }
229 
230     protected Content2BeanTransformer instantiateTransformer(Class<?> beanClass, String transformerClassName) throws ClassNotFoundException {
231         final ClassFactory classFactory = Classes.getClassFactory();
232         final Class<Content2BeanTransformer> transformerClass = classFactory.forName(transformerClassName);
233 
234         if (Content2BeanTransformer.class.isAssignableFrom(transformerClass)) {
235             try {
236                 log.debug("Found a custom transformer [{" + transformerClass + "}] for [{" + beanClass + "}]");
237                 // TODO use components ?
238                 return classFactory.newInstance(transformerClass);
239             } catch (Exception e) {
240                 log.error("Can't instantiate custom transformer [{" + transformerClass + "}] for [{" + beanClass + "}]", e);
241             }
242         }
243         return null;
244     }
245 
246     /**
247      * Find a method.
248      *
249      * @param numberOfParameters
250      */
251     protected Method getExactMethod(Class<?> type, String name, int numberOfParameters) {
252         Method[] methods = type.getMethods();
253         for (int i = 0; i < methods.length; i++) {
254             Method method = methods[i];
255             if (method.getName().equals(name)) {
256                 // TODO - CAUTION: in case there's several methods with the same name and the same numberOfParameters
257                 // this method might pick the "wrong" one. We should think about adding a check and throw an exceptions
258                 // if there's more than one match!
259                 if (method.getParameterTypes().length == numberOfParameters) {
260                     return method;
261                 }
262             }
263         }
264         return null;
265     }
266 
267 }