View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.objectfactory.configuration;
35  
36  import info.magnolia.module.model.ComponentDefinition;
37  import info.magnolia.module.model.ComponentsDefinition;
38  import info.magnolia.module.model.ConfigurerDefinition;
39  import info.magnolia.module.model.ModuleDefinition;
40  import info.magnolia.module.model.TypeMappingDefinition;
41  import info.magnolia.repository.RepositoryConstants;
42  
43  import java.util.List;
44  
45  import org.apache.commons.lang3.StringUtils;
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  /**
50   * Builder for creating {@link ComponentProviderConfiguration}s from component definitions.
51   */
52  public class ComponentProviderConfigurationBuilder {
53  
54      private static final Logger log = LoggerFactory.getLogger(ComponentProviderConfigurationBuilder.class);
55  
56      /**
57       * Reads component definitions from the specified resources and returns a {@link ComponentProviderConfiguration}.
58       */
59      public ComponentProviderConfiguration readConfiguration(List<String> resourcePaths, String id) {
60          ComponentConfigurationReadertConfigurationReader.html#ComponentConfigurationReader">ComponentConfigurationReader reader = new ComponentConfigurationReader();
61          List<ComponentsDefinition> componentsDefinitions = reader.readAll(resourcePaths);
62          ComponentProviderConfigurationConfiguration.html#ComponentProviderConfiguration">ComponentProviderConfiguration configuration = new ComponentProviderConfiguration();
63          for (ComponentsDefinition componentsDefinition : componentsDefinitions) {
64              if (componentsDefinition.getId().equals(id)) {
65                  addComponents(configuration, componentsDefinition);
66              }
67          }
68          return configuration;
69      }
70  
71      /**
72       * Reads component definitions from module descriptors and return a {@link ComponentProviderConfiguration}
73       * containing all components with the given id.
74       */
75      public ComponentProviderConfiguration getComponentsFromModules(String id, List<ModuleDefinition> moduleDefinitions) {
76          ComponentProviderConfigurationConfiguration.html#ComponentProviderConfiguration">ComponentProviderConfiguration configuration = new ComponentProviderConfiguration();
77          for (ModuleDefinition moduleDefinition : moduleDefinitions) {
78              log.debug("Getting components defined for module [{}] at version [{}]", moduleDefinition.getName(), moduleDefinition.getVersion().toString());
79              for (ComponentsDefinition componentsDefinition : moduleDefinition.getComponents()) {
80                  if (componentsDefinition.getId().equals(id)) {
81                      addComponents(configuration, componentsDefinition);
82                  }
83              }
84          }
85          return configuration;
86      }
87  
88      public void addComponents(ComponentProviderConfiguration configuration, ComponentsDefinition componentsDefinition) {
89          for (ConfigurerDefinition configurerDefinition : componentsDefinition.getConfigurers()) {
90              configuration.addConfigurer(getConfigurer(configurerDefinition));
91          }
92          for (ComponentDefinition componentDefinition : componentsDefinition.getComponents()) {
93              configuration.addComponent(getComponent(componentDefinition));
94          }
95          for (TypeMappingDefinition typeMappingDefinition : componentsDefinition.getTypeMappings()) {
96              configuration.addTypeMapping(classForName(typeMappingDefinition.getType()), classForName(typeMappingDefinition.getImplementation()));
97          }
98      }
99  
100     protected ComponentConfigurer getConfigurer(ConfigurerDefinition configurerDefinition) {
101         Class clazz = classForName(configurerDefinition.getClassName());
102         if (!ComponentConfigurer.class.isAssignableFrom(clazz)) {
103             throw new ComponentConfigurationException("Configurer must be of type ComponentConfigurer [" + clazz + "]");
104         }
105         try {
106             return (ComponentConfigurer) clazz.newInstance();
107         } catch (InstantiationException | IllegalAccessException e) {
108             throw new ComponentConfigurationException("Unable to instantiate configurer [" + clazz + "]", e);
109         }
110     }
111 
112     public ComponentConfiguration getComponent(ComponentDefinition definition) {
113         final ComponentConfiguration componentConfiguration;
114         if (isProvider(definition)) {
115             componentConfiguration = getProvider(definition);
116         } else if (isImplementation(definition)) {
117             componentConfiguration = getImplementation(definition);
118         } else if (isConfigured(definition)) {
119             componentConfiguration = getConfigured(definition);
120         } else if (isObserved(definition)) {
121             componentConfiguration = getObserved(definition);
122         } else {
123             throw new ComponentConfigurationException("Unable to add component with key " + definition.getType());
124         }
125         componentConfiguration.setBindingContext(definition.getBindingContext());
126         return componentConfiguration;
127     }
128 
129     protected ImplementationConfiguration getImplementation(ComponentDefinition definition) {
130         ImplementationConfigurationConfiguration.html#ImplementationConfiguration">ImplementationConfiguration configuration = new ImplementationConfiguration();
131         configuration.setType(classForName(definition.getType()));
132         configuration.setImplementation(classForName(definition.getImplementation()));
133         configuration.setScope(definition.getScope());
134         configuration.setLazy(parseLazyFlag(definition));
135         return configuration;
136     }
137 
138     protected ComponentConfiguration getProvider(ComponentDefinition definition) {
139         ProviderConfigurationConfiguration.html#ProviderConfiguration">ProviderConfiguration configuration = new ProviderConfiguration();
140         configuration.setType(classForName(definition.getType()));
141         configuration.setProviderClass(classForName(definition.getProvider()));
142         configuration.setScope(definition.getScope());
143         configuration.setLazy(parseLazyFlag(definition));
144         return configuration;
145     }
146 
147     protected ComponentConfiguration getConfigured(ComponentDefinition definition) {
148         ConfiguredComponentConfigurationConfiguration.html#ConfiguredComponentConfiguration">ConfiguredComponentConfiguration configuration = new ConfiguredComponentConfiguration();
149         configuration.setType(classForName(definition.getType()));
150         configuration.setWorkspace(StringUtils.defaultIfEmpty(definition.getWorkspace(), RepositoryConstants.CONFIG));
151         configuration.setPath(definition.getPath());
152         configuration.setObserved(false);
153         configuration.setScope(definition.getScope());
154         configuration.setLazy(parseLazyFlag(definition));
155         return configuration;
156     }
157 
158     protected ComponentConfiguration getObserved(ComponentDefinition definition) {
159         ConfiguredComponentConfigurationConfiguration.html#ConfiguredComponentConfiguration">ConfiguredComponentConfiguration configuration = new ConfiguredComponentConfiguration();
160         configuration.setType(classForName(definition.getType()));
161         configuration.setWorkspace(StringUtils.defaultIfEmpty(definition.getWorkspace(), RepositoryConstants.CONFIG));
162         configuration.setPath(definition.getPath());
163         configuration.setObserved(true);
164         configuration.setScope(definition.getScope());
165         configuration.setLazy(parseLazyFlag(definition));
166         return configuration;
167     }
168 
169     protected boolean isImplementation(ComponentDefinition definition) {
170         return StringUtils.isNotBlank(definition.getImplementation());
171     }
172 
173     protected boolean isProvider(ComponentDefinition definition) {
174         return StringUtils.isNotBlank(definition.getProvider());
175     }
176 
177     protected boolean isConfigured(ComponentDefinition definition) {
178         return StringUtils.isNotBlank(definition.getPath()) && !Boolean.parseBoolean(definition.getObserved());
179     }
180 
181     protected boolean isObserved(ComponentDefinition definition) {
182         return StringUtils.isNotBlank(definition.getPath()) && Boolean.parseBoolean(definition.getObserved());
183     }
184 
185     protected boolean parseLazyFlag(ComponentDefinition definition) {
186         String lazy = definition.getLazy();
187         return StringUtils.isEmpty(lazy) || Boolean.parseBoolean(lazy);
188     }
189 
190     /**
191      * Returns the class denoted by the supplied class name without initializing the class.
192      */
193     protected Class<?> classForName(String className) {
194         try {
195             return Class.forName(className, false, this.getClass().getClassLoader());
196         } catch (ClassNotFoundException e) {
197             throw new ComponentConfigurationException(e);
198         }
199     }
200 }