View Javadoc
1   /**
2    * This file Copyright (c) 2012-2015 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.jcr.node2bean.impl;
35  
36  import info.magnolia.cms.util.ContentUtil;
37  import info.magnolia.cms.util.SystemContentWrapper;
38  import info.magnolia.jcr.iterator.FilteringNodeIterator;
39  import info.magnolia.jcr.node2bean.Node2BeanException;
40  import info.magnolia.jcr.node2bean.Node2BeanTransformer;
41  import info.magnolia.jcr.node2bean.PropertyTypeDescriptor;
42  import info.magnolia.jcr.node2bean.TransformationState;
43  import info.magnolia.jcr.node2bean.TypeDescriptor;
44  import info.magnolia.jcr.node2bean.TypeMapping;
45  import info.magnolia.jcr.predicate.AbstractPredicate;
46  import info.magnolia.jcr.util.NodeTypes;
47  import info.magnolia.objectfactory.Classes;
48  import info.magnolia.objectfactory.ComponentProvider;
49  import info.magnolia.objectfactory.Components;
50  
51  import java.lang.reflect.Array;
52  import java.lang.reflect.Constructor;
53  import java.lang.reflect.InvocationTargetException;
54  import java.lang.reflect.Method;
55  import java.util.ArrayList;
56  import java.util.Collection;
57  import java.util.HashSet;
58  import java.util.Iterator;
59  import java.util.LinkedHashMap;
60  import java.util.LinkedList;
61  import java.util.List;
62  import java.util.Locale;
63  import java.util.Map;
64  import java.util.Queue;
65  import java.util.Set;
66  
67  import javax.inject.Inject;
68  import javax.jcr.Node;
69  import javax.jcr.NodeIterator;
70  import javax.jcr.RepositoryException;
71  
72  import org.apache.commons.beanutils.BeanUtilsBean;
73  import org.apache.commons.beanutils.Converter;
74  import org.apache.commons.beanutils.MethodUtils;
75  import org.apache.commons.beanutils.PropertyUtils;
76  import org.apache.commons.lang3.LocaleUtils;
77  import org.apache.commons.lang3.StringUtils;
78  import org.slf4j.Logger;
79  import org.slf4j.LoggerFactory;
80  
81  import com.google.common.collect.Iterables;
82  
83  /**
84   * Concrete implementation using reflection, generics and setter methods.
85   */
86  public class Node2BeanTransformerImpl implements Node2BeanTransformer {
87  
88      private static final Logger log = LoggerFactory.getLogger(Node2BeanTransformerImpl.class);
89  
90      private final BeanUtilsBean beanUtilsBean;
91  
92      private final Class<?> defaultListImpl;
93  
94      private final Class<?> defaultSetImpl;
95  
96      private final Class<?> defaultQueueImpl;
97  
98      /**
99       * @deprecated since 5.4 - use IoC, don't instantiate directly. You should not have to instantiate this if you
100      * use magnolia-configuration mechanisms introduced in 5.4.
101      */
102     @Deprecated
103     public Node2BeanTransformerImpl() {
104         this(Components.getComponent(PreConfiguredBeanUtils.class));
105     }
106 
107     @Inject
108     public Node2BeanTransformerImpl(PreConfiguredBeanUtils beanUtilsBean) {
109         this(beanUtilsBean, LinkedList.class, HashSet.class, LinkedList.class);
110     }
111 
112     /**
113      * @deprecated since 5.4 - use IoC, don't instantiate directly. You should not have to instantiate this if you
114      * use magnolia-configuration mechanisms introduced in 5.4.
115      */
116     @Deprecated
117     public Node2BeanTransformerImpl(Class<?> defaultListImpl, Class<?> defaultSetImpl, Class<?> defaultQueueImpl) {
118         this(Components.getComponent(PreConfiguredBeanUtils.class), defaultListImpl, defaultSetImpl, defaultQueueImpl);
119     }
120 
121     protected Node2BeanTransformerImpl(PreConfiguredBeanUtils beanUtilsBean, Class<?> defaultListImpl, Class<?> defaultSetImpl, Class<?> defaultQueueImpl) {
122         this.beanUtilsBean = beanUtilsBean;
123         this.defaultListImpl = defaultListImpl;
124         this.defaultSetImpl = defaultSetImpl;
125         this.defaultQueueImpl = defaultQueueImpl;
126 
127         // TODO GJ/MM : Michi commented the below in ToBeanBeanUtils - why ?
128         beanUtilsBean.getConvertUtils().deregister(Class.class);
129     }
130 
131     @Override
132     public TransformationState newState() {
133         return new TransformationStateImpl();
134     }
135 
136     @Override
137     public TypeDescriptor resolveType(TypeMapping typeMapping, TransformationState state, ComponentProvider componentProvider) throws ClassNotFoundException, RepositoryException {
138         TypeDescriptor typeDscr = null;
139         Node node = state.getCurrentNode();
140 
141         try {
142             if (node.hasProperty("class")) {
143                 String className = StringUtils.trim(node.getProperty("class").getString());
144                 if (StringUtils.isBlank(className)) {
145                     log.warn("Cannot resolve type for node [{}] because class property has empty value.", node);
146                 } else {
147                     Class<?> clazz = Classes.getClassFactory().forName(className);
148                     typeDscr = typeMapping.getTypeDescriptor(clazz);
149                 }
150             }
151         } catch (RepositoryException e) {
152             log.warn("Can't read class property from node [{}]", node.getPath(), e);
153         }
154 
155         if (typeDscr == null && state.getLevel() > 1) {
156             TypeDescriptor parentTypeDscr = state.getCurrentType();
157             PropertyTypeDescriptor propDscr;
158 
159             if (parentTypeDscr.isMap() || parentTypeDscr.isCollection()) {
160                 if (state.getLevel() > 2) {
161                     // this is not necessarily the parent node of the current
162                     String mapProperyName = state.peekNode(1).getName();
163                     propDscr = state.peekType(1).getPropertyTypeDescriptor(mapProperyName, typeMapping);
164                     if (propDscr != null) {
165                         typeDscr = propDscr.getCollectionEntryType();
166                     }
167                 }
168             } else {
169                 propDscr = state.getCurrentType().getPropertyTypeDescriptor(node.getName(), typeMapping);
170                 if (propDscr != null) {
171                     typeDscr = propDscr.getType();
172                 }
173             }
174         }
175 
176         typeDscr = onResolveType(typeMapping, state, typeDscr, componentProvider);
177 
178         if (typeDscr != null) {
179             // might be that the factory util defines a default implementation for interfaces
180             final Class<?> type = typeDscr.getType();
181             typeDscr = typeMapping.getTypeDescriptor(componentProvider.getImplementation(type));
182 
183             // now that we know the property type we should delegate to the custom transformer if any defined
184             Node2BeanTransformer customTransformer = typeDscr.getTransformer();
185             if (customTransformer != null && customTransformer != this) {
186                 TypeDescriptor typeFoundByCustomTransformer = customTransformer.resolveType(typeMapping, state, componentProvider);
187                 // if no specific type has been provided by the
188                 // TODO - TypeDescriptor - equals and hashCode impl and use
189                 // not equals instead of !=
190                 if (typeFoundByCustomTransformer != TypeMapping.MAP_TYPE) {
191                     // might be that the factory util defines a default implementation for interfaces
192                     Class<?> implementation = componentProvider.getImplementation(typeFoundByCustomTransformer.getType());
193                     typeDscr = typeMapping.getTypeDescriptor(implementation);
194                 }
195             }
196         }
197 
198         if (typeDscr == null || typeDscr.needsDefaultMapping()) {
199             if (typeDscr == null) {
200                 log.debug("Was not able to resolve type for node [{}] will use a map", node);
201             }
202             typeDscr = TypeMapping.MAP_TYPE;
203         }
204         log.debug("Resolved type [{}] for node [{}]", typeDscr.getType(), node.getPath());
205 
206         return typeDscr;
207     }
208 
209     @Override
210     public NodeIterator getChildren(Node node) throws RepositoryException {
211         // TODO create predicate into separate class, <? extends Item> ItemHidingPredicate (regexp)
212         return new FilteringNodeIterator(node.getNodes(), new AbstractPredicate<Node>() {
213             @Override
214             public boolean evaluateTyped(Node t) {
215                 try {
216                     return !(t.getName().startsWith(NodeTypes.JCR_PREFIX) || t.isNodeType(NodeTypes.MetaData.NAME));
217                 } catch (RepositoryException e) {
218                     return false;
219                 }
220             }
221         });
222     }
223 
224     @Override
225     public Object newBeanInstance(TransformationState state, Map<String, Object> values, ComponentProvider componentProvider) throws Node2BeanException {
226         // we try first to use conversion (Map --> primitive type)
227         // this is the case when we flattening the hierarchy?
228         final Object bean = convertPropertyValue(state.getCurrentType().getType(), values);
229         // were the properties transformed?
230         if (bean == values) {
231             try {
232                 // is this property remove necessary?
233                 values.remove("class");
234                 final Class<?> type = state.getCurrentType().getType();
235                 if (LinkedHashMap.class.equals(type)) {
236                     // TODO - as far as I can tell, "bean" and "properties" are already the same instance of a
237                     // LinkedHashMap, so what are we doing in here ?
238                     return new LinkedHashMap();
239                 } else if (Map.class.isAssignableFrom(type)) {
240                     // TODO ?
241                     log.warn("someone wants another type of map ? {}", type);
242                 } else if (Collection.class.isAssignableFrom(type)) {
243                     // someone wants specific collection
244                     return type.newInstance();
245                 }
246                 return componentProvider.newInstance(type);
247             } catch (Throwable e) {
248                 throw new Node2BeanException(e);
249             }
250         }
251         return bean;
252     }
253 
254     @Override
255     public void initBean(TransformationState state, Map values) throws Node2BeanException {
256         Object bean = state.getCurrentBean();
257 
258         Method init;
259         try {
260             init = bean.getClass().getMethod("init");
261             try {
262                 init.invoke(bean); // no parameters
263             } catch (Exception e) {
264                 throw new Node2BeanException("can't call init method", e);
265             }
266         } catch (SecurityException e) {
267             return;
268         } catch (NoSuchMethodException e) {
269             return;
270         }
271         log.debug("{} is initialized", bean);
272 
273     }
274 
275     @Override
276     public Object convertPropertyValue(Class<?> propertyType, Object value) throws Node2BeanException {
277         if (Class.class.equals(propertyType)) {
278             try {
279                 return Classes.getClassFactory().forName(value.toString());
280             } catch (ClassNotFoundException e) {
281                 log.error("Can't convert property. Class for type [{}] not found.", propertyType);
282                 throw new Node2BeanException(e);
283             }
284         }
285 
286         if (Locale.class.equals(propertyType)) {
287             if (value instanceof String) {
288                 String localeStr = (String) value;
289                 if (StringUtils.isNotEmpty(localeStr)) {
290                     return LocaleUtils.toLocale(localeStr);
291                 }
292             }
293         }
294 
295         if (Collection.class.equals(propertyType) && value instanceof Map) {
296             // TODO never used ?
297             return ((Map) value).values();
298         }
299 
300         // this is mainly the case when we are flattening node hierarchies
301         if (String.class.equals(propertyType) && value instanceof Map && ((Map) value).size() == 1) {
302             return ((Map) value).values().iterator().next();
303         }
304 
305         return value;
306     }
307 
308     /**
309      * Called once the type should have been resolved. The resolvedType might be
310      * null if no type has been resolved. Every subclass should override this method.
311      */
312     protected TypeDescriptor onResolveType(TypeMapping typeMapping, TransformationState state, TypeDescriptor resolvedType, ComponentProvider componentProvider) {
313         return resolvedType;
314     }
315 
316     @Override
317     public void setProperty(TypeMapping mapping, TransformationState state, PropertyTypeDescriptor descriptor, Map<String, Object> values) throws RepositoryException {
318         String propertyName = descriptor.getName();
319         if (propertyName.equals("class")) {
320             return;
321         }
322         Object value = values.get(propertyName);
323         Object bean = state.getCurrentBean();
324 
325         if (propertyName.equals("content") && value == null) {
326             // TODO this should be changed to node but this would require to
327             // rewrite some classes to use node instead of content
328             value = new SystemContentWrapper(ContentUtil.asContent(state.getCurrentNode()));
329         } else if (propertyName.equals("name") && value == null) {
330             value = state.getCurrentNode().getName();
331         } else if (propertyName.equals("className") && value == null) {
332             value = values.get("class");
333         }
334 
335         // do no try to set a bean-property that has no corresponding node-property
336         if (value == null) {
337             return;
338         }
339 
340         log.debug("try to set {}.{} with value {}", bean, propertyName, value);
341         // if the parent bean is a map, we can't guess the types.
342         if (!(bean instanceof Map)) {
343             try {
344                 PropertyTypeDescriptor dscr = mapping.getPropertyTypeDescriptor(bean.getClass(), propertyName);
345                 if (dscr.getType() != null) {
346                     // try to use a setter method for a Collection property of
347                     // the bean
348                     if (dscr.isCollection() || dscr.isMap() || dscr.isArray()) {
349                         log.debug("{} is of type collection, map or array", propertyName);
350                         if (dscr.getWriteMethod() != null) {
351                             Method method = dscr.getWriteMethod();
352                             clearCollection(bean, propertyName);
353                             filterOrConvertCollectionsAndMaps(dscr, value);
354                             if (dscr.isMap()) {
355                                 method.invoke(bean, value);
356                             } else if (dscr.isArray()) {
357                                 Class<?> entryClass = dscr.getCollectionEntryType().getType();
358                                 Collection<Object> list = new LinkedList<Object>(((Map<Object, Object>) value).values());
359 
360                                 Object[] arr = (Object[]) Array.newInstance(entryClass, list.size());
361                                 for (int i = 0; i < arr.length; i++) {
362                                     arr[i] = Iterables.get(list, i);
363                                 }
364                                 method.invoke(bean, new Object[]{arr});
365                             } else if (dscr.isCollection()) {
366                                 if (value instanceof Map) {
367                                     value = createCollectionFromMap((Map<Object, Object>) value, dscr.getType().getType());
368                                 }
369                                 method.invoke(bean, value);
370                             }
371                             return;
372                         } else if (dscr.getAddMethod() != null) {
373                             Method method = dscr.getAddMethod();
374                             clearCollection(bean, propertyName);
375                             Class<?> entryClass = dscr.getCollectionEntryType().getType();
376 
377                             log.warn("Will use deprecated add method [{}] to populate [{}] in bean class [{}].", method.getName(), propertyName, bean.getClass().getName());
378                             for (Iterator<Object> iter = ((Map<Object, Object>) value).keySet().iterator(); iter.hasNext(); ) {
379                                 Object key = iter.next();
380                                 Object entryValue = ((Map<Object, Object>) value).get(key);
381                                 entryValue = convertPropertyValue(entryClass, entryValue);
382                                 if (entryClass.isAssignableFrom(entryValue.getClass())) {
383                                     if (dscr.isCollection() || dscr.isArray()) {
384                                         log.debug("will add value {}", entryValue);
385                                         method.invoke(bean, entryValue);
386                                     }
387                                     // is a map
388                                     else {
389                                         log.debug("will add key {} with value {}", key, entryValue);
390                                         method.invoke(bean, key, entryValue);
391                                     }
392                                 }
393                             }
394                             return;
395                         }
396                         if (dscr.isCollection()) {
397                             log.debug("transform the values to a collection", propertyName);
398                             value = ((Map<Object, Object>) value).values();
399                         }
400                     } else {
401                         value = convertPropertyValue(dscr.getType().getType(), value);
402                     }
403                 }
404             } catch (Exception e) {
405                 handleSetPropertyException(state, propertyName, value, bean, e);
406             }
407         }
408 
409         try {
410             // This uses the converters registered in beanUtilsBean.convertUtilsBean (see constructor of this class)
411             // If a converter is registered, beanutils will determineBestMatch value.toString(), not the value object as-is.
412             // If no converter is registered, then the value Object is set as-is.
413             // If convertPropertyValue() already converted this value, you'll probably want to unregister the beanutils
414             // converter.
415             // some conversions like string to class. Performance of PropertyUtils.setProperty() would be better
416             beanUtilsBean.setProperty(bean, propertyName, value);
417         } catch (Exception e) {
418             handleSetPropertyException(state, propertyName, value, bean, e);
419         }
420     }
421 
422     protected boolean isBeanEnabled(Object bean) {
423         Method method = null;
424         Object isEnabled = null;
425         try {
426             method = bean.getClass().getMethod("isEnabled");
427             // Check if return type is *not primitive* Boolean and if so return always true
428             // See ConfiguredAreaDefinition
429             if (method.getReturnType().equals(Boolean.class)) {
430                 return true;
431             }
432             isEnabled = method.invoke(bean);
433         } catch (NoSuchMethodException e) {
434             // this is ok, enabled property is optional
435             return true;
436         } catch (IllegalArgumentException e) {
437             // this should never happen
438             return true;
439         } catch (IllegalAccessException e) {
440             log.warn("Can't access method [{}#isEnabled]. Maybe it's private/protected?", bean.getClass());
441             return true;
442         } catch (InvocationTargetException e) {
443             log.error("An exception was thrown by [{}]#isEnabled method.", bean.getClass(), e);
444             return true;
445         }
446         return (Boolean) isEnabled;
447     }
448 
449     /**
450      * Filters out not matching values and converts values if required.
451      *
452      * @param dscr descriptor keeping information about the properties
453      * @param value object from which some element might be filtered or converted
454      */
455     private void filterOrConvertCollectionsAndMaps(final PropertyTypeDescriptor dscr, final Object value) {
456         if (dscr.getCollectionEntryType() != null) {
457             Class<?> entryClass = dscr.getCollectionEntryType().getType();
458             if (dscr.getType().isCollection() && value instanceof Collection) {
459                 final Collection<?> collection = ((Collection) value);
460                 final Collection convertedCollection = filterOrConvert(collection, entryClass);
461                 collection.clear();
462                 collection.addAll(convertedCollection);
463             } else if (value instanceof Map) {
464                 final Map map = (Map) value;
465                 final Map convertedMap = filterOrConvert(map, entryClass);
466                 map.clear();
467                 map.putAll(convertedMap);
468             }
469         }
470     }
471 
472     private Collection filterOrConvert(final Collection collection, final Class entryClass) {
473         final Collection converted = new ArrayList();
474         final Iterator<?> it = collection.iterator();
475         while (it.hasNext()) {
476             Object obj = it.next();
477             if (isBeanEnabled(obj)) {
478                 if (!entryClass.isAssignableFrom(obj.getClass())) {
479                     final Converter converter = beanUtilsBean.getConvertUtils().lookup(entryClass);
480                     if (converter != null) {
481                         converted.add(converter.convert(entryClass, obj));
482                     }
483                 } else {
484                     converted.add(obj);
485                 }
486             }
487         }
488         return converted;
489     }
490 
491     private Map filterOrConvert(final Map map, final Class entryClass) {
492         final Map converted = new LinkedHashMap();
493         for (Object key : map.keySet()) {
494             Object obj = map.get(key);
495             if (isBeanEnabled(obj)) {
496                 if (!entryClass.isAssignableFrom(obj.getClass())) {
497                     final Converter converter = beanUtilsBean.getConvertUtils().lookup(entryClass);
498                     if (converter != null) {
499                         converted.put(key, converter.convert(entryClass, obj));
500                     }
501                 } else {
502                     converted.put(key, obj);
503                 }
504             }
505         }
506         return converted;
507     }
508 
509     private void clearCollection(Object bean, String propertyName) {
510         log.debug("clearing the current content of the collection/map");
511         try {
512             Object col = PropertyUtils.getProperty(bean, propertyName);
513             if (col != null) {
514                 MethodUtils.invokeExactMethod(col, "clear", new Object[]{});
515             }
516         } catch (Exception e) {
517             log.debug("no clear method found on collection {}", propertyName);
518         }
519     }
520 
521     /**
522      * Creates collection from map. Collection type depends on passed class parameter. If passed class parameter is
523      * interface, then default implementation will be used for creating collection.<br/>
524      * By default
525      * <ul>
526      * <li>{@link LinkedList} is used for creating List and Queue collections.</li>
527      * <li>{@link HashSet} is used for creating Set collection.</li>
528      * </ul>
529      * If passed class parameter is an implementation of any collection type, then this method will create
530      * this implementation and returns it.
531      *
532      * @param map a map which values will be converted to a collection
533      * @param clazz collection type
534      * @return Collection of elements or null.
535      */
536     protected Collection<?> createCollectionFromMap(Map<?, ?> map, Class<?> clazz) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
537         Collection<?> collection = null;
538         Constructor<?> constructor = null;
539         if (clazz.isInterface()) {
540             // class is an interface, we need to decide which implementation of interface we will use
541             if (List.class.isAssignableFrom(clazz)) {
542                 constructor = defaultListImpl.getConstructor(Collection.class);
543             } else if (clazz.isAssignableFrom(Queue.class)) {
544                 constructor = defaultQueueImpl.getConstructor(Collection.class);
545             } else if (Set.class.isAssignableFrom(clazz)) {
546                 constructor = defaultSetImpl.getConstructor(Collection.class);
547             }
548         } else {
549             if (Collection.class.isAssignableFrom(clazz)) {
550                 constructor = clazz.getConstructor(Collection.class);
551             }
552         }
553         if (constructor != null) {
554             collection = (Collection<?>) constructor.newInstance(map.values());
555         }
556         return collection;
557     }
558 
559     private void handleSetPropertyException(TransformationState state, String propertyName, Object value, Object bean, Throwable e) throws RepositoryException /* See MAGNOLIA-5890, Node2BeanException */ {
560         if (e instanceof InvocationTargetException) {
561             e = e.getCause();
562         }
563         log.debug("Can't set property [{}] to value [{}] in bean [{}] for node {} due to {}", propertyName, value, bean.getClass().getName(), state.getCurrentNode().getPath(), e.toString(), e);
564         log.error("Can't set property [{}] to value [{}] in bean [{}] for node {} due to {}", propertyName, value, bean.getClass().getName(), state.getCurrentNode().getPath(), e.toString());
565     }
566 
567 }