From c5eba23cec9e1689501ba1788910c742623e18d4 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sun, 26 Jan 2025 10:47:03 +0100 Subject: [PATCH] HHH-18982 throw instead of log when BeanManager not on classpath --- .../internal/TypeBeanInstanceProducer.java | 9 ++-- .../boot/model/internal/GeneratorBinder.java | 4 +- .../internal/ConnectionProviderInitiator.java | 22 +++++++--- .../internal/CdiBeanContainerBuilder.java | 5 ++- .../CdiBeanContainerExtendedAccessImpl.java | 9 ++-- .../CdiBeanContainerImmediateAccessImpl.java | 17 +++----- .../ContainerManagedLifecycleStrategy.java | 2 +- .../JpaCompliantLifecycleStrategy.java | 4 +- .../spi/AbstractCdiBeanContainer.java | 21 +++++----- .../beans/internal/BeansMessageLogger.java | 26 +++++++----- .../FallbackBeanInstanceProducer.java | 2 +- .../resource/beans/internal/Helper.java | 42 +++++++++---------- .../spi/ManagedBeanRegistryInitiator.java | 23 ++++++---- .../service/spi/ServiceException.java | 1 + 14 files changed, 101 insertions(+), 86 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/TypeBeanInstanceProducer.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/TypeBeanInstanceProducer.java index 2fd7fed6b31b..54e55b8f05f8 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/TypeBeanInstanceProducer.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/TypeBeanInstanceProducer.java @@ -7,7 +7,6 @@ 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; @@ -15,6 +14,8 @@ 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. * @@ -32,8 +33,8 @@ public TypeBeanInstanceProducer(ConfigurationService configurationService, Servi @Override public B produceBeanInstance(Class beanType) { - final Constructor bootstrapContextAwareConstructor = - ReflectHelper.getConstructorOrNull( beanType, TypeBootstrapContext.class ); + final Constructor bootstrapContextAwareConstructor = + getConstructorOrNull( beanType, TypeBootstrapContext.class ); if ( bootstrapContextAwareConstructor != null ) { try { return bootstrapContextAwareConstructor.newInstance( this ); @@ -43,7 +44,7 @@ public B produceBeanInstance(Class beanType) { } } else { - final Constructor constructor = ReflectHelper.getConstructorOrNull( beanType ); + final Constructor constructor = getConstructorOrNull( beanType ); if ( constructor != null ) { try { return constructor.newInstance(); diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java index 9157bb32c843..cf7a235e6edc 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/internal/GeneratorBinder.java @@ -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 Generator instantiateGeneratorAsBean( Annotation annotation, BeanContainer beanContainer, GeneratorCreationContext creationContext, - Class generatorClass, + Class generatorClass, MemberDetails memberDetails, Class annotationType) { return Helper.getBean( diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java index 89e8cc009b57..279393203aff 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/ConnectionProviderInitiator.java @@ -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() ); @@ -118,10 +118,21 @@ else if ( explicitSetting instanceof Class providerClass ) { return instantiateConnectionProvider( configurationValues, strategySelector, beanContainer ); } + private static Class connectionProviderClass(Class providerClass) { + if ( !ConnectionProvider.class.isAssignableFrom( providerClass ) ) { + throw new ConnectionProviderConfigurationException( "Class '" + providerClass.getName() + + "' does not implement 'ConnectionProvider'" ); + } + @SuppressWarnings("unchecked") + final Class connectionProviderClass = + (Class) providerClass; + return connectionProviderClass; + } + private ConnectionProvider instantiateNamedConnectionProvider( String providerName, StrategySelector strategySelector, BeanContainer beanContainer) { LOG.instantiatingExplicitConnectionProvider( providerName ); - final Class providerClass = + final Class providerClass = strategySelector.selectStrategyImplementor( ConnectionProvider.class, providerName ); try { return instantiateExplicitConnectionProvider( providerClass, beanContainer ); @@ -192,7 +203,8 @@ private Class getSingleRegisteredProvider(Strategy : null; } - private ConnectionProvider instantiateExplicitConnectionProvider(Class providerClass, BeanContainer beanContainer) { + private T instantiateExplicitConnectionProvider( + Class providerClass, BeanContainer beanContainer) { try { if ( beanContainer != null ) { return Helper.getBean( @@ -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 ); @@ -211,7 +223,7 @@ private ConnectionProvider instantiateExplicitConnectionProvider(Class provid ); } else { - return (ConnectionProvider) providerClass.getConstructor().newInstance(); + return providerClass.getConstructor().newInstance(); } } catch (Exception e) { diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerBuilder.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerBuilder.java index c608ccffc292..bd691a508149 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerBuilder.java @@ -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}. * @@ -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 ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerExtendedAccessImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerExtendedAccessImpl.java index 1495cd3ed4dd..99838d271e63 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerExtendedAccessImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerExtendedAccessImpl.java @@ -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; @@ -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 @@ -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 diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerImmediateAccessImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerImmediateAccessImpl.java index b15f4d4a1af3..3206c29e50ab 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerImmediateAccessImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerImmediateAccessImpl.java @@ -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; } @@ -36,7 +34,8 @@ protected ContainedBeanImplementor createBean( Class beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer) { - final ContainedBeanImplementor bean = lifecycleStrategy.createBean( beanType, fallbackProducer, this ); + final ContainedBeanImplementor bean = + lifecycleStrategy.createBean( beanType, fallbackProducer, this ); bean.initialize(); return bean; } @@ -47,12 +46,8 @@ protected ContainedBeanImplementor createBean( Class beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer) { - final ContainedBeanImplementor bean = lifecycleStrategy.createBean( - name, - beanType, - fallbackProducer, - this - ); + final ContainedBeanImplementor bean = + lifecycleStrategy.createBean( name, beanType, fallbackProducer, this ); bean.initialize(); return bean; } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/ContainerManagedLifecycleStrategy.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/ContainerManagedLifecycleStrategy.java index b0203d39c68f..4474e8336a52 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/ContainerManagedLifecycleStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/ContainerManagedLifecycleStrategy.java @@ -199,7 +199,7 @@ protected Instance 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 ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/JpaCompliantLifecycleStrategy.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/JpaCompliantLifecycleStrategy.java index 729ead08c3eb..8c915f1663e0 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/JpaCompliantLifecycleStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/JpaCompliantLifecycleStrategy.java @@ -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) { diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractCdiBeanContainer.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractCdiBeanContainer.java index 3d7b63e320f1..9add740c33b5 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractCdiBeanContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractCdiBeanContainer.java @@ -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 */ @@ -34,7 +35,6 @@ public ContainedBean getBean( : createBean( beanType, lifecycleOptions, fallbackProducer ); } - @SuppressWarnings("unchecked") private ContainedBean getCacheableBean( Class beanType, LifecycleOptions lifecycleOptions, @@ -43,6 +43,7 @@ private ContainedBean getCacheableBean( final ContainedBeanImplementor existing = beanCache.get( beanCacheKey ); if ( existing != null ) { + //noinspection unchecked return (ContainedBeanImplementor) existing; } @@ -77,15 +78,11 @@ public ContainedBean getBean( Class 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 ContainedBeanImplementor getCacheableBean( String beanName, Class beanType, @@ -95,10 +92,12 @@ private ContainedBeanImplementor getCacheableBean( final ContainedBeanImplementor existing = beanCache.get( beanCacheKey ); if ( existing != null ) { + //noinspection unchecked return (ContainedBeanImplementor) existing; } - final ContainedBeanImplementor bean = createBean( beanName, beanType, lifecycleOptions, fallbackProducer ); + final ContainedBeanImplementor bean = + createBean( beanName, beanType, lifecycleOptions, fallbackProducer ); beanCache.put( beanCacheKey, bean ); return bean; } @@ -133,7 +132,7 @@ protected final void forEachBean(Consumer> consumer) @Override public final void stop() { - BeansMessageLogger.BEANS_MSG_LOGGER.stoppingBeanContainer( this ); + BEANS_MSG_LOGGER.stoppingBeanContainer( this ); forEachBean( ContainedBeanImplementor::release ); registeredBeans.clear(); beanCache.clear(); diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/BeansMessageLogger.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/BeansMessageLogger.java index ecb81ab22c9c..b9a389c0e13f 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/BeansMessageLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/BeansMessageLogger.java @@ -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 @@ -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(); @@ -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(); } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/FallbackBeanInstanceProducer.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/FallbackBeanInstanceProducer.java index e325fb787d5f..92dd4d709460 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/FallbackBeanInstanceProducer.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/FallbackBeanInstanceProducer.java @@ -36,7 +36,7 @@ private FallbackBeanInstanceProducer() { public B produceBeanInstance(Class beanType) { log.tracef( "Creating ManagedBean(%s) using direct instantiation", beanType.getName() ); try { - Constructor constructor = beanType.getDeclaredConstructor(); + final Constructor constructor = beanType.getDeclaredConstructor(); constructor.setAccessible( true ); return constructor.newInstance(); } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java index 058546683c2e..80949a302651 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java @@ -6,10 +6,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; import org.hibernate.engine.config.spi.ConfigurationService; -import org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy; -import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy; import org.hibernate.resource.beans.container.spi.BeanContainer; -import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy; +import org.hibernate.resource.beans.container.spi.ContainedBean; import org.hibernate.resource.beans.spi.BeanInstanceProducer; import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.service.ServiceRegistry; @@ -40,13 +38,6 @@ public static boolean allowExtensionsInCdi(ServiceRegistry serviceRegistry) { .getSetting( ALLOW_EXTENSIONS_IN_CDI, BOOLEAN, false ); } - @SuppressWarnings("unused") - public static BeanLifecycleStrategy getLifecycleStrategy(boolean shouldRegistryManageLifecycle) { - return shouldRegistryManageLifecycle - ? JpaCompliantLifecycleStrategy.INSTANCE - : ContainerManagedLifecycleStrategy.INSTANCE; - } - @Nullable public static BeanContainer getBeanContainer(ServiceRegistry serviceRegistry) { return allowExtensionsInCdi( serviceRegistry ) @@ -54,18 +45,25 @@ public static BeanContainer getBeanContainer(ServiceRegistry serviceRegistry) { : null; } - @SuppressWarnings( "unchecked" ) @Nullable public static T getBean( - @Nullable BeanContainer beanContainer, - Class beanType, + @Nullable BeanContainer container, + Class beanType, boolean canUseCachedReferences, boolean useJpaCompliantCreation, @Nullable Supplier fallbackSupplier) { - if ( beanContainer == null ) { - return null; - } - return (T) beanContainer.getBean( + return container == null ? null + : containedBean( container, beanType, canUseCachedReferences, useJpaCompliantCreation, fallbackSupplier ) + .getBeanInstance(); + } + + private static ContainedBean containedBean( + BeanContainer container, + Class beanType, + boolean canUseCachedReferences, + boolean useJpaCompliantCreation, + Supplier fallbackSupplier) { + return container.getBean( beanType, new BeanContainer.LifecycleOptions() { @Override @@ -79,17 +77,19 @@ public boolean useJpaCompliantCreation() { } }, new BeanInstanceProducer() { - @Override + @Override @SuppressWarnings( "unchecked" ) public B produceBeanInstance(Class beanType) { - return fallbackSupplier != null ? (B) fallbackSupplier.get() : null; + return fallbackSupplier != null + ? (B) fallbackSupplier.get() + : null; } @Override public B produceBeanInstance(String name, Class beanType) { - throw new UnsupportedOperationException("The method shouldn't be called"); + throw new UnsupportedOperationException( "The method shouldn't be called" ); } } - ).getBeanInstance(); + ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistryInitiator.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistryInitiator.java index 732058c3639f..318552baab3a 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistryInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistryInitiator.java @@ -12,7 +12,6 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.boot.registry.selector.spi.StrategySelector; -import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.config.spi.ConfigurationService; import org.hibernate.resource.beans.container.internal.CdiBeanContainerBuilder; import org.hibernate.resource.beans.container.spi.BeanContainer; @@ -21,6 +20,9 @@ import org.hibernate.service.spi.ServiceException; import org.hibernate.service.spi.ServiceRegistryImplementor; +import static org.hibernate.cfg.ManagedBeanSettings.BEAN_CONTAINER; +import static org.hibernate.cfg.ManagedBeanSettings.CDI_BEAN_MANAGER; +import static org.hibernate.cfg.ManagedBeanSettings.JAKARTA_CDI_BEAN_MANAGER; import static org.hibernate.resource.beans.internal.BeansMessageLogger.BEANS_MSG_LOGGER; /** @@ -49,7 +51,7 @@ public ManagedBeanRegistry initiateService( private BeanContainer resolveBeanContainer(Map configurationValues, ServiceRegistry serviceRegistry) { // was a specific container explicitly specified? - final Object explicitBeanContainer = configurationValues.get( AvailableSettings.BEAN_CONTAINER ); + final Object explicitBeanContainer = configurationValues.get( BEAN_CONTAINER ); return explicitBeanContainer == null ? interpretImplicitBeanContainer( serviceRegistry ) : interpretExplicitBeanContainer( explicitBeanContainer, serviceRegistry ); @@ -63,21 +65,18 @@ private static BeanContainer interpretImplicitBeanContainer(ServiceRegistry serv case 1 -> beanContainers.iterator().next(); case 0 -> interpretImplicitCdiBeanContainer( serviceRegistry ); default -> throw new ServiceException( "Multiple BeanContainer service implementations found" - + " (set '" + AvailableSettings.BEAN_CONTAINER + "' explicitly)" ); + + " (set '" + BEAN_CONTAINER + "' explicitly)" ); }; } // simplified CDI support private static BeanContainer interpretImplicitCdiBeanContainer(ServiceRegistry serviceRegistry) { - final Map settings = serviceRegistry.requireService( ConfigurationService.class ).getSettings(); - Object beanManager = settings.get( AvailableSettings.CDI_BEAN_MANAGER ); - if ( beanManager == null ) { - beanManager = settings.get( AvailableSettings.JAKARTA_CDI_BEAN_MANAGER ); - } + final Object beanManager = getConfiguredBeanManager( serviceRegistry ); final boolean isCdiAvailable = isCdiAvailable( serviceRegistry ); if ( beanManager != null ) { if ( !isCdiAvailable ) { - BEANS_MSG_LOGGER.beanManagerButCdiNotAvailable( beanManager ); + throw new ServiceException( "A bean manager was provided via '" + JAKARTA_CDI_BEAN_MANAGER + + "' but CDI is not available on the Hibernate class loader"); } return CdiBeanContainerBuilder.fromBeanManagerReference( beanManager, serviceRegistry ); } @@ -89,6 +88,12 @@ private static BeanContainer interpretImplicitCdiBeanContainer(ServiceRegistry s } } + private static Object getConfiguredBeanManager(ServiceRegistry serviceRegistry) { + final Map settings = serviceRegistry.requireService( ConfigurationService.class ).getSettings(); + final Object beanManager = settings.get( JAKARTA_CDI_BEAN_MANAGER ); + return beanManager != null ? beanManager : settings.get( CDI_BEAN_MANAGER ); + } + private BeanContainer interpretExplicitBeanContainer(Object explicitSetting, ServiceRegistry serviceRegistry) { if ( explicitSetting == null ) { return null; diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceException.java b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceException.java index 72249cfc4b83..2d342c3ca799 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceException.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceException.java @@ -3,6 +3,7 @@ * Copyright Red Hat Inc. and Hibernate Authors */ package org.hibernate.service.spi; + import org.hibernate.HibernateException; /**