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.ui.framework.ioc;
35
36 import info.magnolia.event.EventBus;
37 import info.magnolia.ui.api.ioc.AdmincentralScoped;
38 import info.magnolia.ui.api.ioc.AdmincentralScopedEager;
39 import info.magnolia.ui.api.ioc.AppScoped;
40 import info.magnolia.ui.api.ioc.AppScopedEager;
41 import info.magnolia.ui.api.ioc.SubAppScoped;
42 import info.magnolia.ui.api.ioc.SubAppScopedEager;
43 import info.magnolia.ui.api.ioc.UiContextScoped;
44 import info.magnolia.ui.api.ioc.ViewScoped;
45 import info.magnolia.ui.api.ioc.ViewScopedEager;
46
47 import java.lang.annotation.Annotation;
48 import java.util.HashMap;
49 import java.util.Map;
50
51 import com.google.inject.Binder;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 class UiScopes {
97
98 private final Map<Class<? extends Annotation>, SessionStoreScope> scopes = new HashMap<>();
99
100 UiScopes(EventBus systemEventBus) {
101 this.eagerAppScope = new EagerSessionStoreScope(appScope, systemEventBus, this);
102 this.eagerAdmincentralScope = new EagerSessionStoreScope(admincentralScope, systemEventBus, this);
103 this.eagerSubAppScope = new EagerSessionStoreScope(subAppScope, systemEventBus, this);
104 this.eagerViewScope = new EagerSessionStoreScope(viewScope, systemEventBus, this);
105 this.eagerCurrentUiContextScope = new EagerSessionStoreScope(uiContextScope, systemEventBus, this);
106
107 scopes.put(AppScoped.class, this.appScope());
108 scopes.put(SubAppScoped.class, this.subAppScope());
109 scopes.put(AdmincentralScoped.class, this.admincentralScope());
110 scopes.put(ViewScoped.class, this.viewScope());
111 scopes.put(UiContextScoped.class, this.currentUiContextScope());
112
113 scopes.put(AppScopedEager.class, this.eagerAppScope());
114 scopes.put(AdmincentralScopedEager.class, this.eagerAdmincentralScope());
115 scopes.put(SubAppScopedEager.class, this.eagerSubAppScope());
116 scopes.put(ViewScopedEager.class, this.eagerViewScope());
117 }
118
119 void bind(Binder binder) {
120 scopes.forEach(binder::bindScope);
121 }
122
123 SessionStoreScope getScope(Class<? extends Annotation> annotation) {
124 return this.scopes.get(annotation);
125 }
126
127 SessionStoreScope getScope(Annotation annotation) {
128 return this.getScope(annotation.annotationType());
129 }
130
131
132
133
134
135
136 private final SessionStoreScopee.html#SessionStoreScope">SessionStoreScope admincentralScope = new SessionStoreScope() {
137 @Override
138 protected UiContextReference getCurrentContextKey() {
139 return UiContextReference.ofCurrentUi();
140 }
141
142 @Override
143 public String toString() {
144 return "AdminCentral scope";
145 }
146 };
147
148
149
150
151
152 private final SessionStoreScopeStoreScope.html#SessionStoreScope">SessionStoreScope appScope = new SessionStoreScope() {
153 @Override
154 protected UiContextReference getCurrentContextKey() {
155 return CurrentUiContextReference.get().getAppReference().orElseThrow(() -> new IllegalStateException("Not in app scope, can't resolve current app store!"));
156 }
157
158 @Override
159 public String toString() {
160 return "App scope";
161 }
162 };
163
164
165
166
167
168 private final SessionStoreScopetoreScope.html#SessionStoreScope">SessionStoreScope viewScope = new SessionStoreScope() {
169 @Override
170 protected UiContextReference getCurrentContextKey() {
171 return CurrentUiContextReference.get().getViewReference().orElseThrow(() -> new IllegalStateException("Not in a view scope, cannot resolve the corresponding bean store!"));
172 }
173
174 @Override
175 public String toString() {
176 return "View scope";
177 }
178 };
179
180
181
182
183
184 private final SessionStoreScopereScope.html#SessionStoreScope">SessionStoreScope subAppScope = new SessionStoreScope() {
185
186 @Override
187 protected UiContextReference getCurrentContextKey() {
188 return CurrentUiContextReference.get().getSubAppReference().orElseThrow(() -> new IllegalStateException("Not in sub-app scope, can't resolve current sub-app store!"));
189 }
190
191 @Override
192 public String toString() {
193 return "Sub-app scope";
194 }
195 };
196
197 private final SessionStoreScopecope.html#SessionStoreScope">SessionStoreScope uiContextScope = new SessionStoreScope() {
198 @Override
199 protected UiContextReference getCurrentContextKey() {
200 return CurrentUiContextReference.get().getUiContextReference();
201 }
202
203 @Override
204 public String toString() {
205 return "Current UI context scope";
206 }
207 };
208
209 private final EagerSessionStoreScope eagerAdmincentralScope;
210
211 private final EagerSessionStoreScope eagerAppScope;
212
213 private final EagerSessionStoreScope eagerSubAppScope;
214
215 private final EagerSessionStoreScope eagerViewScope;
216
217 private final SessionStoreScope eagerCurrentUiContextScope;
218
219 SessionStoreScope appScope() {
220 return this.appScope;
221 }
222
223 SessionStoreScope admincentralScope() {
224 return this.admincentralScope;
225 }
226
227 SessionStoreScope subAppScope() {
228 return this.subAppScope;
229 }
230
231 SessionStoreScope viewScope() {
232 return this.viewScope;
233 }
234
235 SessionStoreScope currentUiContextScope() {
236 return this.uiContextScope;
237 }
238
239 SessionStoreScope eagerAppScope() {
240 return this.eagerAppScope;
241 }
242
243 SessionStoreScope eagerSubAppScope() {
244 return this.eagerSubAppScope;
245 }
246
247 SessionStoreScope eagerAdmincentralScope() {
248 return this.eagerAdmincentralScope;
249 }
250
251 SessionStoreScope eagerViewScope() {
252 return this.eagerViewScope;
253 }
254
255 SessionStoreScope eagerCurrentUiContextScope() {
256 return this.eagerCurrentUiContextScope;
257 }
258 }