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.guice;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import com.google.inject.AbstractModule;
40  import com.google.inject.Guice;
41  import com.google.inject.Injector;
42  import com.google.inject.Module;
43  import com.google.inject.Stage;
44  import com.google.inject.util.Modules;
45  import com.mycila.inject.jsr250.Jsr250;
46  import info.magnolia.objectfactory.ComponentProvider;
47  import info.magnolia.objectfactory.Components;
48  import info.magnolia.objectfactory.configuration.ComponentConfigurer;
49  import info.magnolia.objectfactory.configuration.ComponentProviderConfiguration;
50  
51  
52  /**
53   * Builder for creating a GuiceComponentProvider.
54   *
55   * @version $Id$
56   */
57  public class GuiceComponentProviderBuilder {
58  
59      private ComponentProviderConfiguration configuration;
60      private boolean exposeGlobally;
61      private GuiceComponentProvider parent;
62      private List<Module> extraModules = new ArrayList<Module>();
63      private Stage stage;
64  
65      public GuiceComponentProviderBuilder exposeGlobally() {
66          this.exposeGlobally = true;
67          return this;
68      }
69  
70      public GuiceComponentProviderBuilder withConfiguration(ComponentProviderConfiguration configuration) {
71          this.configuration = configuration;
72          return this;
73      }
74  
75      public GuiceComponentProviderBuilder withParent(GuiceComponentProvider parent) {
76          this.parent = parent;
77          return this;
78      }
79  
80      public GuiceComponentProviderBuilder inStage(Stage stage) {
81          this.stage = stage;
82          return this;
83      }
84  
85      public void addModule(Module module) {
86          this.extraModules.add(module);
87      }
88  
89      public GuiceComponentProvider build() {
90  
91          if (configuration == null) {
92              configuration = new ComponentProviderConfiguration();
93          }
94  
95          // Add implicit configurers
96          configuration.addConfigurer(new GuiceContextAndScopesConfigurer());
97          configuration.addConfigurer(new GuicePropertyConfigurer());
98  
99          // Allow configurers to customize the configuration before we use it
100         for (ComponentConfigurer configurer : configuration.getConfigurers()) {
101             configurer.doWithConfiguration(parent, configuration);
102         }
103 
104         // Create the ComponentProvider instance and expose it globally if required
105         final GuiceComponentProvider componentProvider = new GuiceComponentProvider(configuration.getTypeMapping(), parent);
106         if (exposeGlobally) {
107             Components.setComponentProvider(componentProvider);
108         }
109 
110         Module module = new AbstractModule() {
111             @Override
112             protected void configure() {
113 
114                 // Bind the ComponentProvider instance first so its the first thing to get member injection
115                 bind(ComponentProvider.class).toInstance(componentProvider);
116 
117                 // requireExplicitBindings is switched off because Guice internally creates jit bindings and those are called for @PreDestroy, which fails if this is turned on
118 //                binder().requireExplicitBindings();
119 
120                 // JSR-250 support added by Mycila
121                 install(Jsr250.newJsr250Module());
122 
123                 install(new GuiceComponentConfigurationModule(configuration));
124 
125                 for (Module extraModule : extraModules) {
126                     binder().install(extraModule);
127                 }
128             }
129         };
130 
131         if (parent != null) {
132             GuiceParentBindingsModule parentBindingsModule = new GuiceParentBindingsModule(parent.getInjector());
133             module = Modules.override(parentBindingsModule).with(module);
134         }
135 
136         Injector injector = Guice.createInjector(resolveStageToUse(), module);
137 
138         return (GuiceComponentProvider) injector.getInstance(ComponentProvider.class);
139     }
140 
141     private Stage resolveStageToUse() {
142         if (stage != null) {
143             return stage;
144         }
145         if (parent != null) {
146             return parent.getInjector().getInstance(Stage.class);
147         }
148         return Stage.PRODUCTION;
149     }
150 }