View Javadoc
1   /**
2    * This file Copyright (c) 2011-2015 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 e) {
103             throw new ComponentConfigurationException("Unable to instantiate configurer [" + clazz + "]", e);
104         } catch (IllegalAccessException e) {
105             throw new ComponentConfigurationException("Unable to instantiate configurer [" + clazz + "]", e);
106         }
107     }
108 
109     protected ComponentConfiguration getComponent(ComponentDefinition definition) {
110         if (isProvider(definition)) {
111             return getProvider(definition);
112         } else if (isImplementation(definition)) {
113             return getImplementation(definition);
114         } else if (isConfigured(definition)) {
115             return getConfigured(definition);
116         } else if (isObserved(definition)) {
117             return getObserved(definition);
118         } else {
119             throw new ComponentConfigurationException("Unable to add component with key " + definition.getType());
120         }
121     }
122 
123     protected ImplementationConfiguration getImplementation(ComponentDefinition definition) {
124         ImplementationConfiguration configuration = new ImplementationConfiguration();
125         configuration.setType(classForName(definition.getType()));
126         configuration.setImplementation(classForName(definition.getImplementation()));
127         configuration.setScope(definition.getScope());
128         configuration.setLazy(parseLazyFlag(definition));
129         return configuration;
130     }
131 
132     protected ComponentConfiguration getProvider(ComponentDefinition definition) {
133         ProviderConfiguration configuration = new ProviderConfiguration();
134         configuration.setType(classForName(definition.getType()));
135         configuration.setProviderClass(classForName(definition.getProvider()));
136         configuration.setScope(definition.getScope());
137         configuration.setLazy(parseLazyFlag(definition));
138         return configuration;
139     }
140 
141     protected ComponentConfiguration getConfigured(ComponentDefinition definition) {
142         ConfiguredComponentConfiguration configuration = new ConfiguredComponentConfiguration();
143         configuration.setType(classForName(definition.getType()));
144         configuration.setWorkspace(StringUtils.defaultIfEmpty(definition.getWorkspace(), RepositoryConstants.CONFIG));
145         configuration.setPath(definition.getPath());
146         configuration.setObserved(false);
147         configuration.setScope(definition.getScope());
148         configuration.setLazy(parseLazyFlag(definition));
149         return configuration;
150     }
151 
152     protected ComponentConfiguration getObserved(ComponentDefinition definition) {
153         ConfiguredComponentConfiguration configuration = new ConfiguredComponentConfiguration();
154         configuration.setType(classForName(definition.getType()));
155         configuration.setWorkspace(StringUtils.defaultIfEmpty(definition.getWorkspace(), RepositoryConstants.CONFIG));
156         configuration.setPath(definition.getPath());
157         configuration.setObserved(true);
158         configuration.setScope(definition.getScope());
159         configuration.setLazy(parseLazyFlag(definition));
160         return configuration;
161     }
162 
163     protected boolean isImplementation(ComponentDefinition definition) {
164         return StringUtils.isNotBlank(definition.getImplementation());
165     }
166 
167     protected boolean isProvider(ComponentDefinition definition) {
168         return StringUtils.isNotBlank(definition.getProvider());
169     }
170 
171     protected boolean isConfigured(ComponentDefinition definition) {
172         return StringUtils.isNotBlank(definition.getPath()) && !Boolean.parseBoolean(definition.getObserved());
173     }
174 
175     protected boolean isObserved(ComponentDefinition definition) {
176         return StringUtils.isNotBlank(definition.getPath()) && Boolean.parseBoolean(definition.getObserved());
177     }
178 
179     protected boolean parseLazyFlag(ComponentDefinition definition) {
180         String lazy = definition.getLazy();
181         return StringUtils.isEmpty(lazy) || Boolean.parseBoolean(lazy);
182     }
183 
184     /**
185      * Returns the class denoted by the supplied class name without initializing the class.
186      */
187     protected Class<?> classForName(String className) {
188         try {
189             return Class.forName(className, false, this.getClass().getClassLoader());
190         } catch (ClassNotFoundException e) {
191             throw new ComponentConfigurationException(e);
192         }
193     }
194 }