View Javadoc
1   /**
2    * This file Copyright (c) 2016-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.ui.framework.ioc;
35  
36  import info.magnolia.objectfactory.CandidateParameterResolver;
37  import info.magnolia.objectfactory.ComponentProvider;
38  import info.magnolia.objectfactory.ParameterResolver;
39  import info.magnolia.objectfactory.guice.GuiceComponentProvider;
40  
41  import java.io.Serializable;
42  import java.util.function.Supplier;
43  
44  import com.google.inject.Key;
45  import com.vaadin.server.VaadinServlet;
46  
47  /**
48   * Lightweight {@link ComponentProvider} implementation for UI components which conducts provisioning
49   * by delegating it to the only UI Guice instance via a {@link GuiceComponentProvider} associated with it.
50   * <p>
51   * The only significant function of such {@link ComponentProvider component provider} is that it
52   * is bound to a certain UI context via {@link UiContextReference} which is pushed into {@link CurrentUiContextReference}
53   * before any Guice invocation. By doing this Guice knows (when uncertain) what flavour of components to inject.
54   * </p>
55   *
56   * @see UiAnnotations
57   * @see UiContextApplyingProvider
58   */
59  public class UiContextBoundComponentProvider implements ComponentProvider, Serializable {
60  
61      private final transient GuiceComponentProvider parentComponentProvider;
62  
63      private final CurrentUiContextReference currentUiContextReference;
64  
65      private final UiContextReference uiContextReference;
66  
67      public UiContextBoundComponentProvider(UiContextReference uiContextReference) {
68          this(uiContextReference, (GuiceComponentProvider) VaadinServlet.getCurrent().getServletContext().getAttribute("componentProvider"), CurrentUiContextReference.get());
69      }
70  
71      public UiContextBoundComponentProvider(UiContextReference uiContextReference, GuiceComponentProvider parentComponentProvider, CurrentUiContextReference currentUiContextReference) {
72          this.uiContextReference = uiContextReference;
73          this.parentComponentProvider = parentComponentProvider;
74          this.currentUiContextReference = currentUiContextReference;
75      }
76  
77      public UiContextReference getUiContextReference() {
78          return this.uiContextReference;
79      }
80  
81      @Override
82      public <T> Class<? extends T> getImplementation(Class<T> type) {
83          return getParent().getImplementation(getClosestMappedType(type));
84      }
85  
86      private <T> Key<T> getClosestMappedType(Class<T> type) {
87          return this.currentUiContextReference.getAvailableContextReferences().stream()
88                  .map(key -> Key.get(type, key.getAnnotation()))
89                  // first check if there is a matching annotated type mapping
90                  .filter(key -> getParent().getTypeMappings().containsKey(key))
91                  .findFirst()
92                  // otherwise just fetch the un-annotated type mapping
93                  .orElseGet(() -> Key.get(type));
94      }
95  
96      @Override
97      public <T> T getSingleton(Class<T> type) {
98          return getParent().getSingleton(type);
99      }
100 
101     @Override
102     public <T> T getComponent(Class<T> type) {
103         return provideInCurrentScope(() -> getParent().getComponent(type));
104     }
105 
106     @Override
107     public <T> T newInstance(Class<T> type, Object... parameters) {
108         return newInstanceWithParameterResolvers(type, new CandidateParameterResolver(parameters));
109     }
110 
111     @Override
112     public <T> T newInstanceWithParameterResolvers(Class<T> type, ParameterResolver... parameters) {
113         return provideInCurrentScope(() -> getParent().newInstanceWithParameterResolvers(getClosestMappedType(type), parameters));
114     }
115 
116     @Override
117     public GuiceComponentProvider getParent() {
118         return this.parentComponentProvider;
119     }
120 
121     private <T> T provideInCurrentScope(Supplier<T> provider) {
122         final UiContextReference previousUiContextReference = currentUiContextReference.getUiContextReference();
123         currentUiContextReference.setUiContextReference(this.uiContextReference);
124         try {
125             return provider.get();
126         } finally {
127             currentUiContextReference.setUiContextReference(previousUiContextReference);
128         }
129     }
130 }