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.objectfactory.CandidateParameterResolver;
37  import info.magnolia.objectfactory.ComponentFactory;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.objectfactory.NoSuchComponentException;
40  import info.magnolia.objectfactory.ObjectManufacturer;
41  import info.magnolia.objectfactory.ParameterResolver;
42  
43  import java.util.Arrays;
44  import java.util.Map;
45  
46  import javax.inject.Inject;
47  import javax.inject.Provider;
48  
49  import org.slf4j.Logger;
50  import org.slf4j.LoggerFactory;
51  
52  import com.google.inject.Injector;
53  
54  
55  /**
56   * ComponentProvider implementation based on Guice.
57   *
58   * @see ComponentProvider
59   * @see GuiceComponentProviderBuilder
60   */
61  public class GuiceComponentProvider implements ComponentProvider {
62  
63      private static Logger logger = LoggerFactory.getLogger(GuiceComponentProvider.class);
64  
65      @Inject
66      private Injector injector;
67      private ObjectManufacturer manufacturer;
68      private final Map<Class<?>, Class<?>> typeMappings;
69      private final GuiceComponentProvider parentComponentProvider;
70  
71      public GuiceComponentProvider(Map<Class<?>, Class<?>> typeMappings, GuiceComponentProvider parentComponentProvider) {
72          this.parentComponentProvider = parentComponentProvider;
73          this.typeMappings = typeMappings;
74      }
75  
76      @Override
77      public <T> Class<? extends T> getImplementation(Class<T> type) {
78          Class<?> implementation = typeMappings.get(type);
79          if (implementation == null) {
80              if (parentComponentProvider != null) {
81                  return parentComponentProvider.getImplementation(type);
82              }
83              return type;
84          }
85          if (ComponentFactory.class.isAssignableFrom(implementation)) {
86              return type;
87          }
88          return (Class<? extends T>) implementation;
89      }
90  
91      @Override
92      @Deprecated
93      public <T> T getSingleton(Class<T> type) {
94          return getComponent(type);
95      }
96  
97      @Override
98      public <T> T getComponent(Class<T> type) throws NoSuchComponentException {
99          if (!GuiceUtils.hasExplicitBindingFor(injector, type)) {
100             throw new NoSuchComponentException("No component configuration for type [" + type.getName() + "] found. Please add a configuration to your module descriptor.");
101         }
102         return injector.getInstance(type);
103     }
104 
105     @Override
106     public <T> T newInstance(Class<T> type, Object... parameters) {
107         return newInstanceWithParameterResolvers(type, new CandidateParameterResolver(parameters));
108     }
109 
110     @Override
111     public <T> T newInstanceWithParameterResolvers(Class<T> type, ParameterResolver... parameterResolvers) {
112         if (this.manufacturer == null) {
113             this.manufacturer = new ObjectManufacturer();
114         }
115         Class<? extends T> implementation = getImplementation(type);
116         if (implementation == null) {
117             logger.warn("Failed to resolve implementation for {}. Perhaps it is not defined. The only types defined for this container are {}.", type, typeMappings);
118         }
119 
120         parameterResolvers = concat(parameterResolvers, new GuiceParameterResolver(injector));
121         T instance = (T) manufacturer.newInstance(implementation, parameterResolvers);
122         injectMembers(instance);
123         return instance;
124     }
125 
126     private ParameterResolver[] concat(ParameterResolver[] array, ParameterResolver extra) {
127         ParameterResolver[] newArray = Arrays.copyOf(array, array.length + 1);
128         newArray[array.length] = extra;
129         return newArray;
130     }
131 
132     public Injector getInjector() {
133         return injector;
134     }
135 
136     public <T> Provider<T> getProvider(Class<T> type) {
137         if (!GuiceUtils.hasExplicitBindingFor(injector, type)) {
138             return null;
139         }
140         return injector.getProvider(type);
141     }
142 
143     public void injectMembers(Object instance) {
144         injector.injectMembers(instance);
145     }
146 
147     public void destroy() {
148         /*
149 
150        Destroy using @PreDestroy is disabled because the implementation acquires instances for lazy-init singletons
151        only to destroy them. It also tries to acquire instances that have non-existing scopes leading to exceptions
152        being thrown. This usually results in shutdown of the application being interrupted before having a chance to
153        properly close down JackRabbit. With (at least) the derby persistence manager this results in threads not being
154        closed down properly and therefore Tomcat stalls at shutdown.
155 
156         */
157     }
158 
159     @Override
160     public GuiceComponentProvider getParent() {
161         return parentComponentProvider;
162     }
163 }