
org.jboss.weld.bootstrap.events.AfterBeanDiscoveryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weld-se-shaded Show documentation
Show all versions of weld-se-shaded Show documentation
This jar bundles all the bits of Weld and CDI required for Java SE.
/*
* 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.Interceptor;
import javax.enterprise.inject.spi.ObserverMethod;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.inject.spi.Prioritized;
import javax.enterprise.inject.spi.builder.BeanConfigurator;
import javax.enterprise.inject.spi.builder.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.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.experimental.ExperimentalAfterBeanDiscovery;
import org.jboss.weld.experimental.InterceptorBuilder;
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 ExperimentalAfterBeanDiscovery {
private static final String TYPE_ARGUMENT_NAME = "type";
public static void fire(BeanManagerImpl beanManager, Deployment deployment, BeanDeploymentArchiveMapping bdaMapping,
Collection> contexts) {
final AfterBeanDiscoveryImpl event = new AfterBeanDiscoveryImpl(beanManager, deployment, bdaMapping, contexts);
event.fire();
event.finish();
}
private AfterBeanDiscoveryImpl(BeanManagerImpl beanManager, Deployment deployment, BeanDeploymentArchiveMapping bdaMapping,
Collection> contexts) {
super(beanManager, ExperimentalAfterBeanDiscovery.class, bdaMapping, deployment, contexts);
this.slimAnnotatedTypeStore = beanManager.getServices().get(SlimAnnotatedTypeStore.class);
this.containerLifecycleEvents = beanManager.getServices().get(ContainerLifecycleEvents.class);
this.additionalBeans = new LinkedList();
this.additionalObservers = new LinkedList>();
this.additionalObserverConfigurators = new LinkedList>();
}
private final SlimAnnotatedTypeStore slimAnnotatedTypeStore;
private final ContainerLifecycleEvents containerLifecycleEvents;
private final List additionalBeans;
private final List> additionalObservers;
private final List> additionalObserverConfigurators;
@Override
public void addBean(Bean> bean) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(bean, "bean");
ExternalBeanAttributesFactory.validateBeanAttributes(bean, getBeanManager());
validateBean(bean);
additionalBeans.add(BeanRegistration.of(bean));
BootstrapLogger.LOG.addBeanCalled(getReceiver(), bean);
}
@Override
public BeanConfigurator addBean() {
checkWithinObserverNotification();
BeanConfiguratorImpl configurator = new BeanConfiguratorImpl<>(getReceiver().getClass(), getBeanDeploymentFinder());
additionalBeans.add(BeanRegistration.of(configurator));
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 ObserverMethodConfigurator addObserverMethod() {
checkWithinObserverNotification();
ObserverMethodConfiguratorImpl configurator = new ObserverMethodConfiguratorImpl<>();
additionalObserverConfigurators.add(configurator);
return configurator;
}
@Override
public AnnotatedType getAnnotatedType(Class type, String id) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(type, TYPE_ARGUMENT_NAME);
return slimAnnotatedTypeStore.get(type, id);
}
@Override
public Iterable> getAnnotatedTypes(Class type) {
checkWithinObserverNotification();
Preconditions.checkArgumentNotNull(type, TYPE_ARGUMENT_NAME);
return cast(slimAnnotatedTypeStore.get(type));
}
@Override
public InterceptorBuilder interceptorBuilder() {
return new InterceptorBuilderImpl(getBeanManager());
}
public InterceptorBuilder addInterceptor() {
InterceptorBuilderImpl builder = new InterceptorBuilderImpl(getBeanManager());
additionalBeans.add(BeanRegistration.of(builder));
return builder;
}
protected 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);
}
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 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 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();
validateObserverMethod(observer, getBeanManager(), null);
processAdditionalObserver(observer);
}
} catch (Exception e) {
throw new DefinitionException(e);
}
}
private void processAdditionalObserver(ObserverMethod> observer) {
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 InterceptorBuilderImpl interceptorBuilder;
static BeanRegistration of(Bean> bean) {
return new BeanRegistration(bean, null, null);
}
static BeanRegistration of(BeanConfiguratorImpl> configurator) {
return new BeanRegistration(null, cast(new BeanBuilderImpl<>(configurator)),null);
}
static BeanRegistration of(InterceptorBuilderImpl interceptorBuilder) {
return new BeanRegistration(null, null, interceptorBuilder);
}
BeanRegistration(Bean> bean, BeanBuilderImpl> beanBuilder, InterceptorBuilderImpl interceptorBuilder) {
this.bean = bean;
this.beanBuilder = beanBuilder;
this.interceptorBuilder = interceptorBuilder;
}
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();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy