Skip to content

Commit

Permalink
HHH-18982 throw instead of log when BeanManager not on classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Jan 26, 2025
1 parent 9a04065 commit c5eba23
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import org.hibernate.InstantiationException;
import org.hibernate.Internal;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
import org.hibernate.type.spi.TypeBootstrapContext;
import org.hibernate.service.ServiceRegistry;

import java.lang.reflect.Constructor;
import java.util.Map;

import static org.hibernate.internal.util.ReflectHelper.getConstructorOrNull;

/**
* {@link BeanInstanceProducer} implementation for building beans related to custom types.
*
Expand All @@ -32,8 +33,8 @@ public TypeBeanInstanceProducer(ConfigurationService configurationService, Servi

@Override
public <B> B produceBeanInstance(Class<B> beanType) {
final Constructor<B> bootstrapContextAwareConstructor =
ReflectHelper.getConstructorOrNull( beanType, TypeBootstrapContext.class );
final Constructor<? extends B> bootstrapContextAwareConstructor =
getConstructorOrNull( beanType, TypeBootstrapContext.class );
if ( bootstrapContextAwareConstructor != null ) {
try {
return bootstrapContextAwareConstructor.newInstance( this );
Expand All @@ -43,7 +44,7 @@ public <B> B produceBeanInstance(Class<B> beanType) {
}
}
else {
final Constructor<B> constructor = ReflectHelper.getConstructorOrNull( beanType );
final Constructor<? extends B> constructor = getConstructorOrNull( beanType );
if ( constructor != null ) {
try {
return constructor.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,11 @@ private static Generator instantiateGenerator(
* @param beanContainer an optional {@code BeanContainer}
* @param generatorClass a class which implements {@code Generator}
*/
private static Generator instantiateGeneratorAsBean(
private static <T extends Generator> Generator instantiateGeneratorAsBean(
Annotation annotation,
BeanContainer beanContainer,
GeneratorCreationContext creationContext,
Class<? extends Generator> generatorClass,
Class<T> generatorClass,
MemberDetails memberDetails,
Class<? extends Annotation> annotationType) {
return Helper.getBean(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public ConnectionProvider initiateService(
}
else if ( explicitSetting instanceof Class<?> providerClass ) {
LOG.instantiatingExplicitConnectionProvider( providerClass.getName() );
return instantiateExplicitConnectionProvider( providerClass, beanContainer );
return instantiateExplicitConnectionProvider( connectionProviderClass( providerClass ), beanContainer );
}
else {
final String providerName = nullIfBlank( explicitSetting.toString() );
Expand All @@ -118,10 +118,21 @@ else if ( explicitSetting instanceof Class<?> providerClass ) {
return instantiateConnectionProvider( configurationValues, strategySelector, beanContainer );
}

private static Class<? extends ConnectionProvider> connectionProviderClass(Class<?> providerClass) {
if ( !ConnectionProvider.class.isAssignableFrom( providerClass ) ) {
throw new ConnectionProviderConfigurationException( "Class '" + providerClass.getName()
+ "' does not implement 'ConnectionProvider'" );
}
@SuppressWarnings("unchecked")
final Class<? extends ConnectionProvider> connectionProviderClass =
(Class<? extends ConnectionProvider>) providerClass;
return connectionProviderClass;
}

private ConnectionProvider instantiateNamedConnectionProvider(
String providerName, StrategySelector strategySelector, BeanContainer beanContainer) {
LOG.instantiatingExplicitConnectionProvider( providerName );
final Class<?> providerClass =
final Class<? extends ConnectionProvider> providerClass =
strategySelector.selectStrategyImplementor( ConnectionProvider.class, providerName );
try {
return instantiateExplicitConnectionProvider( providerClass, beanContainer );
Expand Down Expand Up @@ -192,7 +203,8 @@ private Class<? extends ConnectionProvider> getSingleRegisteredProvider(Strategy
: null;
}

private ConnectionProvider instantiateExplicitConnectionProvider(Class<?> providerClass, BeanContainer beanContainer) {
private <T extends ConnectionProvider> T instantiateExplicitConnectionProvider(
Class<T> providerClass, BeanContainer beanContainer) {
try {
if ( beanContainer != null ) {
return Helper.getBean(
Expand All @@ -202,7 +214,7 @@ private ConnectionProvider instantiateExplicitConnectionProvider(Class<?> provid
true,
() -> {
try {
return (ConnectionProvider) providerClass.getConstructor().newInstance();
return providerClass.getConstructor().newInstance();
}
catch (Exception e) {
throw new HibernateException( "Could not instantiate connection provider [" + providerClass.getName() + "]", e );
Expand All @@ -211,7 +223,7 @@ private ConnectionProvider instantiateExplicitConnectionProvider(Class<?> provid
);
}
else {
return (ConnectionProvider) providerClass.getConstructor().newInstance();
return providerClass.getConstructor().newInstance();
}
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import jakarta.enterprise.inject.spi.BeanManager;
import org.hibernate.AssertionFailure;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.resource.beans.container.spi.BeanContainer;
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
import org.hibernate.service.ServiceRegistry;

import static org.hibernate.cfg.ManagedBeanSettings.DELAY_CDI_ACCESS;

/**
* Helper class for building a CDI-based {@link BeanContainer}.
*
Expand All @@ -36,6 +37,6 @@ else if ( beanManager instanceof BeanManager cdiBeanManager ) {

private static boolean delayCdiAccess(ServiceRegistry serviceRegistry) {
return serviceRegistry.requireService( ConfigurationService.class )
.getSetting( AvailableSettings.DELAY_CDI_ACCESS, StandardConverters.BOOLEAN, false );
.getSetting( DELAY_CDI_ACCESS, StandardConverters.BOOLEAN, false );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package org.hibernate.resource.beans.container.internal;

import jakarta.enterprise.inject.spi.BeanManager;

import org.hibernate.Internal;
import org.hibernate.resource.beans.container.spi.AbstractCdiBeanContainer;
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
Expand All @@ -14,7 +13,7 @@
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
import org.hibernate.resource.beans.spi.BeanInstanceProducer;

import org.jboss.logging.Logger;
import static org.hibernate.resource.beans.internal.BeansMessageLogger.BEANS_MSG_LOGGER;

/**
* @author Steve Ebersole
Expand All @@ -23,15 +22,13 @@ public class CdiBeanContainerExtendedAccessImpl
extends AbstractCdiBeanContainer
implements ExtendedBeanManager.LifecycleListener {

// NOTE : we continue to use the deprecated form for now since that is what WildFly needs for the time being still

private static final Logger log = Logger.getLogger( CdiBeanContainerExtendedAccessImpl.class );
// NOTE : we continue to use the deprecated form for now since that is what WildFly needs for the time being

private BeanManager usableBeanManager;

CdiBeanContainerExtendedAccessImpl(ExtendedBeanManager beanManager) {
beanManager.registerLifecycleListener( this );
log.debugf( "Extended access requested to CDI BeanManager : %s", beanManager );
BEANS_MSG_LOGGER.extendedAccessToBeanManager();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
package org.hibernate.resource.beans.container.internal;

import jakarta.enterprise.inject.spi.BeanManager;

import org.hibernate.resource.beans.container.spi.AbstractCdiBeanContainer;
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
import org.hibernate.resource.beans.container.spi.ContainedBeanImplementor;
import org.hibernate.resource.beans.spi.BeanInstanceProducer;

import org.jboss.logging.Logger;
import static org.hibernate.resource.beans.internal.BeansMessageLogger.BEANS_MSG_LOGGER;

/**
* @author Steve Ebersole
*/
public class CdiBeanContainerImmediateAccessImpl extends AbstractCdiBeanContainer {
private static final Logger log = Logger.getLogger( CdiBeanContainerImmediateAccessImpl.class );

private final BeanManager beanManager;

CdiBeanContainerImmediateAccessImpl(BeanManager beanManager) {
log.debugf( "Standard access requested to CDI BeanManager : %s", beanManager );
BEANS_MSG_LOGGER.standardAccessToBeanManager();
this.beanManager = beanManager;
}

Expand All @@ -36,7 +34,8 @@ protected <B> ContainedBeanImplementor<B> createBean(
Class<B> beanType,
BeanLifecycleStrategy lifecycleStrategy,
BeanInstanceProducer fallbackProducer) {
final ContainedBeanImplementor<B> bean = lifecycleStrategy.createBean( beanType, fallbackProducer, this );
final ContainedBeanImplementor<B> bean =
lifecycleStrategy.createBean( beanType, fallbackProducer, this );
bean.initialize();
return bean;
}
Expand All @@ -47,12 +46,8 @@ protected <B> ContainedBeanImplementor<B> createBean(
Class<B> beanType,
BeanLifecycleStrategy lifecycleStrategy,
BeanInstanceProducer fallbackProducer) {
final ContainedBeanImplementor<B> bean = lifecycleStrategy.createBean(
name,
beanType,
fallbackProducer,
this
);
final ContainedBeanImplementor<B> bean =
lifecycleStrategy.createBean( name, beanType, fallbackProducer, this );
bean.initialize();
return bean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ protected Instance<B> resolveContainerInstance() {
return root.select( beanType, new NamedBeanQualifier( beanName ) );
}
catch (Exception e) {
throw new NoSuchBeanException( "Bean class not known to CDI : " + beanType.getName(), e );
throw new NoSuchBeanException( "Bean class not known to CDI: " + beanType.getName(), e );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public void initialize() {
beanInstance = fallbackProducer.produceBeanInstance( beanType );

try {
if ( this.creationalContext != null ) {
this.creationalContext.release();
if ( creationalContext != null ) {
creationalContext.release();
}
}
catch (Exception ignore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
import org.hibernate.resource.beans.container.internal.CdiBasedBeanContainer;
import org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy;
import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy;
import org.hibernate.resource.beans.internal.BeansMessageLogger;
import org.hibernate.resource.beans.internal.Helper;
import org.hibernate.resource.beans.spi.BeanInstanceProducer;

import static org.hibernate.resource.beans.internal.BeansMessageLogger.BEANS_MSG_LOGGER;

/**
* @author Steve Ebersole
*/
Expand All @@ -34,7 +35,6 @@ public <B> ContainedBean<B> getBean(
: createBean( beanType, lifecycleOptions, fallbackProducer );
}

@SuppressWarnings("unchecked")
private <B> ContainedBean<B> getCacheableBean(
Class<B> beanType,
LifecycleOptions lifecycleOptions,
Expand All @@ -43,6 +43,7 @@ private <B> ContainedBean<B> getCacheableBean(

final ContainedBeanImplementor<?> existing = beanCache.get( beanCacheKey );
if ( existing != null ) {
//noinspection unchecked
return (ContainedBeanImplementor<B>) existing;
}

Expand Down Expand Up @@ -77,15 +78,11 @@ public <B> ContainedBean<B> getBean(
Class<B> beanType,
LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) {
if ( lifecycleOptions.canUseCachedReferences() ) {
return getCacheableBean( beanName, beanType, lifecycleOptions, fallbackProducer );
}
else {
return createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
}
return lifecycleOptions.canUseCachedReferences()
? getCacheableBean( beanName, beanType, lifecycleOptions, fallbackProducer )
: createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
}

@SuppressWarnings("unchecked")
private <B> ContainedBeanImplementor<B> getCacheableBean(
String beanName,
Class<B> beanType,
Expand All @@ -95,10 +92,12 @@ private <B> ContainedBeanImplementor<B> getCacheableBean(

final ContainedBeanImplementor<?> existing = beanCache.get( beanCacheKey );
if ( existing != null ) {
//noinspection unchecked
return (ContainedBeanImplementor<B>) existing;
}

final ContainedBeanImplementor<B> bean = createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
final ContainedBeanImplementor<B> bean =
createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
beanCache.put( beanCacheKey, bean );
return bean;
}
Expand Down Expand Up @@ -133,7 +132,7 @@ protected final void forEachBean(Consumer<ContainedBeanImplementor<?>> consumer)

@Override
public final void stop() {
BeansMessageLogger.BEANS_MSG_LOGGER.stoppingBeanContainer( this );
BEANS_MSG_LOGGER.stoppingBeanContainer( this );
forEachBean( ContainedBeanImplementor::release );
registeredBeans.clear();
beanCache.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.jboss.logging.Logger.Level.DEBUG;
import static org.jboss.logging.Logger.Level.INFO;
import static org.jboss.logging.Logger.Level.WARN;

/**
* @author Steve Ebersole
Expand All @@ -33,20 +32,11 @@ public interface BeansMessageLogger {

BeansMessageLogger BEANS_MSG_LOGGER = Logger.getMessageLogger( MethodHandles.lookup(), BeansMessageLogger.class, LOGGER_NAME );

@LogMessage( level = WARN )
@Message(
id = 10005001,
value = "An explicit CDI BeanManager reference [%s] was passed to Hibernate, " +
"but CDI is not available on the Hibernate ClassLoader. This is likely " +
"going to lead to exceptions later on in bootstrap."
)
void beanManagerButCdiNotAvailable(Object cdiBeanManagerReference);

@LogMessage( level = INFO )
@Message(
id = 10005002,
value = "No explicit CDI BeanManager reference was passed to Hibernate, " +
"but CDI is available on the Hibernate ClassLoader."
"but CDI is available on the Hibernate class loader"
)
void noBeanManagerButCdiAvailable();

Expand All @@ -56,4 +46,18 @@ public interface BeansMessageLogger {
value = "Stopping BeanContainer: %s"
)
void stoppingBeanContainer(BeanContainer beanContainer);

@LogMessage( level = DEBUG )
@Message(
id = 10005006,
value = "Standard access to BeanManager"
)
void standardAccessToBeanManager();

@LogMessage( level = DEBUG )
@Message(
id = 10005007,
value = "Extended access to BeanManager"
)
void extendedAccessToBeanManager();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private FallbackBeanInstanceProducer() {
public <B> B produceBeanInstance(Class<B> beanType) {
log.tracef( "Creating ManagedBean(%s) using direct instantiation", beanType.getName() );
try {
Constructor<B> constructor = beanType.getDeclaredConstructor();
final Constructor<B> constructor = beanType.getDeclaredConstructor();
constructor.setAccessible( true );
return constructor.newInstance();
}
Expand Down
Loading

0 comments on commit c5eba23

Please sign in to comment.