View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 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 info.magnolia.config.registry.DefinitionProvider;
37  import info.magnolia.module.blossom.annotation.DialogFactory;
38  import info.magnolia.module.blossom.annotation.PreRegister;
39  import info.magnolia.module.blossom.support.AbstractBeanDetector;
40  import info.magnolia.module.blossom.support.MethodInvocationUtils;
41  import info.magnolia.module.blossom.support.ParameterResolver;
42  import info.magnolia.objectfactory.Components;
43  import info.magnolia.ui.dialog.DialogDefinition;
44  import info.magnolia.ui.dialog.DialogDefinitionRegistry;
45  
46  import java.lang.reflect.Method;
47  import java.lang.reflect.Modifier;
48  import java.util.ArrayList;
49  import java.util.Collections;
50  import java.util.List;
51  import java.util.concurrent.atomic.AtomicReference;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  import org.springframework.aop.support.AopUtils;
56  import org.springframework.beans.factory.InitializingBean;
57  import org.springframework.util.ClassUtils;
58  import org.springframework.util.ReflectionUtils;
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          DefinitionProvider<DialogDefinition> dialogProvider = createDialogDefinitionProvider(dialogDescription);
94          DialogDefinitionRegistry registry = Components.getComponent(DialogDefinitionRegistry.class);
95  
96          registry.register(dialogProvider);
97          registry.addDecorator(new DialogCreatorDefinitionDecorator(dialogDescription));
98  
99          if (logger.isInfoEnabled()) {
100             logger.info("Registered dialog [" + dialogDescription.getId() + "]");
101         }
102     }
103 
104     protected DefinitionProvider<DialogDefinition> createDialogDefinitionProvider(BlossomDialogDescription dialogDescription) {
105         return new BlossomDialogDefinitionProvider(dialogDescription);
106     }
107 
108     /**
109      * Allows sub-classes to do post-processing on dialogs for &#64;DialogFactory classes before they're registered. If
110      * it returns null the dialog will not be registered.
111      */
112     protected BlossomDialogDescriptionlossomDialogDescription">BlossomDialogDescription postProcessDialogFactoryDialog(final BlossomDialogDescription dialogDescription) {
113 
114         Object factoryObject = dialogDescription.getFactoryMetaData().getFactoryObject();
115 
116         final Class<?> factoryClass = AopUtils.getTargetClass(factoryObject);
117 
118         final List<Method> matchingMethods = new ArrayList<Method>();
119 
120         ReflectionUtils.doWithMethods(factoryClass, new ReflectionUtils.MethodCallback() {
121 
122             @Override
123             public void doWith(Method method) {
124                 if (method.isAnnotationPresent(PreRegister.class) && method.equals(ClassUtils.getMostSpecificMethod(method, factoryClass))) {
125                     if (Modifier.isStatic(method.getModifiers())) {
126                         throw new IllegalStateException("PreRegister annotation is not supported on static methods");
127                     }
128                     if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(BlossomDialogDescription.class)) {
129                         matchingMethods.add(method);
130                     }
131                 }
132             }
133         });
134         Collections.reverse(matchingMethods);
135 
136         final AtomicReference<BlossomDialogDescription> reference = new AtomicReference<BlossomDialogDescription>(dialogDescription);
137         for (Method matchingMethod : matchingMethods) {
138             Object returnValue = MethodInvocationUtils.invoke(matchingMethod, factoryObject, new ParameterResolver() {
139                 @Override
140                 public Object resolveParameter(Class<?> parameterType) {
141                     if (parameterType.isAssignableFrom(BlossomDialogDescription.class)) {
142                         return reference.get();
143                     }
144                     return super.resolveParameter(parameterType);
145                 }
146             });
147             if (returnValue == null && !matchingMethod.getReturnType().equals(Void.TYPE)) {
148                 return null;
149             }
150             if (returnValue instanceof BlossomDialogDescription) {
151                 reference.set((BlossomDialogDescription) returnValue);
152             }
153         }
154 
155         return reference.get();
156     }
157 
158     @Override
159     public void afterPropertiesSet() throws Exception {
160         if (dialogDescriptionBuilder == null) {
161             dialogDescriptionBuilder = new DialogDescriptionBuilder();
162         }
163     }
164 }