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