Skip to content

Commit

Permalink
WELD-2794 Change how we determine BM for synth beans in BCE that aren…
Browse files Browse the repository at this point in the history
…'t related to any bean archive
  • Loading branch information
manovotn committed Jul 31, 2024
1 parent cfb60e5 commit 0bad05e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ public BeanDeployment getOrCreateBeanDeployment(Class<?> clazz) {
return DeploymentStructures.getOrCreateBeanDeployment(deployment, deploymentManager, bdaMapping, contexts, clazz);
}

public BeanDeployment getBeanDeploymentIfExists(Class<?> clazz) {
return DeploymentStructures.getBeanDeploymentIfExists(deployment, bdaMapping, clazz);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void addBean(Bean<?> bean) {
public <T> WeldBeanConfigurator<T> addBean() {
// null is only going to occur if the invocation is outside of OM in which case it will fail in the
// subsequent method inside checkWithinObserverNotification()
return addBean(getReceiver() != null ? getReceiver().getClass() : null);
return addBean(getReceiver() != null ? getReceiver().getClass() : null, null);
}

/**
Expand All @@ -115,12 +115,14 @@ public <T> WeldBeanConfigurator<T> addBean() {
* This method should not be used anywhere else.
*
* @param receiverClass class of the Build Compatible extension performing synth. bean registration
* @param fallbackClass fallback receiver class for BCEs coming from non-CDI archives; can be null
* @param <T> bean type
* @return instance of {@link WeldBeanConfigurator}
*/
public <T> WeldBeanConfigurator<T> addBean(Class<?> receiverClass) {
public <T> WeldBeanConfigurator<T> addBean(Class<?> receiverClass, Class<?> fallbackClass) {
checkWithinObserverNotification();
BeanConfiguratorImpl<T> configurator = new BeanConfiguratorImpl<>(receiverClass, getBeanDeploymentFinder());
BeanConfiguratorImpl<T> configurator = new BeanConfiguratorImpl<>(receiverClass, fallbackClass,
getBeanDeploymentFinder());
// note that here we deliberately keep getReceiver() since the logging is related to registering portable extension
additionalBeans.add(BeanRegistration.of(configurator, getReceiver()));
return configurator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.spi.AnnotatedType;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanAttributes;
Expand All @@ -42,6 +43,7 @@
import org.jboss.weld.bean.BeanIdentifiers;
import org.jboss.weld.bean.StringBeanIdentifier;
import org.jboss.weld.bean.WeldBean;
import org.jboss.weld.bootstrap.BeanDeployment;
import org.jboss.weld.bootstrap.BeanDeploymentFinder;
import org.jboss.weld.bootstrap.event.WeldBeanConfigurator;
import org.jboss.weld.contexts.CreationalContextImpl;
Expand Down Expand Up @@ -83,10 +85,18 @@ public class BeanConfiguratorImpl<T> implements WeldBeanConfigurator<T>, Configu
* @param defaultBeanClass
* @param beanDeploymentFinder
*/
public BeanConfiguratorImpl(Class<?> defaultBeanClass, BeanDeploymentFinder beanDeploymentFinder) {
public BeanConfiguratorImpl(Class<?> defaultBeanClass, Class<?> fallbackClass, BeanDeploymentFinder beanDeploymentFinder) {
this.beanClass = defaultBeanClass;
this.injectionPoints = new HashSet<>();
this.beanManager = beanDeploymentFinder.getOrCreateBeanDeployment(beanClass).getBeanManager();
BeanDeployment beanDeployment = beanDeploymentFinder.getBeanDeploymentIfExists(beanClass);
if (beanDeployment == null && fallbackClass != null && BuildCompatibleExtension.class.isAssignableFrom(beanClass)) {
// case of synth beans coming from BCE with no backing archive
// use a fallback class, typically LiteExtensionTranslator.class
beanDeployment = beanDeploymentFinder.getOrCreateBeanDeployment(fallbackClass);
} else {
beanDeployment = beanDeploymentFinder.getOrCreateBeanDeployment(beanClass);
}
this.beanManager = beanDeployment.getBeanManager();
this.attributes = new BeanAttributesConfiguratorImpl<>(beanManager);
}

Expand Down
11 changes: 11 additions & 0 deletions impl/src/main/java/org/jboss/weld/util/DeploymentStructures.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ public static BeanDeployment getOrCreateBeanDeployment(Deployment deployment, Be
}
}

public static BeanDeployment getBeanDeploymentIfExists(Deployment deployment, BeanDeploymentArchiveMapping bdaMapping,
Class<?> clazz) {
BeanDeploymentArchive beanDeploymentArchive = deployment.loadBeanDeploymentArchive(clazz);
if (beanDeploymentArchive == null) {
throw UtilLogger.LOG.unableToFindBeanDeploymentArchive(clazz);
} else {
// can be null
return bdaMapping.getBeanDeployment(beanDeploymentArchive);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public void synthesis(@Priority(Integer.MAX_VALUE) @Observes jakarta.enterprise.
if (abd instanceof AfterBeanDiscoveryImpl) {
// specify the receiver class to be the BCE extension, linking the bean to it
// in EE env this affects the BM used for dynamic resolution inside bean creation method
configurator = ((AfterBeanDiscoveryImpl) abd).addBean(syntheticBean.extensionClass);
configurator = ((AfterBeanDiscoveryImpl) abd).addBean(syntheticBean.extensionClass,
LiteExtensionTranslator.class);
} else {
configurator = abd.addBean();
}
Expand Down

0 comments on commit 0bad05e

Please sign in to comment.