1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.dialog

File DialogExporter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
31% of files have more coverage

Code metrics

20
33
7
1
160
93
20
0.61
4.71
7
2.86
3.2% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
DialogExporter 67 33 3.2% 20 8
0.866666786.7%
 

Contributing tests

This file is covered by 5 tests. .

Source view

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    toggle public void setDialogDescriptionBuilder(DialogDescriptionBuilder dialogDescriptionBuilder) {
74    this.dialogDescriptionBuilder = dialogDescriptionBuilder;
75    }
76   
 
77  111 toggle @Override
78    protected boolean include(Class<?> beanType, String beanName) {
79  111 beanType = ClassUtils.isCglibProxyClass(beanType) ? beanType.getSuperclass() : beanType;
80  111 return beanType.isAnnotationPresent(DialogFactory.class);
81    }
82   
 
83  7 toggle @Override
84    protected void onBeanDetection(Object bean, String beanName) {
85   
86  7 BlossomDialogDescription dialogDescription = dialogDescriptionBuilder.buildDescription(bean);
87   
88  7 dialogDescription = postProcessDialogFactoryDialog(dialogDescription);
89  7 if (dialogDescription == null) {
90  1 return;
91    }
92   
93  6 Components.getComponent(DialogDefinitionRegistry.class).register(createDialogDefinitionProvider(dialogDescription));
94   
95  6 if (logger.isDebugEnabled()) {
96  0 logger.debug("Registered dialog [" + dialogDescription.getId() + "]");
97    }
98    }
99   
 
100  2 toggle protected DefinitionProvider<DialogDefinition> createDialogDefinitionProvider(BlossomDialogDescription dialogDescription) {
101  2 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  7 toggle protected BlossomDialogDescription postProcessDialogFactoryDialog(final BlossomDialogDescription dialogDescription) {
109   
110  7 Object factoryObject = dialogDescription.getFactoryMetaData().getFactoryObject();
111   
112  7 final Class<?> factoryClass = AopUtils.getTargetClass(factoryObject);
113   
114  7 final List<Method> matchingMethods = new ArrayList<Method>();
115   
116  7 ReflectionUtils.doWithMethods(factoryClass, new ReflectionUtils.MethodCallback() {
117   
 
118  90 toggle @Override
119    public void doWith(Method method) {
120  90 if (method.isAnnotationPresent(PreRegister.class) && method.equals(ClassUtils.getMostSpecificMethod(method, factoryClass))) {
121  4 if (Modifier.isStatic(method.getModifiers())) {
122  0 throw new IllegalStateException("PreRegister annotation is not supported on static methods");
123    }
124  4 if (method.getParameterTypes().length == 1 && method.getParameterTypes()[0].isAssignableFrom(BlossomDialogDescription.class)) {
125  4 matchingMethods.add(method);
126    }
127    }
128    }
129    });
130  7 Collections.reverse(matchingMethods);
131   
132  7 final AtomicReference<BlossomDialogDescription> reference = new AtomicReference<BlossomDialogDescription>(dialogDescription);
133  7 for (Method matchingMethod : matchingMethods) {
134  4 Object returnValue = MethodInvocationUtils.invoke(matchingMethod, factoryObject, new ParameterResolver() {
 
135  4 toggle @Override
136    public Object resolveParameter(Class<?> parameterType) {
137  4 if (parameterType.isAssignableFrom(BlossomDialogDescription.class)) {
138  4 return reference.get();
139    }
140  0 return super.resolveParameter(parameterType);
141    }
142    });
143  4 if (returnValue == null && !matchingMethod.getReturnType().equals(Void.TYPE)) {
144  1 return null;
145    }
146  3 if (returnValue instanceof BlossomDialogDescription) {
147  2 reference.set((BlossomDialogDescription) returnValue);
148    }
149    }
150   
151  6 return reference.get();
152    }
153   
 
154  8 toggle @Override
155    public void afterPropertiesSet() throws Exception {
156  8 if (dialogDescriptionBuilder == null) {
157  8 dialogDescriptionBuilder = new DialogDescriptionBuilder();
158    }
159    }
160    }