View Javadoc
1   /**
2    * This file Copyright (c) 2017-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.annotation.LazySingleton;
37  
38  import java.lang.annotation.Annotation;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.Objects;
42  
43  import javax.inject.Singleton;
44  
45  import com.google.inject.AbstractModule;
46  import com.google.inject.Key;
47  import com.google.inject.Module;
48  import com.google.inject.binder.ScopedBindingBuilder;
49  import com.google.inject.internal.Scoping;
50  import com.google.inject.spi.Element;
51  import com.google.inject.spi.Elements;
52  
53  /**
54   * Wrapper Guice module which inspects all the {@link com.google.inject.Binding bindings} of the source module
55   * and re-scopes the {@link Singleton singletons} and {@link LazySingleton lazy singletons}
56   * to the appropriate UI-scopes.
57   * <p>
58   * <strong>NOTE:</strong> unfortunately due to how Guice processes the bindings we cannot
59   * detect and process the singletons that are configured via annotations without explicit
60   * binding. Such scoping info will be available only on the injector level which is too late
61   * for us. In order to handle such 'snaeaky' bindings there is another wrapper module
62   * {@link EliminateRedundantSingletonAnnotations}.
63   * </p>
64   */
65  final class RebindUiComponentScopes extends AbstractModule {
66  
67      private final Module sourceModule;
68  
69      RebindUiComponentScopes(Module sourceModule) {
70          this.sourceModule = sourceModule;
71      }
72  
73      @Override
74      @SuppressWarnings("unchecked")
75      protected void configure() {
76          final List<Element> elements = Elements.getElements(sourceModule);
77          final List<Element> elementsToWrite = new ArrayList<>(elements);
78  
79          GuiceSpi.getBindings(elements).forEach(binding -> {
80              final Scoping scoping = GuiceSpi.resolveScope(binding);
81  
82              if (scoping == null) {
83                  return;
84              }
85  
86              final Class<? extends Annotation> scopeAnnotation = scoping.getScopeAnnotation();
87  
88              if (isSingletonScopeAnnotation(scopeAnnotation)) {
89                  boolean isEagerSingleton = isEagerSingletonAnnotation(scopeAnnotation);
90                  final UiContextAnnotation uiContextAnnotation = UiAnnotations.cast(binding.getKey().getAnnotation());
91                  final Class<? extends Annotation> relatedScopeAnnotation = uiContextAnnotation.getRelatedScopeAnnotation(isEagerSingleton);
92  
93                  if (!Objects.equals(relatedScopeAnnotation, scopeAnnotation)) {
94                      elementsToWrite.remove(binding);
95                      final Key key = binding.getKey();
96                      GuiceSpi.<ScopedBindingBuilder>inspect(binding)
97                              .onLinkedBinding(linkedKeyBinding -> bind(key).to(linkedKeyBinding.getLinkedKey()))
98                              .onProviderKeyBinding(providerBinding -> bind(key).toProvider(providerBinding.getProviderKey()))
99                              .visit()
100                             .ifPresent(scopedBindingBuilder -> scopedBindingBuilder.in(relatedScopeAnnotation));
101                 }
102             }
103         });
104 
105         elementsToWrite.forEach(element -> element.applyTo(binder()));
106     }
107 
108     private boolean isEagerSingletonAnnotation(Class<? extends Annotation> scopeAnnotation) {
109         return scopeAnnotation.equals(Singleton.class) || scopeAnnotation.equals(com.google.inject.Singleton.class);
110     }
111 
112     private boolean isSingletonScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
113         return Objects.equals(scopeAnnotation, Singleton.class) ||
114                Objects.equals(scopeAnnotation, com.google.inject.Singleton.class) ||
115                Objects.equals(scopeAnnotation, LazySingleton.class);
116     }
117 }
118