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.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  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import com.google.inject.AbstractModule;
49  import com.google.inject.CreationException;
50  import com.google.inject.Guice;
51  import com.google.inject.Injector;
52  import com.google.inject.Module;
53  import com.google.inject.Stage;
54  import com.google.inject.util.Modules;
55  import com.mycila.guice.ext.closeable.CloseableModule;
56  import com.mycila.guice.ext.jsr250.Jsr250Module;
57  
58  /**
59   * Builder for creating a GuiceComponentProvider.
60   */
61  public class GuiceComponentProviderBuilder {
62  
63      private static final Logger log = LoggerFactory.getLogger(GuiceComponentProviderBuilder.class);
64      private ComponentProviderConfiguration configuration;
65      private boolean exposeGlobally;
66      private GuiceComponentProvider parent;
67      private final List<Module> extraModules = new ArrayList<Module>();
68      private Stage stage;
69  
70      public GuiceComponentProviderBuilder exposeGlobally() {
71          this.exposeGlobally = true;
72          return this;
73      }
74  
75      public GuiceComponentProviderBuilder withConfiguration(ComponentProviderConfiguration configuration) {
76          this.configuration = configuration;
77          return this;
78      }
79  
80      public GuiceComponentProviderBuilder withParent(GuiceComponentProvider parent) {
81          this.parent = parent;
82          return this;
83      }
84  
85      public GuiceComponentProviderBuilder inStage(Stage stage) {
86          this.stage = stage;
87          return this;
88      }
89  
90      public void addModule(Module module) {
91          this.extraModules.add(module);
92      }
93  
94      public GuiceComponentProvider build(ComponentConfigurer... configurers) {
95  
96          if (configuration == null) {
97              configuration = new ComponentProviderConfiguration();
98          }
99  
100         // Add implicit configurers
101         for (ComponentConfigurer configurer : configurers) {
102             configuration.addConfigurer(configurer);
103         }
104 
105         // Allow configurers to customize the configuration before we use it
106         for (ComponentConfigurer configurer : configuration.getConfigurers()) {
107             configurer.doWithConfiguration(parent, configuration);
108         }
109 
110         // Create the ComponentProvider instance and expose it globally if required
111         final GuiceComponentProvider componentProvider = new GuiceComponentProvider(configuration.getTypeMapping(), parent);
112         if (exposeGlobally) {
113             Components.setComponentProvider(componentProvider);
114         }
115 
116         Module module = new AbstractModule() {
117             @Override
118             protected void configure() {
119 
120                 // Bind the ComponentProvider instance first so its the first thing to get member injection
121                 bind(ComponentProvider.class).toInstance(componentProvider);
122 
123                 // Components aren't available yet
124                 if (Boolean.valueOf(SystemProperty.getProperty("magnolia.jsr250.enabled", "true"))) {
125                     // JSR-250 support added by Mycila
126                     install(new Jsr250Module());
127                 }
128 
129                 install(new GuiceComponentConfigurationModule(configuration));
130 
131                 for (Module extraModule : extraModules) {
132                     binder().install(extraModule);
133                 }
134 
135                 install(new CloseableModule());
136             }
137         };
138 
139         if (parent != null) {
140             GuiceParentBindingsModule parentBindingsModule = new GuiceParentBindingsModule(parent.getInjector());
141             module = Modules.override(parentBindingsModule).with(module);
142         }
143 
144         try {
145             Injector injector = Guice.createInjector(resolveStageToUse(), module);
146             return (GuiceComponentProvider) injector.getInstance(ComponentProvider.class);
147         } catch (CreationException e) {
148             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.getTypeMapping(), configuration.getComponents(), e);
149             throw e;
150         }
151 
152     }
153 
154     public GuiceComponentProvider build() {
155         return build(new GuiceContextAndScopesConfigurer(), new GuicePropertyConfigurer());
156     }
157 
158     private Stage resolveStageToUse() {
159         if (stage != null) {
160             return stage;
161         }
162         if (parent != null) {
163             return parent.getInjector().getInstance(Stage.class);
164         }
165         return Stage.PRODUCTION;
166     }
167 }