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.guice.GuiceComponentProvider;
37  import info.magnolia.objectfactory.guice.GuiceUtils;
38  import info.magnolia.ui.api.ioc.AdminCentral;
39  import info.magnolia.ui.datasource.DatasourceDefinition;
40  import info.magnolia.ui.framework.ioc.DatasourceComponent.DatasourceComponentImpl;
41  
42  import java.util.Optional;
43  
44  import javax.inject.Inject;
45  
46  import com.google.inject.Injector;
47  import com.google.inject.Key;
48  import com.google.inject.Provider;
49  import com.google.inject.name.Names;
50  
51  /**
52   * <p>
53   * Special {@link Provider} implementation which is used
54   * when a certain type is bound differently in different contexts.
55   * </p>
56   * <p>
57   * This would be the case when e.g. app <b>bar</b> configures a type
58   * {@code Foo} to be implemented by {@code FooBarImpl} and app
59   * <b>baz</b> has a binding of {@code Foo} to {@code FooBazImpl}.
60   * </p>
61   * <p>
62   * In order to support such cases we assume that {@code Foo} is bound
63   * in a special way. It is expected that two concrete implementations
64   * of it are bound using a {@link com.google.inject.BindingAnnotation
65   * binding annotation} such as e.g.
66   * {@link info.magnolia.ui.api.ioc.App App}.
67   * </p>
68   * <p>
69   * By binding the un-annotated type to this provider we allow the developers
70   * to inject types like {@code Foo} without explicitly
71   * mentioning the binding annotations.
72   * This provider will use {@link CurrentUiContextReference} to fetch all the
73   * relevant UI contexts and will produce the possible Guice {@link Key keys}.
74   * The most suitable key is then picked, i.e. we pick the binding
75   * which is closest to the current UI context.
76   * </p>
77   *
78   * <p>
79   * For more information on binding annotations, see
80   * <a href="https://github.com/google/guice/wiki/BindingAnnotations">Guice Binding Annotations tutorial</a>.
81   * </p>
82   *
83   * @param <T> type of the provided instance.
84   *
85   * @see UiContextReference
86   * @see UiContextBoundComponentProvider
87   */
88  class UiContextApplyingProvider<T> implements Provider<T> {
89  
90      public static <U> UiContextApplyingProvider<U> of(Class<U> type) {
91          return new UiContextApplyingProvider<>(type);
92      }
93  
94      private @Inject Injector injector;
95  
96      private final Class<T> type;
97  
98      UiContextApplyingProvider(Class<T> type) {
99          this.type = type;
100     }
101 
102     @Override
103     public T get() {
104         UiContextBoundComponentProvider componentProvider = injector.getInstance(UiContextBoundComponentProvider.class);
105         // get all the UI contexts that are reachable from the current one
106         Key<T> boundKey = componentProvider.getReachableUiContextReferences()
107                 // produce a Guice type Key relevant to the UI context
108                 .map(uiContextReference -> Key.get(type, uiContextReference.getAnnotation()))
109                 // check whether such Key is bound to anything
110                 .filter(key -> GuiceUtils.hasExplicitBindingFor(injector, key))
111                 .findFirst()
112                 .orElseGet(() -> resolveRelatedDataSourceContextKey(type).orElse(null));
113 
114         if (boundKey == null) {
115             return getDefault(componentProvider.getComponent(CurrentUiContextReference.class));
116         }
117 
118         if (boundKey.getAnnotation() instanceof AdminCentral) {
119             return resolveRelatedDataSourceContextKey(type).map(injector::getInstance).orElseGet(() -> injector.getInstance(boundKey));
120         }
121 
122         return injector.getInstance(boundKey);
123 
124     }
125 
126     private Optional<Key<T>> resolveRelatedDataSourceContextKey(Class<T> type) {
127         return Optional.ofNullable(injector.getInstance(DatasourceDefinition.class))
128                 .map(DatasourceDefinition::getName)
129                 .map(dsName -> Key.get(type, new DatasourceComponentImpl(dsName)))
130                 .filter(key -> GuiceUtils.hasExplicitBindingFor(injector, key));
131     }
132 
133 
134     private T getDefault(CurrentUiContextReference context) {
135         return Optional.ofNullable(
136                 SessionStore.access().getBeanStore(context.getUiContextReference()).get(type))
137                 .orElseGet(() -> {
138                     try {
139                         return this.injector.getInstance(GuiceComponentProvider.class).newInstance(type);
140                     } catch (Exception e) {
141                         return this.injector.getInstance(Key.get(GuiceComponentProvider.class, Names.named("main"))).getComponent(type);
142                     }
143                 });
144     }
145 }