View Javadoc

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