View Javadoc
1   /**
2    * This file Copyright (c) 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.admincentral;
35  
36  import info.magnolia.context.AsynchronousContext;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.objectfactory.guice.GuiceComponentProvider;
41  import info.magnolia.ui.editor.NonRoundingConverterFactory;
42  import info.magnolia.ui.framework.ioc.CurrentUiContextReference;
43  import info.magnolia.ui.framework.ioc.SessionStore;
44  import info.magnolia.ui.framework.ioc.UiContextBoundComponentProvider;
45  import info.magnolia.ui.framework.ioc.UiContextReference;
46  
47  import java.util.HashMap;
48  import java.util.Map;
49  
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  import com.google.inject.Key;
54  import com.vaadin.annotations.JavaScript;
55  import com.vaadin.annotations.Push;
56  import com.vaadin.annotations.Theme;
57  import com.vaadin.server.VaadinRequest;
58  import com.vaadin.server.VaadinSession;
59  import com.vaadin.shared.communication.PushMode;
60  import com.vaadin.shared.ui.ui.Transport;
61  import com.vaadin.ui.UI;
62  import com.vaadin.ui.UIDetachedException;
63  
64  /**
65   * The Resurface UI is the root Vaadin application for the Magnolia 6.0 Admincentral.
66   */
67  // TODO restore pluggability via magnolia.properties OR resolve on webapp level
68  // Don't remove the theme quite yet; keep the old value in magnolia.properties until we unbundle the old admincentral
69  @Theme("resurface-admincentral")
70  @JavaScript({ "jquery-3.4.1.min.js", "jquery-migrate-3.0.1.min.js" })
71  @Push(transport = Transport.LONG_POLLING, value = PushMode.MANUAL)
72  public class ResurfaceUI extends UI {
73      private static final Logger log = LoggerFactory.getLogger(ResurfaceUI.class);
74      private ResurfaceUIPresenter presenter;
75      private GuiceComponentProvider guiceComponentProvider;
76      private AsynchronousContext.OperationFactory operationFactory;
77  
78      @Override
79      protected void init(VaadinRequest request) {
80          log.debug("Initializing Resurface Admincentral UI...");
81          getPage().setTitle("Admincentral - Magnolia");
82  
83          // Disable rounding for Vaadin's legacy string-to-floating-point converters
84          VaadinSession currentSession = VaadinSession.getCurrent();
85          currentSession.setConverterFactory(new NonRoundingConverterFactory());
86  
87          log.debug("Initializing ComponentProvider with Admincentral UiContext...");
88          UiContextReference admincentralUiReference = UiContextReference.ofUi(this);
89  
90          Map<Key, Object> instances = new HashMap<>();
91          instances.put(Key.get(CurrentUiContextReference.class), new CurrentUiContextReference());
92          SessionStore.access().createBeanStore(admincentralUiReference, instances);
93          // CurrentUiContextReference has to be put into the BeanStore before the ComponentProvider is constructed
94          ComponentProvider componentProvider = new UiContextBoundComponentProvider(admincentralUiReference);
95          // save the reference to the Guice component provider so that we can push it into context
96          // when accessing this UI from a background thread
97          this.guiceComponentProvider = (GuiceComponentProvider) componentProvider.getParent();
98  
99          setContent(componentProvider.getComponent(ResurfaceUILayout.class));
100 
101         this.operationFactory = componentProvider.getComponent(AsynchronousContext.OperationFactory.class);
102 
103         presenter = componentProvider.newInstance(ResurfaceUIPresenter.class, this);
104         presenter.start();
105     }
106 
107     @Override
108     public void accessSynchronously(Runnable runnable) throws UIDetachedException {
109             super.accessSynchronously(() -> {
110                 // check whether we're in UI context.
111                 // if we aren't, we need to push the UI Guice component provider
112                 // manually for the current action
113                 boolean isExecutingOutsideOfUiIoCContext = Components.getComponentProvider() != this.guiceComponentProvider;
114                 if (isExecutingOutsideOfUiIoCContext) {
115                     Components.pushProvider(guiceComponentProvider);
116                 }
117                 try {
118                     Runnable withMgnlContext = MgnlContext.hasInstance() ? runnable : operationFactory.wrap(runnable);
119                     withMgnlContext.run();
120                 } finally {
121                     if (isExecutingOutsideOfUiIoCContext) {
122                         Components.popProvider();
123                     }
124                 }
125             });
126     }
127 
128     @Override
129     public void detach() {
130         try {
131             SessionStore.access().releaseBeanStore(UiContextReference.ofCurrentUi());
132             presenter.stop();
133             // make sure the error handler covers the detach phase (it does not happen in service phase).
134             super.detach();
135         } catch (Exception e) {
136             getErrorHandler().error(new com.vaadin.server.ErrorEvent(e));
137         }
138     }
139 }