View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.module.blossom.dialog;
35  
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  import org.springframework.aop.support.AopUtils;
39  import org.springframework.beans.factory.InitializingBean;
40  import org.springframework.util.ClassUtils;
41  import org.springframework.util.ReflectionUtils;
42  
43  import info.magnolia.module.blossom.annotation.DialogFactory;
44  import info.magnolia.module.blossom.annotation.PreRegister;
45  import info.magnolia.module.blossom.support.AbstractBeanDetector;
46  import info.magnolia.module.blossom.support.MethodInvocationUtils;
47  import info.magnolia.module.blossom.support.ParameterResolver;
48  import info.magnolia.objectfactory.Components;
49  import info.magnolia.ui.dialog.registry.DialogDefinitionProvider;
50  import info.magnolia.ui.dialog.registry.DialogDefinitionRegistry;
51  
52  import java.lang.reflect.Method;
53  import java.lang.reflect.Modifier;
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.List;
57  import java.util.concurrent.atomic.AtomicReference;
58  
59  /**
60   * Detects class level dialog factories by scanning beans in the local application context and registers them with the
61   * dialog registry.
62   *
63   * @see info.magnolia.module.blossom.annotation.DialogFactory
64   * @since 1.1.1
65   */
66  public class DialogExporter extends AbstractBeanDetector implements InitializingBean {
67  
68      private final Logger logger = LoggerFactory.getLogger(getClass());
69  
70      private DialogDescriptionBuilder dialogDescriptionBuilder;
71  
72      public void setDialogDescriptionBuilder(DialogDescriptionBuilder dialogDescriptionBuilder) {
73          this.dialogDescriptionBuilder = dialogDescriptionBuilder;
74      }
75  
76      @Override
77      protected boolean include(Class<?> beanType, String beanName) {
78          beanType = AopUtils.isCglibProxyClass(beanType) ? beanType.getSuperclass() : beanType;
79          return beanType.isAnnotationPresent(DialogFactory.class);
80      }
81  
82      @Override
83      protected void onBeanDetection(Object bean, String beanName) {
84  
85          BlossomDialogDescription dialogDescription = dialogDescriptionBuilder.buildDescription(bean);
86  
87          dialogDescription = postProcessDialogFactoryDialog(dialogDescription);
88          if (dialogDescription == null) {
89              return;
90          }
91  
92          Components.getComponent(DialogDefinitionRegistry.class).register(createDialogDefinitionProvider(dialogDescription));
93  
94          if (logger.isDebugEnabled()) {
95              logger.debug("Registered dialog [" + dialogDescription.getId() + "]");
96          }
97      }
98  
99      protected DialogDefinitionProvider createDialogDefinitionProvider(BlossomDialogDescription dialogDescription) {
100         return new BlossomDialogDefinitionProvider(dialogDescription);
101     }
102 
103     /**
104      * Allows sub-classes to do post-processing on dialogs for &#64;DialogFactory classes before they're registered. If
105      * it returns null the dialog will not be registered.
106      */
107     protected BlossomDialogDescription postProcessDialogFactoryDialog(final BlossomDialogDescription dialogDescription) {
108 
109         Object factoryObject = dialogDescription.getFactoryMetaData().getFactoryObject();
110 
111         final Class<?> factoryClass = AopUtils.getTargetClass(factoryObject);
112 
113         final List<Method> matchingMethods = new ArrayList<Method>();
114 
115         ReflectionUtils.doWithMethods(factoryClass, new ReflectionUtils.MethodCallback() {
116 
117             @Override
118             public void doWith(Method method) {
119                 if (method.isAnnotationPresent(PreRegister.class) && method.equals(ClassUtils.getMostSpecificMethod(method, factoryClass))) {
120                     if (Modifier.isStatic(method.getModifiers())) {
121                         throw new IllegalStateException("PreRegister annotation is not supported on static methods");
122                     }
123                     if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(BlossomDialogDescription.class)) {
124                         matchingMethods.add(method);
125                     }
126                 }
127             }
128         });
129         Collections.reverse(matchingMethods);
130 
131         final AtomicReference<BlossomDialogDescription> reference = new AtomicReference<BlossomDialogDescription>(dialogDescription);
132         for (Method matchingMethod : matchingMethods) {
133             Object returnValue = MethodInvocationUtils.invoke(matchingMethod, factoryObject, new ParameterResolver() {
134                 @Override
135                 public Object resolveParameter(Class<?> parameterType) {
136                     if (parameterType.isAssignableFrom(BlossomDialogDescription.class)) {
137                         return reference.get();
138                     }
139                     return super.resolveParameter(parameterType);
140                 }
141             });
142             if (returnValue == null && !matchingMethod.getReturnType().equals(Void.TYPE)) {
143                 return null;
144             }
145             if (returnValue instanceof BlossomDialogDescription) {
146                 reference.set((BlossomDialogDescription) returnValue);
147             }
148         }
149 
150         return reference.get();
151     }
152 
153     @Override
154     public void afterPropertiesSet() throws Exception {
155         if (dialogDescriptionBuilder == null) {
156             dialogDescriptionBuilder = new DialogDescriptionBuilder();
157         }
158     }
159 }