1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
66
67
68
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
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
94 ComponentProvider componentProvider = new UiContextBoundComponentProvider(admincentralUiReference);
95
96
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
111
112
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
134 super.detach();
135 } catch (Exception e) {
136 getErrorHandler().error(new com.vaadin.server.ErrorEvent(e));
137 }
138 }
139 }