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