View Javadoc

1   /**
2    * This file Copyright (c) 2008-2011 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.beanmerger;
35  
36  import java.lang.reflect.InvocationTargetException;
37  import java.lang.reflect.Method;
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Set;
43  
44  import org.apache.commons.beanutils.MethodUtils;
45  import org.apache.commons.proxy.Invoker;
46  import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
47  
48  import net.sf.cglib.proxy.Enhancer;
49  
50  
51  /**
52   * Proxy-based bean merger.
53   *
54   * @version $Id: ProxyBasedBeanMerger.java 51985 2011-11-25 17:02:47Z tmattsson $
55   */
56  public class ProxyBasedBeanMerger extends BeanMergerBase {
57  
58      @Override
59      protected Object mergeBean(List sources) {
60          Set<Class> types = new HashSet<Class>();
61          Class< ? > mostSpecificAssignableClass = sources.get(0).getClass();
62  
63          for (Object source : sources) {
64  
65              // Skip up the class hierarchy to avoid proxying cglib proxy classes
66              Class clazz = source.getClass();
67              while (Enhancer.isEnhanced(clazz)) {
68                  clazz = clazz.getSuperclass();
69              }
70  
71              Collections.addAll(types, clazz.getInterfaces());
72  
73              if (mostSpecificAssignableClass.isAssignableFrom(clazz)) {
74                  mostSpecificAssignableClass = clazz;
75              }
76          }
77          types.add(mostSpecificAssignableClass);
78  
79          return new CglibProxyFactory().createInvokerProxy(newInvoker(sources), types.toArray(new Class< ? >[types.size()]));
80      }
81  
82      protected MergeInvoker newInvoker(List sources) {
83          return new MergeInvoker(this, sources);
84      }
85  
86      /**
87       * Merge Invoker.
88       *
89       * @version $Id: ProxyBasedBeanMerger.java 51985 2011-11-25 17:02:47Z tmattsson $
90       */
91      protected static final class MergeInvoker implements Invoker {
92  
93          private final List sources;
94  
95          private BeanMerger merger;
96  
97          private MergeInvoker(BeanMerger merger, List sources) {
98              this.merger = merger;
99              this.sources = sources;
100         }
101 
102         @Override
103         public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
104             // only merge calls to property getters
105             if (arguments.length > 0) {
106                 return method.invoke(sources.get(0), arguments);
107             }
108 
109             List values = new ArrayList();
110             for (Object obj : sources) {
111                 values.add(evaluate(obj, method, arguments));
112             }
113 
114             return merger.merge(values);
115         }
116 
117         private Object evaluate(Object obj, Method method, Object[] arguments) throws IllegalAccessException, InvocationTargetException {
118 
119             // direct method call
120             if (method.getDeclaringClass().isAssignableFrom(obj.getClass())) {
121                 return method.invoke(obj, arguments);
122             }
123 
124             // ducking
125             try {
126                 return MethodUtils.invokeMethod(obj, method.getName(), arguments);
127             } catch (NoSuchMethodException e) {
128                 return null;
129             }
130         }
131     }
132 
133 }