View Javadoc

1   /**
2    * This file Copyright (c) 2003-2014 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.Content;
37  import info.magnolia.cms.util.ContentUtil;
38  import info.magnolia.cms.util.SystemContentWrapper;
39  import info.magnolia.content2bean.Content2BeanException;
40  import info.magnolia.content2bean.Content2BeanTransformer;
41  import info.magnolia.content2bean.PropertyTypeDescriptor;
42  import info.magnolia.content2bean.TransformationState;
43  import info.magnolia.content2bean.TypeDescriptor;
44  import info.magnolia.content2bean.TypeMapping;
45  import info.magnolia.objectfactory.Classes;
46  import info.magnolia.objectfactory.ComponentProvider;
47  
48  import java.lang.reflect.Method;
49  import java.util.Collection;
50  import java.util.Iterator;
51  import java.util.LinkedHashMap;
52  import java.util.Locale;
53  import java.util.Map;
54  
55  import javax.inject.Inject;
56  import javax.inject.Singleton;
57  import javax.jcr.RepositoryException;
58  
59  import org.apache.commons.beanutils.BeanUtilsBean;
60  import org.apache.commons.beanutils.MethodUtils;
61  import org.apache.commons.beanutils.PropertyUtils;
62  import org.apache.commons.beanutils.PropertyUtilsBean;
63  import org.apache.commons.lang.LocaleUtils;
64  import org.apache.commons.lang.StringUtils;
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  /**
69   * Concrete implementation using reflection and adder methods.
70   */
71  @Singleton
72  public class Content2BeanTransformerImpl implements Content2BeanTransformer, Content.ContentFilter {
73  
74      private static final Logger log = LoggerFactory.getLogger(Content2BeanTransformerImpl.class);
75  
76      private final BeanUtilsBean beanUtilsBean;
77  
78      /**
79       * @deprecated should not be needed since we pass it around now... or will we ? ... TODO MAGNOLIA-3525
80       */
81      @Inject
82      private TypeMapping typeMapping;
83  
84      public Content2BeanTransformerImpl() {
85          super();
86  
87          // We use non-static BeanUtils conversion, so we can
88          // * use our custom ConvertUtilsBean
89          // * control converters (convertUtilsBean.register()) - we can register them here, locally, as opposed to a
90          // global ConvertUtils.register()
91          final EnumAwareConvertUtilsBean convertUtilsBean = new EnumAwareConvertUtilsBean();
92  
93          // de-register the converter for Class, we do our own conversion in convertPropertyValue()
94          convertUtilsBean.deregister(Class.class);
95  
96          this.beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
97      }
98  
99      @Override
100     @Deprecated
101     public TypeDescriptor resolveType(TransformationState state) throws ClassNotFoundException {
102         throw new UnsupportedOperationException();
103     }
104 
105     /**
106      * Resolves the <code>TypeDescriptor</code> from current transformation state. Resolving happens in the following
107      * order:
108      * <ul>
109      * <li>checks the class property of the current node
110      * <li>calls onResolve subclasses should override
111      * <li>reflection on the parent bean
112      * <li>in case of a collection/map type call getClassForCollectionProperty
113      * <li>otherwise use a Map
114      * </ul>
115      */
116     @Override
117     public TypeDescriptor resolveType(TypeMapping typeMapping, TransformationState state, ComponentProvider componentProvider) throws ClassNotFoundException {
118         TypeDescriptor typeDscr = null;
119         Content node = state.getCurrentContent();
120 
121         try {
122             if (node.hasNodeData("class")) {
123                 String className = node.getNodeData("class").getString();
124                 if (StringUtils.isBlank(className)) {
125                     throw new ClassNotFoundException("(no value for class property)");
126                 }
127                 Class<?> clazz = Classes.getClassFactory().forName(className);
128                 typeDscr = typeMapping.getTypeDescriptor(clazz);
129             }
130         } catch (RepositoryException e) {
131             // ignore
132             log.warn("can't read class property", e);
133         }
134 
135         if (typeDscr == null && state.getLevel() > 1) {
136             TypeDescriptor parentTypeDscr = state.getCurrentType();
137             PropertyTypeDescriptor propDscr;
138 
139             if (parentTypeDscr.isMap() || parentTypeDscr.isCollection()) {
140                 if (state.getLevel() > 2) {
141                     // this is not necessarily the parent node of the current
142                     String mapProperyName = state.peekContent(1).getName();
143                     propDscr = state.peekType(1).getPropertyTypeDescriptor(mapProperyName, typeMapping);
144                     if (propDscr != null) {
145                         typeDscr = propDscr.getCollectionEntryType();
146                     }
147                 }
148             } else {
149                 propDscr = state.getCurrentType().getPropertyTypeDescriptor(node.getName(), typeMapping);
150                 if (propDscr != null) {
151                     typeDscr = propDscr.getType();
152                 }
153             }
154         }
155 
156         typeDscr = onResolveType(typeMapping, state, typeDscr, componentProvider);
157 
158         if (typeDscr != null) {
159             // might be that the factory util defines a default implementation for interfaces
160             final Class<?> type = typeDscr.getType();
161             typeDscr = typeMapping.getTypeDescriptor(componentProvider.getImplementation(type));
162 
163             // now that we know the property type we should delegate to the custom transformer if any defined
164             Content2BeanTransformer customTransformer = typeDscr.getTransformer();
165             if (customTransformer != null && customTransformer != this) {
166                 TypeDescriptor typeFoundByCustomTransformer = customTransformer.resolveType(typeMapping, state, componentProvider);
167                 // if no specific type has been provided by the
168                 // TODO - is this comparison working ?
169                 if (typeFoundByCustomTransformer != TypeMapping.MAP_TYPE) {
170                     // might be that the factory util defines a default implementation for interfaces
171                     Class<?> implementation = componentProvider.getImplementation(typeFoundByCustomTransformer.getType());
172                     typeDscr = typeMapping.getTypeDescriptor(implementation);
173                 }
174             }
175         }
176 
177         if (typeDscr == null || typeDscr.needsDefaultMapping()) {
178             if (typeDscr == null) {
179                 log.debug("was not able to resolve type for node [{}] will use a map", node);
180             }
181             typeDscr = TypeMapping.MAP_TYPE;
182         }
183 
184         log.debug("{} --> {}", node.getHandle(), typeDscr.getType());
185 
186         return typeDscr;
187     }
188 
189     /**
190      * Called once the type should have been resolved. The resolvedType might be null if no type has been resolved.
191      * After the call the FactoryUtil and custom transformers are used to get the final type. TODO - check javadoc
192      */
193     protected TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
194         return resolvedType;
195     }
196 
197     /**
198      * @deprecated since 4.5, use {@link #onResolveType(info.magnolia.content2bean.TypeMapping, info.magnolia.content2bean.TransformationState, info.magnolia.content2bean.TypeDescriptor, info.magnolia.objectfactory.ComponentProvider)}
199      */
200     protected TypeDescriptor onResolveType(TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
201         return onResolveType(getTypeMapping(), state, resolvedType, componentProvider);
202     }
203 
204     @Override
205     public Collection<Content> getChildren(Content node) {
206         return node.getChildren(this);
207     }
208 
209     /**
210      * Process all nodes except MetaData and nodes with names prefixed by "jcr:".
211      */
212     @Override
213     public boolean accept(Content content) {
214         return ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER.accept(content);
215     }
216 
217     @Override
218     public void setProperty(TransformationState state, PropertyTypeDescriptor descriptor, Map<String, Object> values) {
219         throw new UnsupportedOperationException();
220     }
221 
222     /**
223      * Do not set class property. In case of a map/collection try to use adder method.
224      */
225     @Override
226     public void setProperty(TypeMapping mapping, TransformationState state, PropertyTypeDescriptor descriptor, Map<String, Object> values) {
227         String propertyName = descriptor.getName();
228         if (propertyName.equals("class")) {
229             return;
230         }
231         Object value = values.get(propertyName);
232         Object bean = state.getCurrentBean();
233 
234         if (propertyName.equals("content") && value == null) {
235             value = new SystemContentWrapper(state.getCurrentContent());
236         } else if (propertyName.equals("name") && value == null) {
237             value = state.getCurrentContent().getName();
238         } else if (propertyName.equals("className") && value == null) {
239             value = values.get("class");
240         }
241 
242         // do no try to set a bean-property that has no corresponding node-property
243         // else if (!values.containsKey(propertyName)) {
244         if (value == null) {
245             return;
246         }
247 
248         log.debug("try to set {}.{} with value {}", new Object[] { bean, propertyName, value });
249 
250         // if the parent bean is a map, we can't guess the types.
251         if (!(bean instanceof Map)) {
252             try {
253                 PropertyTypeDescriptor dscr = mapping.getPropertyTypeDescriptor(bean.getClass(), propertyName);
254                 if (dscr.getType() != null) {
255 
256                     // try to use an adder method for a Collection property of the bean
257                     if (dscr.isCollection() || dscr.isMap()) {
258                         log.debug("{} is of type collection, map or /array", propertyName);
259                         Method method = dscr.getAddMethod();
260 
261                         if (method != null) {
262                             log.debug("clearing the current content of the collection/map");
263                             try {
264                                 Object col = PropertyUtils.getProperty(bean, propertyName);
265                                 if (col != null) {
266                                     MethodUtils.invokeExactMethod(col, "clear", new Object[] {});
267                                 }
268                             } catch (Exception e) {
269                                 log.debug("no clear method found on collection {}", propertyName);
270                             }
271 
272                             Class<?> entryClass = dscr.getCollectionEntryType().getType();
273 
274                             log.debug("will add values by using adder method {}", method.getName());
275                             for (Iterator<Object> iter = ((Map<Object, Object>) value).keySet().iterator(); iter
276                                     .hasNext();) {
277                                 Object key = iter.next();
278                                 Object entryValue = ((Map<Object, Object>) value).get(key);
279                                 entryValue = convertPropertyValue(entryClass, entryValue);
280                                 if (entryClass.isAssignableFrom(entryValue.getClass())) {
281                                     if (dscr.isCollection()) {
282                                         log.debug("will add value {}", entryValue);
283                                         method.invoke(bean, new Object[] { entryValue });
284                                     }
285                                     // is a map
286                                     else {
287                                         log.debug("will add key {} with value {}", key, entryValue);
288                                         method.invoke(bean, new Object[] { key, entryValue });
289                                     }
290                                 }
291                             }
292 
293                             return;
294                         }
295                         log.debug("no add method found for property {}", propertyName);
296                         if (dscr.isCollection()) {
297                             log.debug("transform the values to a collection", propertyName);
298                             value = ((Map<Object, Object>) value).values();
299                         }
300                     } else {
301                         value = convertPropertyValue(dscr.getType().getType(), value);
302                     }
303                 }
304             } catch (Exception e) {
305                 // do it better
306                 log.error("Can't set property [{}] to value [{}] in bean [{}] for node {} due to {}",
307                         new Object[] { propertyName, value, bean.getClass().getName(),
308                                 state.getCurrentContent().getHandle(), e.toString() });
309                 log.debug("stacktrace", e);
310             }
311         }
312 
313         try {
314             // This uses the converters registered in beanUtilsBean.convertUtilsBean (see constructor of this class)
315             // If a converter is registered, beanutils will convert value.toString(), not the value object as-is.
316             // If no converter is registered, then the value Object is set as-is.
317             // If convertPropertyValue() already converted this value, you'll probably want to unregister the beanutils
318             // converter.
319             // some conversions like string to class. Performance of PropertyUtils.setProperty() would be better
320             beanUtilsBean.setProperty(bean, propertyName, value);
321 
322             // TODO this also does things we probably don't want/need, i.e nested and indexed properties
323 
324         } catch (Exception e) {
325             // do it better
326             log.error("Can't set property [{}] to value [{}] in bean [{}] for node {} due to {}",
327                     new Object[] { propertyName, value, bean.getClass().getName(),
328                             state.getCurrentContent().getHandle(), e.toString() });
329             log.debug("stacktrace", e);
330         }
331 
332     }
333 
334     /**
335      * Most of the conversion is done by the BeanUtils. TODO don't use bean utils conversion since it can't be used for
336      * the adder methods
337      */
338     @Override
339     public Object convertPropertyValue(Class<?> propertyType, Object value) throws Content2BeanException {
340         if (Class.class.equals(propertyType)) {
341             try {
342                 return Classes.getClassFactory().forName(value.toString());
343             } catch (ClassNotFoundException e) {
344                 log.error(e.getMessage());
345                 throw new Content2BeanException(e);
346             }
347         }
348 
349         if (Locale.class.equals(propertyType)) {
350             if (value instanceof String) {
351                 String localeStr = (String) value;
352                 if (StringUtils.isNotEmpty(localeStr)) {
353                     return LocaleUtils.toLocale(localeStr);
354                 }
355             }
356         }
357 
358         if (Collection.class.equals(propertyType) && value instanceof Map) {
359             // TODO never used ?
360             return ((Map) value).values();
361         }
362 
363         // this is mainly the case when we are flattening node hierarchies
364         if (String.class.equals(propertyType) && value instanceof Map && ((Map) value).size() == 1) {
365             return ((Map) value).values().iterator().next();
366         }
367 
368         return value;
369     }
370 
371     /**
372      * Use the factory util to instantiate. This is useful to get default implementation of interfaces
373      */
374     @Override
375     public Object newBeanInstance(TransformationState state, Map properties, ComponentProvider componentProvider) throws Content2BeanException {
376         // we try first to use conversion (Map --> primitive type)
377         // this is the case when we flattening the hierarchy?
378         final Object bean = convertPropertyValue(state.getCurrentType().getType(), properties);
379         // were the properties transformed?
380         if (bean == properties) {
381             try {
382                 // TODO MAGNOLIA-2569 MAGNOLIA-3525 what is going on here ? (added the following if to avoid permanently
383                 // requesting LinkedHashMaps to ComponentFactory)
384                 final Class<?> type = state.getCurrentType().getType();
385                 if (LinkedHashMap.class.equals(type)) {
386                     // TODO - as far as I can tell, "bean" and "properties" are already the same instance of a
387                     // LinkedHashMap, so what are we doing in here ?
388                     return new LinkedHashMap();
389                 } else if (Map.class.isAssignableFrom(type)) {
390                     // TODO ?
391                     log.warn("someone wants another type of map ? " + type);
392                 }
393                 return componentProvider.newInstance(type);
394             } catch (Throwable e) {
395                 throw new Content2BeanException(e);
396             }
397         }
398         return bean;
399     }
400 
401     /**
402      * Initializes bean by calling its init method if present.
403      */
404     @Override
405     public void initBean(TransformationState state, Map properties) throws Content2BeanException {
406         Object bean = state.getCurrentBean();
407 
408         Method init;
409         try {
410             init = bean.getClass().getMethod("init", new Class[] {});
411             try {
412                 init.invoke(bean); // no parameters
413             } catch (Exception e) {
414                 throw new Content2BeanException("can't call init method", e);
415             }
416         } catch (SecurityException e) {
417             return;
418         } catch (NoSuchMethodException e) {
419             return;
420         }
421         log.debug("{} is initialized", bean);
422     }
423 
424     @Override
425     public TransformationState newState() {
426         return new TransformationStateImpl();
427         // TODO - do we really need different impls for TransformationState ?
428         // if so, this was defined in mgnl-beans.properties
429         // Components.getComponentProvider().newInstance(TransformationState.class);
430     }
431 
432     /**
433      * Returns the default mapping.
434      * 
435      * @deprecated since 4.5, do not use.
436      */
437     @Override
438     public TypeMapping getTypeMapping() {
439         return typeMapping;// TypeMapping.Factory.getDefaultMapping();
440     }
441 
442 }