View Javadoc
1   /**
2    * This file Copyright (c) 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.config.module;
35  
36  import static info.magnolia.transformer.TransformationProblem.warning;
37  
38  import info.magnolia.context.MgnlContext;
39  import info.magnolia.event.EventBus;
40  import info.magnolia.event.SystemEventBus;
41  import info.magnolia.jcr.node2bean.PropertyTypeDescriptor;
42  import info.magnolia.jcr.node2bean.TypeDescriptor;
43  import info.magnolia.jcr.node2bean.TypeMapping;
44  import info.magnolia.jcr.node2bean.impl.PreConfiguredBeanUtils;
45  import info.magnolia.map2bean.Map2BeanTransformer;
46  import info.magnolia.map2bean.TransformationState;
47  import info.magnolia.module.ModuleLifecycle;
48  import info.magnolia.module.ModuleRegistry;
49  import info.magnolia.module.StartModuleEvent;
50  import info.magnolia.module.StopModuleEvent;
51  import info.magnolia.module.model.ModuleDefinition;
52  import info.magnolia.objectfactory.ComponentProvider;
53  import info.magnolia.transformer.BeanTypeResolver;
54  import info.magnolia.transformer.TransformationResult;
55  
56  import java.lang.reflect.InvocationTargetException;
57  import java.lang.reflect.Method;
58  import java.util.ArrayList;
59  import java.util.Collection;
60  import java.util.Map;
61  import java.util.Optional;
62  import java.util.stream.Collectors;
63  import java.util.stream.Stream;
64  
65  import javax.inject.Inject;
66  import javax.inject.Named;
67  
68  import org.apache.commons.beanutils.PropertyUtils;
69  import org.slf4j.Logger;
70  import org.slf4j.LoggerFactory;
71  
72  /**
73   * {@link info.magnolia.map2bean.Map2BeanTransformer} which doesn't create new instances for module classes but returns singleton instances instead.
74   * Also sends {@link info.magnolia.module.StartModuleEvent} and {@link info.magnolia.module.StopModuleEvent} when transforming module class.
75   */
76  class ModuleMap2BeanTransformer extends Map2BeanTransformer {
77  
78      private static final Logger log = LoggerFactory.getLogger(ModuleMap2BeanTransformer.class);
79  
80      private final ModuleRegistry moduleRegistry;
81      private final EventBus eventBus;
82      private final Map<String, String> moduleClassToModuleName;
83  
84      @Inject
85      ModuleMap2BeanTransformer(ComponentProvider componentProvider, TypeMapping mapping, PreConfiguredBeanUtils beanUtils, BeanTypeResolver beanTypeResolver,
86                                ModuleRegistry moduleRegistry, @Named(SystemEventBus.NAME) EventBus eventBus) {
87          super(componentProvider, mapping, beanUtils, beanTypeResolver);
88          this.moduleRegistry = moduleRegistry;
89          this.eventBus = eventBus;
90          this.moduleClassToModuleName = moduleRegistry.getModuleDefinitions().stream()
91                  .filter(moduleDefinition -> moduleDefinition.getClassName() != null)
92                  .collect(Collectors.toMap(ModuleDefinition::getClassName, ModuleDefinition::getName));
93      }
94  
95      @Override
96      public <T> TransformationResult<T> transform(Map<String, Object> map, Class<T> targetType) {
97          return MgnlContext.doInSystemContext(() -> {
98              if (ModuleLifecycle.class.isAssignableFrom(targetType)) {
99                  eventBus.fireEvent(new StopModuleEvent(moduleClassToModuleName.get(targetType.getName()))); //just for restart, first start is triggered by module manager according to dependency order which is not respected be configuration sources
100                 TransformationResult<T> result = super.transform(map, targetType);
101                 eventBus.fireEvent(new StartModuleEvent(moduleClassToModuleName.get(targetType.getName())));
102                 return result;
103             } else {
104                 return super.transform(map, targetType);
105             }
106         });
107     }
108 
109     @Override
110     protected Object createInstance(Class<?> type) {
111         Object instance;
112         if (moduleClassToModuleName.containsKey(type.getName())) {
113             instance = moduleRegistry.getModuleInstance(type);
114         } else {
115             instance = super.createInstance(type);
116         }
117         return instance;
118     }
119 
120     @Override
121     protected void handleMissingProperty(TransformationState state, Object bean, String sourcePropertyName, Object sourcePropertyValue, String missingPropertyMessage) {
122         Optional<Method> addMethodOptional = Stream.of(bean.getClass().getMethods())
123                 .filter(method -> method.getParameterCount() == 1 && method.getName().startsWith("add"))
124                 .findFirst();
125 
126         // if property is missing, look for any adder method in the bean
127         // this is M2B workaround for N2B's info.magnolia.jcr.node2bean.impl.CollectionPropertyHidingTransformer, mainly:
128         // info.magnolia.voting.voters.VoterSetTransformer
129         // info.magnolia.module.cache.executor.CompositeExecutorTransformer
130         if (addMethodOptional.isPresent()) {
131             Method addMethod = addMethodOptional.get();
132             PropertyTypeDescriptor propertyTypeDescriptor = new PropertyTypeDescriptor();
133             TypeDescriptor type = new TypeDescriptor();
134             Class<?> parameterType = addMethod.getParameterTypes()[0];
135             type.setType(parameterType);
136             propertyTypeDescriptor.setType(type);
137             state.pushEntry(sourcePropertyName, sourcePropertyValue, propertyTypeDescriptor);
138             try {
139                 Object value = readValue(state);
140                 if (parameterType.isInstance(value)) {
141                     addMethod.invoke(bean, value);
142                     return;
143                 }
144             } catch (Exception e) {
145                 state.trackProblem(warning(missingPropertyMessage));
146             } finally {
147                 state.popCurrentEntry();
148             }
149         }
150         super.handleMissingProperty(state, bean, sourcePropertyName, sourcePropertyValue, missingPropertyMessage);
151     }
152 
153     @Override
154     protected Collection<Object> readCollection(TransformationState state) { //replacement for info.magnolia.jcr.node2bean.impl.Node2BeanTransformerImpl.filterOrConvert(java.util.Collection, java.lang.Class)
155         Collection<Object> collection = super.readCollection(state);
156         new ArrayList<>(collection).stream()
157                 .filter(o -> !isBeanEnabled(o))
158                 .forEach(collection::remove);
159         return collection;
160     }
161 
162     @Override
163     protected Map<String, Object> readMap(TransformationState state) {  //replacement for info.magnolia.jcr.node2bean.impl.Node2BeanTransformerImpl.filterOrConvert(java.util.Map, java.lang.Class)
164         Map<String, Object> map = super.readMap(state);
165         new ArrayList<>(map.keySet()).stream()
166                 .filter(o -> !isBeanEnabled(map.get(o)))
167                 .forEach(map::remove);
168         return map;               
169     }
170 
171     /**
172      * Replacement for {@link info.magnolia.jcr.node2bean.impl.Node2BeanTransformerImpl#isBeanEnabled(java.lang.Object)}.
173      */
174     private Boolean isBeanEnabled(Object bean) {
175         try {
176             Boolean enabled = (Boolean) PropertyUtils.getSimpleProperty(bean, "enabled");
177             // bean used in Magnolia configuration can have three states, expressed by the Boolean property 'enabled':
178             // true (enabled), false (disabled) and null (undefined or inherited). In the latter case, do not skip bean
179             // so that it can be merged e.g. with a parent definition (e.g. a prototype). See for instance ConfiguredAreaDefinition
180             return enabled == null? true : enabled;
181         } catch (NoSuchMethodException | IllegalArgumentException e) {
182             // this is ok, enabled property is optional
183             return true;
184         } catch (IllegalAccessException e) {
185             log.warn("Can't access method [{}#isEnabled]. Maybe it's private/protected?", bean.getClass());
186             return true;
187         } catch (InvocationTargetException e) {
188             log.error("An exception was thrown by [{}#isEnabled] method.", bean.getClass(), e);
189             return true;
190         }
191     }
192 }