View Javadoc

1   /**
2    * This file Copyright (c) 2011-2012 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.init.MagnoliaConfigurationProperties;
37  import info.magnolia.module.model.ComponentDefinition;
38  import info.magnolia.objectfactory.Classes;
39  import info.magnolia.objectfactory.ComponentConfigurationPath;
40  import info.magnolia.objectfactory.ComponentFactory;
41  import info.magnolia.objectfactory.ComponentProvider;
42  
43  import java.util.ArrayList;
44  import java.util.Arrays;
45  import java.util.Collection;
46  
47  import org.apache.commons.lang.StringUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  /**
52   * This ComponentConfigurer configures components from properties. Each property key is the interface/base-class, and the
53   * value is either the implementation-to-use class name, an implementation of {@link ComponentFactory} which is used to
54   * instantiate the desired implementation, or the path to a node in the repository (in the form of
55   * <code>repository:/path/to/node</code> or <code>/path/to/node</code>, which defaults to the <code>config</code>
56   * repository). In the latter case, the component is constructed via
57   * {@link info.magnolia.objectfactory.ObservedComponentFactory} and reflects (through observation) the contents of the
58   * given path.
59   * <p/>
60   * This behaviour exists for backwards compatibility reasons, prefer configuring your components in a module
61   * descriptor instead, inside a components tag.
62   * <p/>
63   * In order to remain backwards compatible implementations are added both as type mappings and as components and then
64   * always scoped as lazy singletons.
65   *
66   * @version $Id$
67   */
68  public class LegacyComponentsConfigurer implements ComponentConfigurer {
69  
70      private final static Logger log = LoggerFactory.getLogger(LegacyComponentsConfigurer.class);
71  
72      public static final String EXCLUDED_KEYS_PROPERTY = "magnolia.components.config.properties.excluded";
73  
74      protected static final String[] EXCLUDED_BY_DEFAULT = new String[] { "com.zeroturnaround.bundled.org.apache.commons.logging.Log" };
75  
76      @Override
77      public void doWithConfiguration(ComponentProvider parentComponentProvider, ComponentProviderConfiguration configuration) {
78  
79          MagnoliaConfigurationProperties configurationProperties = parentComponentProvider.getComponent(MagnoliaConfigurationProperties.class);
80          final String excludedKeysValue = configurationProperties.getProperty(LegacyComponentsConfigurer.EXCLUDED_KEYS_PROPERTY);
81          Collection<String> excludedKeys = new ArrayList<String>();
82  
83          excludedKeys.addAll(Arrays.asList(EXCLUDED_BY_DEFAULT));
84          if (excludedKeysValue != null) {
85              excludedKeys.addAll(Arrays.asList(StringUtils.split(excludedKeysValue, " \t,")));
86          }
87  
88          if (configurationProperties != null) {
89              for (String key : configurationProperties.getKeys()) {
90                  if (!excludedKeys.contains(key)) {
91                      addComponent(configuration, key, configurationProperties.getProperty(key));
92                  }
93              }
94          }
95      }
96  
97      protected <T> void addComponent(ComponentProviderConfiguration componentProviderConfiguration, String key, String value) {
98  
99          final Class<T> type = (Class<T>) classForName(key);
100         if (type == null) {
101             log.debug("{} does not seem to resolve to a class. (property value: {})", key, value);
102             return;
103         }
104 
105         if (ComponentConfigurationPath.isComponentConfigurationPath(value)) {
106             componentProviderConfiguration.addComponent(getObserved(type, value));
107         } else {
108             Class<? extends T> valueType = (Class<? extends T>) classForName(value);
109             if (valueType == null) {
110                 log.debug("{} does not seem to resolve a class or a configuration path. (property key: {})", value, key);
111             } else {
112                 if (ComponentFactory.class.isAssignableFrom(valueType)) {
113                     componentProviderConfiguration.addComponent(getComponentFactory(type, (Class<? extends ComponentFactory<T>>) valueType));
114                 } else {
115                     componentProviderConfiguration.addComponent(getImplementation(type, valueType));
116                     componentProviderConfiguration.addTypeMapping(type, valueType);
117                 }
118             }
119         }
120     }
121 
122     protected <T> ImplementationConfiguration getImplementation(Class<T> type, Class<? extends T> implementation) {
123         ImplementationConfiguration configuration = new ImplementationConfiguration<T>();
124         configuration.setType(type);
125         configuration.setImplementation(implementation);
126         configuration.setScope(ComponentDefinition.SCOPE_SINGLETON);
127         configuration.setLazy(true);
128         return configuration;
129     }
130 
131     protected <T> ProviderConfiguration<T> getComponentFactory(Class<T> type, Class<? extends ComponentFactory<T>> factoryClass) {
132         ProviderConfiguration<T> configuration = new ProviderConfiguration<T>();
133         configuration.setType(type);
134         configuration.setProviderClass(factoryClass);
135         configuration.setScope(ComponentDefinition.SCOPE_SINGLETON);
136         configuration.setLazy(true);
137         return configuration;
138     }
139 
140     protected <T> ConfiguredComponentConfiguration<T> getObserved(Class<T> type, String workspaceAndPath) {
141         ComponentConfigurationPath path = new ComponentConfigurationPath(workspaceAndPath);
142         ConfiguredComponentConfiguration<T> configuration = new ConfiguredComponentConfiguration<T>();
143         configuration.setType(type);
144         configuration.setWorkspace(path.getRepository());
145         configuration.setPath(path.getPath());
146         configuration.setObserved(true);
147         configuration.setScope(ComponentDefinition.SCOPE_SINGLETON);
148         configuration.setLazy(true);
149         return configuration;
150     }
151 
152     protected Class<?> classForName(String value) {
153         try {
154             return Classes.getClassFactory().forName(value);
155         } catch (ClassNotFoundException e) {
156             return null;
157         }
158     }
159 }