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