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.guice;
35  
36  import info.magnolia.module.model.ComponentDefinition;
37  import info.magnolia.objectfactory.ComponentFactory;
38  import info.magnolia.objectfactory.annotation.LazySingleton;
39  import info.magnolia.objectfactory.annotation.LocalScoped;
40  import info.magnolia.objectfactory.annotation.SessionScoped;
41  import info.magnolia.objectfactory.configuration.ComponentConfiguration;
42  import info.magnolia.objectfactory.configuration.ComponentProviderConfiguration;
43  import info.magnolia.objectfactory.configuration.ConfiguredComponentConfiguration;
44  import info.magnolia.objectfactory.configuration.ImplementationConfiguration;
45  import info.magnolia.objectfactory.configuration.InstanceConfiguration;
46  import info.magnolia.objectfactory.configuration.ProviderConfiguration;
47  
48  import java.lang.annotation.Annotation;
49  import java.util.Map;
50  
51  import javax.inject.Provider;
52  import javax.inject.Singleton;
53  
54  import com.google.inject.util.Providers;
55  import org.apache.commons.lang.StringUtils;
56  
57  import com.google.inject.AbstractModule;
58  import com.google.inject.Module;
59  import com.google.inject.binder.ScopedBindingBuilder;
60  
61  
62  /**
63   * Guice configuration module that adds bindings based on a ComponentProviderConfiguration.
64   */
65  public class GuiceComponentConfigurationModule extends AbstractModule {
66  
67      private final ComponentProviderConfiguration configuration;
68  
69      public GuiceComponentConfigurationModule(ComponentProviderConfiguration configuration) {
70          this.configuration = configuration;
71      }
72  
73      @Override
74      protected void configure() {
75  
76          for (Map.Entry<Class, ComponentConfiguration> entry : configuration.getComponents().entrySet()) {
77              bindConfiguration(entry.getValue());
78          }
79  
80          for (Object configurer : configuration.getConfigurers()) {
81              if (configurer instanceof Module) {
82                  install((Module) configurer);
83              }
84          }
85      }
86  
87      private <T> void bindConfiguration(ComponentConfiguration<T> configuration) {
88          if (configuration instanceof ImplementationConfiguration) {
89              bindImplementation((ImplementationConfiguration<T>) configuration);
90          } else if (configuration instanceof InstanceConfiguration) {
91              bindInstance((InstanceConfiguration<T>) configuration);
92          } else if (configuration instanceof ProviderConfiguration) {
93              bindProvider((ProviderConfiguration<T>) configuration);
94          } else if (configuration instanceof ConfiguredComponentConfiguration) {
95              ConfiguredComponentConfiguration<T> configured = (ConfiguredComponentConfiguration<T>) configuration;
96              if (configured.isObserved()) {
97                  bindObservedComponent(configured);
98              } else {
99                  bindConfiguredComponent(configured);
100             }
101         } else {
102             throw new IllegalStateException("Component configuration is ambiguous for component with type [" + configuration.getType() + "]");
103         }
104     }
105 
106     private <T> void bindConfiguredComponent(ConfiguredComponentConfiguration<T> configuration) {
107         Provider<T> provider = new GuiceConfiguredComponentProvider<T>(configuration.getWorkspace(), configuration.getPath());
108         ScopedBindingBuilder builder = bindProvider(configuration.getType(), provider);
109         bindInScope(builder, configuration);
110     }
111 
112     private <T> void bindObservedComponent(ConfiguredComponentConfiguration<T> configuration) {
113         Class<T> key = configuration.getType();
114         Provider<T> provider = new GuiceObservedComponentProvider<T>(configuration.getWorkspace(), configuration.getPath(), key);
115         ScopedBindingBuilder builder = bindProvider(configuration.getType(), provider);
116         bindInScope(builder, configuration);
117     }
118 
119     private <T> void bindProvider(ProviderConfiguration<T> configuration) {
120         Class<?> factoryClass = configuration.getProviderClass();
121 
122         if (ComponentFactory.class.isAssignableFrom(factoryClass)) {
123             Provider<T> provider = GuiceUtils.providerForComponentFactory((Class<? extends ComponentFactory<T>>) factoryClass);
124             ScopedBindingBuilder builder = bindProvider(configuration.getType(), provider);
125             bindInScope(builder, configuration);
126         } else if (Provider.class.isAssignableFrom(factoryClass)) {
127             ScopedBindingBuilder builder = bindProvider(configuration.getType(), (Class<? extends Provider<T>>) factoryClass);
128             bindInScope(builder, configuration);
129         } else {
130             throw new IllegalStateException("Unsupported provider class [" + factoryClass + "] for component with type [" + configuration.getType() + "]");
131         }
132     }
133 
134     private <T> void bindInstance(InstanceConfiguration<T> configuration) {
135         Class<T> key = configuration.getType();
136         Object instance = configuration.getInstance();
137         if (instance instanceof Provider) {
138             bindProvider(configuration.getType(), (Provider<T>) instance);
139         } else if (instance instanceof ComponentFactory) {
140             bindProvider(configuration.getType(), GuiceUtils.providerForComponentFactory((ComponentFactory<T>) instance));
141         } else {
142             bind(key).toInstance((T) instance);
143         }
144         // we don't apply any scope here since instance are natural singletons
145     }
146 
147     private <T> void bindImplementation(ImplementationConfiguration<T> configuration) {
148         Class<T> key = configuration.getType();
149         Class<? extends T> implementation = configuration.getImplementation();
150 
151         ScopedBindingBuilder builder;
152         if (key.equals(implementation)) {
153             builder = bind(implementation);
154         } else {
155             builder = bind(key).to(implementation);
156         }
157         bindInScope(builder, configuration);
158     }
159 
160     private <T> ScopedBindingBuilder bindProvider(Class<T> type, Provider<T> provider) {
161         return bind(type).toProvider(Providers.guicify(provider));
162     }
163 
164     private <T> ScopedBindingBuilder bindProvider(Class<T> type, Class<? extends Provider<T>> provider) {
165         return bind(type).toProvider(provider);
166     }
167 
168     private <T> void bindInScope(ScopedBindingBuilder builder, ComponentConfiguration<T> configuration) {
169         Class<? extends Annotation> scopeAnnotation = getScope(configuration);
170         if (scopeAnnotation != null) {
171             builder.in(scopeAnnotation);
172         }
173     }
174 
175     private Class<? extends Annotation> getScope(ComponentConfiguration<?> componentConfiguration) {
176         String scope = componentConfiguration.getScope();
177         if (StringUtils.isEmpty(scope)) {
178             return null;
179         }
180         if (ComponentDefinition.SCOPE_SINGLETON.equalsIgnoreCase(scope) && !componentConfiguration.isLazy()) {
181             return Singleton.class;
182         }
183         if (ComponentDefinition.SCOPE_SINGLETON.equalsIgnoreCase(scope) && componentConfiguration.isLazy()) {
184             return LazySingleton.class;
185         }
186         if (ComponentDefinition.SCOPE_LOCAL.equalsIgnoreCase(scope)) {
187             return LocalScoped.class;
188         }
189         if (ComponentDefinition.SCOPE_SESSION.equalsIgnoreCase(scope)) {
190             return SessionScoped.class;
191         }
192         throw new IllegalStateException("Unknown scope [" + scope + "] for component with type [" + componentConfiguration.getType() + "]");
193     }
194 }