-
Notifications
You must be signed in to change notification settings - Fork 109
/
CdiExtension.java
279 lines (252 loc) · 12.2 KB
/
CdiExtension.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.faces.cdi;
import static com.sun.faces.cdi.CdiUtils.addAnnotatedTypes;
import static com.sun.faces.cdi.CdiUtils.getAnnotation;
import static java.util.Collections.unmodifiableMap;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.faces.push.WebsocketChannelManager;
import com.sun.faces.push.WebsocketSessionManager;
import com.sun.faces.push.WebsocketUserManager;
import com.sun.faces.util.FacesLogger;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.AfterBeanDiscovery;
import jakarta.enterprise.inject.spi.AfterDeploymentValidation;
import jakarta.enterprise.inject.spi.AnnotatedField;
import jakarta.enterprise.inject.spi.BeanManager;
import jakarta.enterprise.inject.spi.BeforeBeanDiscovery;
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.ProcessBean;
import jakarta.enterprise.inject.spi.ProcessManagedBean;
import jakarta.faces.annotation.ManagedProperty;
import jakarta.faces.model.DataModel;
import jakarta.faces.model.FacesDataModel;
/**
* The CDI extension.
*/
public class CdiExtension implements Extension {
/**
* Array of Mojarra impl specific CDI managed bean classes to add to CDI.
* This is necessary in case Mojarra is provided by server instead of by webapp as is often the case in Jakarta EE environments.
*/
private static final Class<?>[] MOJARRA_MANAGED_BEANS = {
WebsocketUserManager.class,
WebsocketSessionManager.class,
WebsocketChannelManager.class,
WebsocketChannelManager.ViewScope.class,
InjectionPointGenerator.class,
WebsocketPushContextProducer.class
};
/**
* Map of classes that can be wrapped by a data model to data model implementation classes
*/
private Map<Class<?>, Class<? extends DataModel<?>>> forClassToDataModelClass = new HashMap<>();
/**
* Map of {@code @ManagedProperty} target types
*/
private final Set<Type> managedPropertyTargetTypes = new HashSet<>();
/**
* Stores the logger.
*/
private static final Logger LOGGER = FacesLogger.APPLICATION_VIEW.getLogger();
// As per CDI spec this is the invocation order:
// 1. BeforeBeanDiscovery
// 2. ProcessAnnotatedType and ProcessSyntheticAnnotatedType
// 3. AfterTypeDiscovery
// 4. ProcessInjectionTarget and ProcessProducer
// 5. ProcessInjectionPoint
// 6. ProcessBeanAttributes
// 7. ProcessBean, ProcessManagedBean, ProcessSessionBean, ProcessProducerMethod, ProcessProducerField and ProcessSyntheticBean
// 8. ProcessObserverMethod and ProcessSyntheticObserverMethod
// 9. AfterBeanDiscovery
// 10. AfterDeploymentValidation
/**
* BeforeBeanDiscovery:
* <ul>
* <li>add impl specific managed beans
* </ul>
*
* @param beforeBeanDiscovery the before bean discovery.
* @param beanManager the bean manager.
*/
public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBeanDiscovery, BeanManager beanManager) {
addAnnotatedTypes(beforeBeanDiscovery, beanManager, MOJARRA_MANAGED_BEANS);
}
/**
* ProcessBean:
* <ul>
* <li>if bean is annotated with {@code @FacesDataModel} then collect it for {@link #afterDeploymentValidation(AfterDeploymentValidation, BeanManager)}
* </ul>
*
* @param <T> the generic bean type
* @param processBeanEvent the process bean event
* @param beanManager the current bean manager
*/
@SuppressWarnings("unchecked")
public <T extends DataModel<?>> void processBean(@Observes ProcessBean<T> processBeanEvent, BeanManager beanManager) {
try {
ProcessBean<T> event = processBeanEvent; // JDK8 u60 workaround - https://web.archive.org/web/20161007164846/http://mail.openjdk.java.net/pipermail/lambda-dev/2015-August/012146.html/012146.html
getAnnotation(beanManager, event.getAnnotated(), FacesDataModel.class)
.ifPresent(model -> forClassToDataModelClass.put(model.forClass(), (Class<T>) event.getBean().getBeanClass()));
}
catch (Exception e) {
// Log and continue; if we are not allowed somehow to investigate this ManagedBean, we're unlikely to be interested in it anyway,
// but logging at WARNING level is important
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("Exception happened during processBean: " + e);
}
}
}
/**
* ProcessManagedBean:
* <ul>
* <li>if bean has field with {@code @ManagedProperty} then collect its type for {@link #afterBeanDiscovery(AfterBeanDiscovery, BeanManager)}
* </ul>
*
* @param <T> the generic bean type
* @param processManagedBeanEvent the process managed bean event
* @param beanManager the current bean manager
*/
public <T> void processManagedBean(@Observes ProcessManagedBean<T> processManagedBeanEvent, BeanManager beanManager) {
try {
for (AnnotatedField<? super T> field : processManagedBeanEvent.getAnnotatedBeanClass().getFields()) {
Type type = field.getBaseType();
if (field.isAnnotationPresent(ManagedProperty.class) && (type instanceof Class || type instanceof ParameterizedType)) {
managedPropertyTargetTypes.add(type);
}
}
}
catch (Exception e) {
// Log and continue; if we are not allowed somehow to investigate this ManagedBean, we're unlikely to be interested in it anyway,
// but logging at WARNING level is important
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning("Exception happened during processManagedBean: " + e);
}
}
}
/**
* AfterBeanDiscovery:
* <ul>
* <li>add all CDI producer beans allowing EL resolving of Faces specific artifacts
* <li>add a managed property type producer bean for each managed property type discovered in {@link #processManagedBean(ProcessManagedBean, BeanManager)}
* </ul>
*
* @param afterBeanDiscovery the after bean discovery.
* @param beanManager the bean manager.
*/
public void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
// Ideally below should only happen if Jakarta Faces is considered active,
// but this is not detectable as ServletContext is not necessarily available at this moment.
afterBeanDiscovery.addBean(new ApplicationProducer());
afterBeanDiscovery.addBean(new ApplicationMapProducer(beanManager));
afterBeanDiscovery.addBean(new CompositeComponentProducer(beanManager));
afterBeanDiscovery.addBean(new ComponentProducer(beanManager));
afterBeanDiscovery.addBean(new FlashProducer(beanManager));
afterBeanDiscovery.addBean(new FlowProducer(beanManager));
afterBeanDiscovery.addBean(new FlowMapProducer(beanManager));
afterBeanDiscovery.addBean(new HeaderMapProducer(beanManager));
afterBeanDiscovery.addBean(new HeaderValuesMapProducer(beanManager));
afterBeanDiscovery.addBean(new InitParameterMapProducer(beanManager));
afterBeanDiscovery.addBean(new RequestParameterMapProducer(beanManager));
afterBeanDiscovery.addBean(new RequestParameterValuesMapProducer(beanManager));
afterBeanDiscovery.addBean(new RequestProducer());
afterBeanDiscovery.addBean(new RequestMapProducer(beanManager));
afterBeanDiscovery.addBean(new ResourceHandlerProducer(beanManager));
afterBeanDiscovery.addBean(new ExternalContextProducer(beanManager));
afterBeanDiscovery.addBean(new FacesContextProducer(beanManager));
afterBeanDiscovery.addBean(new RequestCookieMapProducer(beanManager));
afterBeanDiscovery.addBean(new SessionProducer());
afterBeanDiscovery.addBean(new SessionMapProducer(beanManager));
afterBeanDiscovery.addBean(new ViewMapProducer(beanManager));
afterBeanDiscovery.addBean(new ViewProducer(beanManager));
afterBeanDiscovery.addBean(new DataModelClassesMapProducer(beanManager));
for (Type type : managedPropertyTargetTypes) {
afterBeanDiscovery.addBean(new ManagedPropertyProducer(type, beanManager));
}
}
/**
* AfterDeploymentValidation:
* <ul>
* <li>sort faces data models discovered in {@link #processBean(ProcessBean, BeanManager)} for use by {@link DataModelClassesMapProducer}
* </ul>
*
* @param event the after deployment validation event
* @param beanManager the current bean manager
*/
public void afterDeploymentValidation(@Observes AfterDeploymentValidation event, BeanManager beanManager) {
// Sort the classes wrapped by a DataModel that we collected in processBean() such that
// for any 2 classes X and Y from this collection, if an object of X is an instanceof an object of Y,
// X appears in the collection before Y. The collection's sorting is otherwise arbitrary.
//
// E.g.
//
// Given class B, class A extends B and class Q, two possible orders are;
// 1. {A, B, Q}
// 2. {Q, A, B}
//
// The only requirement here is that A appears before B, since A is a subclass of B.
//
// This sorting is used so given an instance of type Z that's being bound to a UIData or UIRepeat
// component, we can find the most specific DataModel that can wrap Z by iterating through the sorted
// collection from beginning to end and stopping this iteration at the first match.
List<Class<?>> sortedForDataModelClasses = new ArrayList<>();
for (Class<?> clazz : forClassToDataModelClass.keySet()) {
int highestSuper = -1;
boolean added = false;
for (int i = 0; i < sortedForDataModelClasses.size(); i++) {
if (sortedForDataModelClasses.get(i).isAssignableFrom(clazz)) {
sortedForDataModelClasses.add(i, clazz);
added = true;
break;
} else if (clazz.isAssignableFrom(sortedForDataModelClasses.get(i))) {
highestSuper = i;
}
}
if (!added) {
if (highestSuper > -1) {
sortedForDataModelClasses.add(highestSuper + 1, clazz);
} else {
sortedForDataModelClasses.add(clazz);
}
}
}
// Use the sorting computed above to order the Map on this. Note that a linked hash map is used
// to preserve this ordering.
Map<Class<?>, Class<? extends DataModel<?>>> linkedForClassToDataModelClass = new LinkedHashMap<>();
for (Class<?> sortedClass : sortedForDataModelClasses) {
linkedForClassToDataModelClass.put(sortedClass, forClassToDataModelClass.get(sortedClass));
}
forClassToDataModelClass = unmodifiableMap(linkedForClassToDataModelClass);
}
/**
* Gets the map of classes that can be wrapped by a data model to data model implementation classes
*
* @return Map of classes that can be wrapped by a data model to data model implementation classes
*/
public Map<Class<?>, Class<? extends DataModel<?>>> getForClassToDataModelClass() {
return forClassToDataModelClass;
}
}