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 java.util.Objects; 37 38 import com.google.inject.Key; 39 import com.google.inject.Provider; 40 41 /** 42 * Special {@link com.google.inject.Scope scope} implementation which allows to proxy/cache 43 * instances coming from a specified scope in other scopes. 44 * 45 * Depending on the {@link CurrentUiContextReference} state proxy scope will behave 46 * differently. 47 * If the current scope is the specified original scope, proxy scope will 48 * merely delegate instance scoping to it. 49 * 50 * Otherwise, if the current scope is different from the original, the 51 * instance provider from the original scope will be passed to the 52 * {@link ProxyScope#proxyScope(Key, Provider, SessionStoreScope)} method. 53 * 54 * A known use case for such scope is the {@link UiBaseModule.EventBusProxyScope 55 * event bus protection mechanism}. 56 */ 57 abstract class ProxyScope implements UiScope { 58 59 private final SessionStoreScope originalScope; 60 private final UiScopes allUiScopes; 61 62 /** 63 * @param originalScope the scope where the instances are coming from. 64 */ 65 ProxyScope(SessionStoreScope originalScope, UiScopes allUiScopes) { 66 this.originalScope = originalScope; 67 this.allUiScopes = allUiScopes; 68 } 69 70 /** 71 * Proxy an instance coming from the original scope in the current one. 72 * 73 * @param <T> type of injected instance 74 * @param key injected instance key 75 * @param scopedProvider provider from the original scope 76 * @param currentScope actual current scope in which the injection happens 77 * @return provider which somehow wraps the {@code baseScopeProvider} and augments 78 * its result according to the current scope. 79 */ 80 protected abstract <T> Provider<T> proxyScope(Key<T> key, Provider<T> scopedProvider, SessionStoreScope currentScope); 81 82 protected final SessionStoreScope getOriginalScope() { 83 return this.originalScope; 84 } 85 86 protected final UiScopes getAllUiScopes() { 87 return allUiScopes; 88 } 89 90 @Override 91 public <T> Provider<T> scope(Key<T> key, Provider<T> creator) { 92 final Provider<T> scopedProvider = originalScope.scope(key, creator); 93 94 return () -> { 95 boolean isEager = originalScope.isEager(); 96 final UiContextReference currentUiContextReference = CurrentUiContextReference.get().getUiContextReference(); 97 final UiContextAnnotation uiContextAnnotation = UiAnnotations.cast(currentUiContextReference.getAnnotation()); 98 99 final SessionStoreScope currentScope = allUiScopes.getScope(uiContextAnnotation.getRelatedScopeAnnotation(isEager)); 100 101 if (Objects.equals(originalScope, currentScope)) { 102 return scopedProvider.get(); 103 } 104 105 return proxyScope(key, scopedProvider, currentScope).get(); 106 }; 107 } 108 }