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.guice;
35  
36  import info.magnolia.cms.core.SystemProperty;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.objectfactory.Components;
39  import info.magnolia.objectfactory.configuration.ComponentConfigurer;
40  import info.magnolia.objectfactory.configuration.ComponentProviderConfiguration;
41  
42  import java.util.ArrayList;
43  import java.util.List;
44  import java.util.Optional;
45  
46  import org.slf4j.Logger;
47  import org.slf4j.LoggerFactory;
48  
49  import com.google.inject.AbstractModule;
50  import com.google.inject.Binding;
51  import com.google.inject.CreationException;
52  import com.google.inject.Guice;
53  import com.google.inject.Injector;
54  import com.google.inject.Module;
55  import com.google.inject.Stage;
56  import com.google.inject.spi.DefaultElementVisitor;
57  import com.google.inject.spi.Element;
58  import com.google.inject.spi.Elements;
59  import com.google.inject.util.Modules;
60  import com.mycila.guice.ext.closeable.CloseableModule;
61  import com.mycila.guice.ext.jsr250.Jsr250Module;
62  
63  /**
64   * Builder for creating a GuiceComponentProvider.
65   */
66  public class GuiceComponentProviderBuilder {
67  
68      private static final Logger log = LoggerFactory.getLogger(GuiceComponentProviderBuilder.class);
69      private ComponentProviderConfiguration configuration;
70      private boolean exposeGlobally;
71      private GuiceComponentProvider parent;
72      private final List<Module> extraModules = new ArrayList<>();
73      private Stage stage;
74  
75      public GuiceComponentProviderBuilder exposeGlobally() {
76          this.exposeGlobally = true;
77          return this;
78      }
79  
80      public GuiceComponentProviderBuilder withConfiguration(ComponentProviderConfiguration configuration) {
81          this.configuration = configuration;
82          return this;
83      }
84  
85      public GuiceComponentProviderBuilder withParent(GuiceComponentProvider parent) {
86          this.parent = parent;
87          return this;
88      }
89  
90      public GuiceComponentProviderBuilder inStage(Stage stage) {
91          this.stage = stage;
92          return this;
93      }
94  
95      public void addModule(Module module) {
96          this.extraModules.add(module);
97      }
98  
99      public GuiceComponentProvider build(ComponentConfigurer... configurers) {
100 
101         if (configuration == null) {
102             configuration = new ComponentProviderConfiguration();
103         }
104 
105         // Add implicit configurers
106         for (ComponentConfigurer configurer : configurers) {
107             configuration.addConfigurer(configurer);
108         }
109 
110         // Allow configurers to customize the configuration before we use it
111         for (ComponentConfigurer configurer : configuration.getConfigurers()) {
112             configurer.doWithConfiguration(parent, configuration);
113         }
114 
115         // Create the ComponentProvider instance and expose it globally if required
116         final GuiceComponentProviderr.html#GuiceComponentProvider">GuiceComponentProvider componentProvider = new GuiceComponentProvider(configuration.getAnnotatedTypeMappings(), parent);
117         if (exposeGlobally) {
118             Components.setComponentProvider(componentProvider);
119         }
120 
121         Module module = new AbstractModule() {
122             @Override
123             protected void configure() {
124 
125                 // Components aren't available yet
126                 if (Boolean.valueOf(SystemProperty.getProperty("magnolia.jsr250.enabled", "true"))) {
127                     // JSR-250 support added by Mycila
128                     install(new Jsr250Module());
129                 }
130 
131                 install(new GuiceComponentConfigurationModule(configuration));
132 
133                 for (Module extraModule : extraModules) {
134                     binder().install(extraModule);
135                 }
136 
137                 install(new CloseableModule());
138             }
139         };
140 
141         module = ensureComponentProviderBinding(module, componentProvider);
142 
143         if (parent != null) {
144             GuiceParentBindingsModuletml#GuiceParentBindingsModule">GuiceParentBindingsModule parentBindingsModule = new GuiceParentBindingsModule(parent.getInjector());
145             module = Modules.override(parentBindingsModule).with(module);
146         }
147 
148         try {
149             Injector injector = Guice.createInjector(resolveStageToUse(), module);
150             injector.injectMembers(componentProvider);
151             return componentProvider;
152         } catch (CreationException e) {
153             log.error("Magnolia failed to load module configuration with types {} and components {}. Please ensure you don't have any legacy modules present in your web application.", configuration.getAnnotatedTypeMappings(), configuration.getAnnotatedComponents(), e);
154             throw e;
155         }
156 
157     }
158 
159     /**
160      * Make sure that the {@link ComponentProvider} is injectable. If it is not yet configured -
161      * use this component provider (one under construction).
162      */
163     private Module ensureComponentProviderBinding(Module module, GuiceComponentProvider fallbackComponentProviderInstance) {
164         final DefaultElementVisitor<Boolean> isComponentProviderBinding = new DefaultElementVisitor<Boolean>() {
165             @Override
166             public <T> Boolean visit(Binding<T> binding) {
167                 return ComponentProvider.class.isAssignableFrom(binding.getKey().getTypeLiteral().getRawType());
168             }
169 
170             @Override
171             protected Boolean visitOther(Element element) {
172                 return false;
173             }
174         };
175 
176         List<Element> elements = new ArrayList<>(Elements.getElements(module));
177 
178         final Optional<Element> componentProviderBinding = elements.stream()
179                 .filter(element -> element.acceptVisitor(isComponentProviderBinding))
180                 .findFirst();
181 
182         if (!componentProviderBinding.isPresent()) {
183             module = Modules.override(module).with(new AbstractModule() {
184                 @Override
185                 protected void configure() {
186                     // Bind the ComponentProvider instance first so its the first thing to get member injection
187                     bind(ComponentProvider.class).toInstance(fallbackComponentProviderInstance);
188                 }
189             });
190         }
191 
192         return module;
193     }
194 
195     public GuiceComponentProvider build() {
196         return build(new GuiceContextAndScopesConfigurer(), new GuicePropertyConfigurer());
197     }
198 
199     private Stage resolveStageToUse() {
200         if (stage != null) {
201             return stage;
202         }
203         if (parent != null) {
204             return parent.getInjector().getInstance(Stage.class);
205         }
206         return Stage.PRODUCTION;
207     }
208 }