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  
37  import info.magnolia.event.EventBus;
38  
39  import java.util.Collections;
40  import java.util.HashMap;
41  import java.util.Map;
42  import java.util.Objects;
43  import java.util.Optional;
44  import java.util.stream.Stream;
45  
46  import com.google.common.collect.HashMultimap;
47  import com.google.common.collect.Multimap;
48  import com.google.inject.Key;
49  import com.vaadin.server.VaadinSession;
50  
51  /**
52   * Provides access to {@link VaadinSession} as if it was a key-value store where
53   * the key would be a {@link UiContextReference} and the value
54   * would be {@link BeanStore} (stores the scoped beans and provides access to them).
55   *
56   * @see UiScopes
57   */
58  public class SessionStore {
59  
60      /**
61       * Returns {@link SessionStore} corresponding to current {@link VaadinSession}.
62       */
63      public static SessionStore access() {
64          return access(VaadinSession.getCurrent());
65      }
66  
67      /**
68       * Returns {@link SessionStore} corresponding to provided {@link VaadinSession}.
69       */
70      public static SessionStore access(VaadinSession session) {
71          return session.getAttribute(SessionStore.class);
72      }
73  
74      private final VaadinSession vaadinSession;
75  
76      private final EventBus eventBus;
77  
78      private final Multimap<UiContextReference, UiContextReference> beanStoreHierarchy = HashMultimap.create();
79      private final Map<UiContextReference, UiContextReference> beanStoreParentReferences = new HashMap<>();
80  
81      public SessionStore(VaadinSession vaadinSession, EventBus eventBus) {
82          this.vaadinSession = vaadinSession;
83          this.eventBus = eventBus;
84      }
85  
86      public BeanStore createBeanStore(UiContextReference uiContextReference) {
87          BeanStore existingBeanStore = getBeanStore(uiContextReference);
88          if (existingBeanStore != null) {
89              return existingBeanStore;
90          }
91          return createBeanStore(uiContextReference, Collections.emptyMap());
92      }
93  
94      public BeanStore createBeanStore(UiContextReference key, Map<Key, Object> initialInstances) {
95  
96          final BeanStoreBeanStore.html#BeanStore">BeanStore beanStore = new BeanStore();
97          vaadinSession.setAttribute(key.asString(), beanStore);
98          key.getDirectParentReference().ifPresent(parentRef -> {
99              beanStoreHierarchy.put(parentRef, key);
100             beanStoreParentReferences.put(key, parentRef);
101         });
102         initialInstances.forEach(beanStore::put);
103 
104         eventBus.fireEvent(new BeanStoreLifecycleEvent.Create(key, beanStore));
105 
106         return beanStore;
107     }
108 
109     /**
110      * Finds a bean store which corresponds to a scope key.
111      * If such store does not exist - creates one.
112      */
113     public BeanStore getBeanStore(UiContextReference uiContextReference) {
114         return (BeanStore) vaadinSession.getAttribute(uiContextReference.asString());
115     }
116 
117     public Optional<UiContextReference> lookupRelatedUiContextReference(Object instance) {
118         return beanStoreHierarchy.values().stream()
119                 .filter(ref ->
120                         Optional.ofNullable(this.getBeanStore(ref))
121                                 .map(beanStore -> beanStore.contains(instance))
122                                 .orElse(false))
123                 .findFirst();
124     }
125 
126     /**
127      * Clears a bean store which corresponds to a ui context and removes it.
128      */
129     public void releaseBeanStore(UiContextReference uiContextReference) {
130         final BeanStore store = getBeanStore(uiContextReference);
131 
132         if (store == null) {
133             throw new IllegalArgumentException(String.format("No bean store registered for provided uiContextReference [%s]...", uiContextReference));
134         }
135 
136         eventBus.fireEvent(new BeanStoreLifecycleEvent.BeforeDestroy(uiContextReference, store));
137 
138         // clean up all the child bean stores and erase information from the hierarchy
139         this.beanStoreHierarchy.removeAll(uiContextReference).forEach(this::releaseBeanStore);
140         Optional.ofNullable(this.beanStoreParentReferences.remove(uiContextReference))
141                 .ifPresent(parentReference -> this.beanStoreHierarchy.remove(parentReference, uiContextReference));
142 
143         store.clear();
144         vaadinSession.setAttribute(uiContextReference.asString(), null);
145 
146         eventBus.fireEvent(new BeanStoreLifecycleEvent.Destroy(uiContextReference, store));
147     }
148 
149     public static <T> Stream<T> lookUpInstancesOf(UiContextReference uiContextReference, Class<T> clazz) {
150         BeanStore beanStore = SessionStore.access().getBeanStore(uiContextReference);
151         return Stream.concat(
152                 Stream.of(beanStore.getClosestInstance(clazz)),
153                 uiContextReference.getParentReferences().stream()
154                         .map(parentRef -> SessionStore.access().getBeanStore(parentRef))
155                         .filter(Objects::nonNull)
156                         .map(parentStore -> parentStore.getClosestInstance(clazz)))
157                 .filter(Objects::nonNull);
158     }
159 }