-
Notifications
You must be signed in to change notification settings - Fork 283
/
AfterBeanDiscoveryImpl.java
336 lines (295 loc) · 14.6 KB
/
AfterBeanDiscoveryImpl.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.bootstrap.events;
import static org.jboss.weld.util.Observers.validateObserverMethod;
import static org.jboss.weld.util.reflection.Reflections.cast;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.spi.Context;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javax.enterprise.inject.spi.DefinitionException;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.Interceptor;
import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.inject.spi.Prioritized;
import javax.enterprise.inject.spi.configurator.BeanConfigurator;
import javax.enterprise.inject.spi.configurator.ObserverMethodConfigurator;
import org.jboss.weld.annotated.slim.SlimAnnotatedTypeStore;
import org.jboss.weld.bean.CustomDecoratorWrapper;
import org.jboss.weld.bean.attributes.ExternalBeanAttributesFactory;
import org.jboss.weld.bootstrap.BeanDeploymentArchiveMapping;
import org.jboss.weld.bootstrap.BeanDeploymentFinder;
import org.jboss.weld.bootstrap.ContextHolder;
import org.jboss.weld.bootstrap.enablement.GlobalEnablementBuilder;
import org.jboss.weld.bootstrap.event.InterceptorConfigurator;
import org.jboss.weld.bootstrap.event.WeldAfterBeanDiscovery;
import org.jboss.weld.bootstrap.events.builder.BeanBuilderImpl;
import org.jboss.weld.bootstrap.events.builder.BeanConfiguratorImpl;
import org.jboss.weld.bootstrap.events.builder.ObserverMethodBuilderImpl;
import org.jboss.weld.bootstrap.events.builder.ObserverMethodConfiguratorImpl;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.logging.BootstrapLogger;
import org.jboss.weld.logging.ContextLogger;
import org.jboss.weld.logging.InterceptorLogger;
import org.jboss.weld.logging.MetadataLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.util.Bindings;
import org.jboss.weld.util.Observers;
import org.jboss.weld.util.Preconditions;
public class AfterBeanDiscoveryImpl extends AbstractBeanDiscoveryEvent implements WeldAfterBeanDiscovery {
private static final String TYPE_ARGUMENT_NAME = "type";
public static void fire(BeanManagerImpl beanManager, Deployment deployment, BeanDeploymentArchiveMapping bdaMapping,
Collection<ContextHolder<? extends Context>> contexts) {
final AfterBeanDiscoveryImpl event = new AfterBeanDiscoveryImpl(beanManager, deployment, bdaMapping, contexts);
event.fire();
event.finish();
}
private AfterBeanDiscoveryImpl(BeanManagerImpl beanManager, Deployment deployment, BeanDeploymentArchiveMapping bdaMapping,
Collection<ContextHolder<? extends Context>> contexts) {
super(beanManager, WeldAfterBeanDiscovery.class, bdaMapping, deployment, contexts);
this.slimAnnotatedTypeStore = beanManager.getServices().get(SlimAnnotatedTypeStore.class);
this.containerLifecycleEvents = beanManager.getServices().get(ContainerLifecycleEvents.class);
this.additionalBeans = new LinkedList<BeanRegistration>();
this.additionalObservers = new LinkedList<ObserverMethod<?>>();
this.additionalObserverConfigurators = new LinkedList<ObserverMethodConfigurator<?>>();
}
private final SlimAnnotatedTypeStore slimAnnotatedTypeStore;
private final ContainerLifecycleEvents containerLifecycleEvents;
private final List<BeanRegistration> additionalBeans;
private final List<ObserverMethod<?>> additionalObservers;
private final List<ObserverMethodConfigurator<?>> additionalObserverConfigurators;
@Override
public void addBean(Bean<?> bean) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(bean, "bean");
ExternalBeanAttributesFactory.validateBeanAttributes(bean, getBeanManager());
validateBean(bean);
additionalBeans.add(BeanRegistration.of(bean, getReceiver()));
BootstrapLogger.LOG.addBeanCalled(getReceiver(), bean);
}
@Override
public <T> BeanConfigurator<T> addBean() {
checkWithinObserverNotification();
BeanConfiguratorImpl<T> configurator = new BeanConfiguratorImpl<>(getReceiver().getClass(), getBeanDeploymentFinder());
additionalBeans.add(BeanRegistration.of(configurator, getReceiver()));
return configurator;
}
@Override
public void addContext(Context context) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(context, "context");
Class<? extends Annotation> scope = context.getScope();
if (scope == null) {
throw ContextLogger.LOG.contextHasNullScope(context);
}
if (!getBeanManager().isScope(scope)) {
MetadataLogger.LOG.contextGetScopeIsNotAScope(scope, context);
}
if (scope == ApplicationScoped.class || scope == Dependent.class) {
throw ContextLogger.LOG.cannotRegisterContext(scope, context);
}
getBeanManager().addContext(context);
BootstrapLogger.LOG.addContext(getReceiver(), context);
}
@Override
public void addObserverMethod(ObserverMethod<?> observerMethod) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(observerMethod, "observerMethod");
validateObserverMethod(observerMethod, getBeanManager(), null);
additionalObservers.add(observerMethod);
BootstrapLogger.LOG.addObserverMethodCalled(getReceiver(), observerMethod);
}
@Override
public <T> ObserverMethodConfigurator<T> addObserverMethod() {
checkWithinObserverNotification();
ObserverMethodConfiguratorImpl<T> configurator = new ObserverMethodConfiguratorImpl<>();
additionalObserverConfigurators.add(configurator);
return configurator;
}
@Override
public <T> AnnotatedType<T> getAnnotatedType(Class<T> type, String id) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(type, TYPE_ARGUMENT_NAME);
return slimAnnotatedTypeStore.get(type, id);
}
@Override
public <T> Iterable<AnnotatedType<T>> getAnnotatedTypes(Class<T> type) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(type, TYPE_ARGUMENT_NAME);
return cast(slimAnnotatedTypeStore.get(type));
}
@Override
public InterceptorConfigurator addInterceptor() {
InterceptorConfiguratorImpl configurator = new InterceptorConfiguratorImpl(getBeanManager());
additionalBeans.add(BeanRegistration.of(configurator));
return configurator;
}
protected <T> void processBeanRegistration(BeanRegistration registration, GlobalEnablementBuilder globalEnablementBuilder) {
Bean<?> bean = registration.getBean();
BeanManagerImpl beanManager = registration.getBeanManager();
if (beanManager == null) {
// Get the bean manager for beans added via ABD#addBean()
beanManager = getOrCreateBeanDeployment(bean.getBeanClass()).getBeanManager();
} else {
// Also validate the bean produced by a builder
ExternalBeanAttributesFactory.validateBeanAttributes(bean, getBeanManager());
validateBean(bean);
}
// Custom beans (alternatives, interceptors, decorators) may also implement javax.enterprise.inject.spi.Prioritized
Integer priority = (bean instanceof Prioritized) ? ((Prioritized) bean).getPriority() : null;
if (bean instanceof Interceptor<?>) {
beanManager.addInterceptor((Interceptor<?>) bean);
if (priority != null) {
globalEnablementBuilder.addInterceptor(bean.getBeanClass(), priority);
}
} else if (bean instanceof Decorator<?>) {
beanManager.addDecorator(CustomDecoratorWrapper.of((Decorator<?>) bean, beanManager));
if (priority != null) {
globalEnablementBuilder.addDecorator(bean.getBeanClass(), priority);
}
} else {
beanManager.addBean(bean);
if (priority != null && bean.isAlternative()) {
globalEnablementBuilder.addAlternative(bean.getBeanClass(), priority);
}
}
containerLifecycleEvents.fireProcessBean(beanManager, bean, registration.getExtension());
}
private void validateBean(Bean<?> bean) {
if (bean.getBeanClass() == null) {
throw BeanLogger.LOG.beanMethodReturnsNull("getBeanClass", bean);
}
if (bean.getInjectionPoints() == null) {
throw BeanLogger.LOG.beanMethodReturnsNull("getInjectionPoints", bean);
}
if (bean instanceof PassivationCapable) {
PassivationCapable passivationCapable = (PassivationCapable) bean;
if (passivationCapable.getId() == null) {
throw BeanLogger.LOG.passivationCapableBeanHasNullId(bean);
}
}
if (bean instanceof Interceptor<?>) {
validateInterceptor((Interceptor<?>) bean);
} else if (bean instanceof Decorator<?>) {
validateDecorator((Decorator<?>) bean);
}
}
private void validateInterceptor(Interceptor<?> interceptor) {
Set<Annotation> bindings = interceptor.getInterceptorBindings();
if (bindings == null) {
throw InterceptorLogger.LOG.nullInterceptorBindings(interceptor);
}
for (Annotation annotation : bindings) {
if (!getBeanManager().isInterceptorBinding(annotation.annotationType())) {
throw MetadataLogger.LOG.notAnInterceptorBinding(annotation, interceptor);
}
}
}
private void validateDecorator(Decorator<?> decorator) {
Set<Annotation> qualifiers = decorator.getDelegateQualifiers();
if (decorator.getDelegateType() == null) {
throw BeanLogger.LOG.decoratorMethodReturnsNull("getDelegateType", decorator);
}
Bindings.validateQualifiers(qualifiers, getBeanManager(), decorator, "Decorator.getDelegateQualifiers");
if (decorator.getDecoratedTypes() == null) {
throw BeanLogger.LOG.decoratorMethodReturnsNull("getDecoratedTypes", decorator);
}
}
/**
* Bean and observer registration is delayed until after all {@link AfterBeanDiscovery} observers are notified.
*/
private void finish() {
try {
GlobalEnablementBuilder globalEnablementBuilder = getBeanManager().getServices().get(GlobalEnablementBuilder.class);
for (BeanRegistration registration : additionalBeans) {
processBeanRegistration(registration, globalEnablementBuilder);
}
for (ObserverMethod<?> observer : additionalObservers) {
processAdditionalObserver(observer);
}
for (ObserverMethodConfigurator<?> configurator : additionalObserverConfigurators) {
ObserverMethod<?> observer = new ObserverMethodBuilderImpl<>(cast(configurator)).build();
processAdditionalObserver(observer);
}
} catch (Exception e) {
throw new DefinitionException(e);
}
}
private void processAdditionalObserver(ObserverMethod<?> observer) {
validateObserverMethod(observer, getBeanManager(), null);
BeanManagerImpl manager = getOrCreateBeanDeployment(observer.getBeanClass()).getBeanManager();
if (Observers.isObserverMethodEnabled(observer, manager)) {
ObserverMethod<?> processedObserver = containerLifecycleEvents.fireProcessObserverMethod(manager, observer);
if (processedObserver != null) {
manager.addObserver(processedObserver);
}
}
}
private BeanDeploymentFinder getBeanDeploymentFinder() {
return new BeanDeploymentFinder(getBeanDeployments(), getDeployment(), getContexts(), getBeanManager());
}
private static class BeanRegistration {
private final Bean<?> bean;
private final BeanBuilderImpl<?> beanBuilder;
private final InterceptorConfiguratorImpl interceptorBuilder;
private final Extension extension;
static BeanRegistration of(Bean<?> bean, Extension extension) {
return new BeanRegistration(bean, null, null, extension);
}
static BeanRegistration of(BeanConfiguratorImpl<?> configurator, Extension extension) {
return new BeanRegistration(null, cast(new BeanBuilderImpl<>(configurator)), null, extension);
}
static BeanRegistration of(InterceptorConfiguratorImpl interceptorBuilder) {
return new BeanRegistration(null, null, interceptorBuilder, null);
}
BeanRegistration(Bean<?> bean, BeanBuilderImpl<?> beanBuilder, InterceptorConfiguratorImpl interceptorBuilder, Extension extension) {
this.bean = bean;
this.beanBuilder = beanBuilder;
this.interceptorBuilder = interceptorBuilder;
this.extension = extension;
}
public Bean<?> getBean() {
if (bean != null) {
return bean;
} else if (beanBuilder != null) {
return beanBuilder.build();
}
return interceptorBuilder.build();
}
protected BeanManagerImpl getBeanManager() {
if (bean != null) {
return null;
} else if (beanBuilder != null) {
return beanBuilder.getBeanManager();
}
return interceptorBuilder.getBeanManager();
}
public Extension getExtension() {
return extension;
}
}
}