From 2f7052c0ce5114e492c7dc4e4b8abd5283b2fe13 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Fri, 22 Nov 2024 08:30:35 +0800 Subject: [PATCH] HHH-18737 Introduce util methods for get bean from BeanContainer (#9100) --- .../SessionFactoryOptionsBuilder.java | 40 ++------- .../boot/model/internal/GeneratorBinder.java | 81 +++++-------------- .../internal/ConnectionProviderInitiator.java | 68 +++++----------- ...ultiTenantConnectionProviderInitiator.java | 41 ++-------- .../resource/beans/internal/Helper.java | 46 +++++++++++ 5 files changed, 104 insertions(+), 172 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java index 17a7c976b9..504eeab575 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java @@ -63,10 +63,7 @@ import org.hibernate.query.sqm.function.SqmFunctionRegistry; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy; import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy; import org.hibernate.query.sqm.sql.SqmTranslatorFactory; -import org.hibernate.resource.beans.container.spi.BeanContainer; import org.hibernate.resource.beans.internal.Helper; -import org.hibernate.resource.beans.spi.BeanInstanceProducer; -import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode; import org.hibernate.resource.jdbc.spi.StatementInspector; import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder; @@ -378,36 +375,13 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions { configurationSettings.get( MULTI_TENANT_IDENTIFIER_RESOLVER ) ); if ( this.currentTenantIdentifierResolver == null ) { - final BeanContainer beanContainer = Helper.allowExtensionsInCdi( serviceRegistry ) ? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer() : null; - if (beanContainer != null) { - this.currentTenantIdentifierResolver = beanContainer.getBean( - CurrentTenantIdentifierResolver.class, - new BeanContainer.LifecycleOptions() { - @Override - public boolean canUseCachedReferences() { - return true; - } - - @Override - public boolean useJpaCompliantCreation() { - return false; - } - }, - new BeanInstanceProducer() { - - @Override - public B produceBeanInstance(Class beanType) { - return null; - } - - @Override - public B produceBeanInstance(String name, Class beanType) { - return null; - } - - } - ).getBeanInstance(); - } + this.currentTenantIdentifierResolver = Helper.getBean( + Helper.getBeanContainer( serviceRegistry ), + CurrentTenantIdentifierResolver.class, + true, + false, + null + ); } this.delayBatchFetchLoaderCreations = configurationService.getSetting( DELAY_ENTITY_LOADER_CREATIONS, BOOLEAN, true ); 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 b16c62d846..29077a4b9c 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 @@ -51,9 +51,7 @@ import org.hibernate.models.spi.AnnotationTarget; import org.hibernate.models.spi.MemberDetails; import org.hibernate.models.spi.SourceModelBuildingContext; import org.hibernate.resource.beans.container.spi.BeanContainer; -import org.hibernate.resource.beans.spi.BeanInstanceProducer; -import org.hibernate.resource.beans.spi.ManagedBeanRegistry; -import org.hibernate.service.ServiceRegistry; +import org.hibernate.resource.beans.internal.Helper; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -498,35 +496,19 @@ public class GeneratorBinder { Class generatorClass, MemberDetails memberDetails, Class annotationType) { - return beanContainer.getBean( generatorClass, - new BeanContainer.LifecycleOptions() { - @Override - public boolean canUseCachedReferences() { - return false; - } - @Override - public boolean useJpaCompliantCreation() { - return true; - } - }, - new BeanInstanceProducer() { - @SuppressWarnings( "unchecked" ) - @Override - public B produceBeanInstance(Class beanType) { - return (B) instantiateGenerator( - annotation, - memberDetails, - annotationType, - creationContext, - generatorClass - ); - } - @Override - public B produceBeanInstance(String name, Class beanType) { - return produceBeanInstance( beanType ); - } - } ) - .getBeanInstance(); + return Helper.getBean( + beanContainer, + generatorClass, + false, + true, + () -> instantiateGenerator( + annotation, + memberDetails, + annotationType, + creationContext, + generatorClass + ) + ); } /** @@ -539,29 +521,13 @@ public class GeneratorBinder { private static T instantiateGeneratorAsBean( BeanContainer beanContainer, Class generatorClass) { - return beanContainer.getBean( generatorClass, - new BeanContainer.LifecycleOptions() { - @Override - public boolean canUseCachedReferences() { - return false; - } - @Override - public boolean useJpaCompliantCreation() { - return true; - } - }, - new BeanInstanceProducer() { - @SuppressWarnings( "unchecked" ) - @Override - public B produceBeanInstance(Class beanType) { - return (B) instantiateGeneratorViaDefaultConstructor( generatorClass ); - } - @Override - public B produceBeanInstance(String name, Class beanType) { - return produceBeanInstance( beanType ); - } - } ) - .getBeanInstance(); + return Helper.getBean( + beanContainer, + generatorClass, + false, + true, + () -> instantiateGeneratorViaDefaultConstructor( generatorClass ) + ); } /** @@ -819,10 +785,7 @@ public class GeneratorBinder { * Obtain a {@link BeanContainer} to be used for instantiating generators. */ public static BeanContainer beanContainer(MetadataBuildingContext buildingContext) { - final ServiceRegistry serviceRegistry = buildingContext.getBootstrapContext().getServiceRegistry(); - return allowExtensionsInCdi( serviceRegistry ) - ? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer() - : null; + return Helper.getBeanContainer( buildingContext.getBootstrapContext().getServiceRegistry() ); } /** 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 c2599a5b89..bc49ef6147 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 @@ -21,10 +21,7 @@ import org.hibernate.internal.CoreLogging; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.StringHelper; import org.hibernate.resource.beans.container.spi.BeanContainer; -import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer; import org.hibernate.resource.beans.internal.Helper; -import org.hibernate.resource.beans.spi.BeanInstanceProducer; -import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.service.spi.ServiceRegistryImplementor; import static java.sql.Connection.TRANSACTION_NONE; @@ -108,7 +105,7 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator B produceBeanInstance(Class beanType) { - return (B) noAppropriateConnectionProvider(); - } - - @Override - public B produceBeanInstance(String name, Class beanType) { - return (B) noAppropriateConnectionProvider(); - } - - } - ).getBeanInstance(); + return Helper.getBean( + beanContainer, + ConnectionProvider.class, + true, + true, + this::noAppropriateConnectionProvider + ); } else { return noAppropriateConnectionProvider(); @@ -236,21 +213,20 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator providerClass, BeanContainer beanContainer) { try { if ( beanContainer != null ) { - return (ConnectionProvider) beanContainer.getBean( - providerClass, - new BeanContainer.LifecycleOptions() { - @Override - public boolean canUseCachedReferences() { - return true; - } - - @Override - public boolean useJpaCompliantCreation() { - return true; - } - }, - FallbackBeanInstanceProducer.INSTANCE - ).getBeanInstance(); + return Helper.getBean( + beanContainer, + providerClass, + true, + true, + () -> { + try { + return (ConnectionProvider) providerClass.getConstructor().newInstance(); + } + catch (Exception e) { + throw new HibernateException( "Could not instantiate connection provider [" + providerClass.getName() + "]", e ); + } + } + ); } else { return (ConnectionProvider) providerClass.getConstructor().newInstance(); diff --git a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/MultiTenantConnectionProviderInitiator.java b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/MultiTenantConnectionProviderInitiator.java index 57179a5e8d..ee1763decb 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/MultiTenantConnectionProviderInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/jdbc/connections/internal/MultiTenantConnectionProviderInitiator.java @@ -12,10 +12,7 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoadingException; import org.hibernate.cfg.AvailableSettings; import org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl; import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider; -import org.hibernate.resource.beans.container.spi.BeanContainer; import org.hibernate.resource.beans.internal.Helper; -import org.hibernate.resource.beans.spi.BeanInstanceProducer; -import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.service.spi.ServiceException; import org.hibernate.service.spi.ServiceRegistryImplementor; @@ -43,37 +40,13 @@ public class MultiTenantConnectionProviderInitiator implements StandardServiceIn @Override public MultiTenantConnectionProvider initiateService(Map configurationValues, ServiceRegistryImplementor registry) { if ( !configurationValues.containsKey( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER ) ) { - final BeanContainer beanContainer = Helper.allowExtensionsInCdi( registry ) ? registry.requireService( ManagedBeanRegistry.class ).getBeanContainer() : null; - if (beanContainer != null) { - return beanContainer.getBean( - MultiTenantConnectionProvider.class, - new BeanContainer.LifecycleOptions() { - @Override - public boolean canUseCachedReferences() { - return true; - } - - @Override - public boolean useJpaCompliantCreation() { - return true; - } - }, - new BeanInstanceProducer() { - - @Override - public B produceBeanInstance(Class beanType) { - return null; - } - - @Override - public B produceBeanInstance(String name, Class beanType) { - return null; - } - - } - ).getBeanInstance(); - } - return null; + return Helper.getBean( + Helper.getBeanContainer( registry ), + MultiTenantConnectionProvider.class, + true, + true, + null + ); } final Object configValue = configurationValues.get( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER ); 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 0ad1b0bd73..486109ddd9 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 @@ -4,12 +4,18 @@ */ package org.hibernate.resource.beans.internal; +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.spi.BeanInstanceProducer; +import org.hibernate.resource.beans.spi.ManagedBeanRegistry; import org.hibernate.service.ServiceRegistry; +import java.util.function.Supplier; + import static org.hibernate.cfg.ManagedBeanSettings.ALLOW_EXTENSIONS_IN_CDI; import static org.hibernate.engine.config.spi.StandardConverters.BOOLEAN; @@ -40,4 +46,44 @@ public final class Helper { ? JpaCompliantLifecycleStrategy.INSTANCE : ContainerManagedLifecycleStrategy.INSTANCE; } + + @Nullable + public static BeanContainer getBeanContainer(ServiceRegistry serviceRegistry) { + return allowExtensionsInCdi( serviceRegistry ) ? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer() : null; + } + + @SuppressWarnings( "unchecked" ) + @Nullable + public static T getBean(@Nullable BeanContainer beanContainer, Class beanType, boolean canUseCachedReferences, boolean useJpaCompliantCreation, @Nullable Supplier fallbackSupplier) { + if ( beanContainer == null ) { + return null; + } + return (T) beanContainer.getBean( + beanType, + new BeanContainer.LifecycleOptions() { + @Override + public boolean canUseCachedReferences() { + return canUseCachedReferences; + } + + @Override + public boolean useJpaCompliantCreation() { + return useJpaCompliantCreation; + } + }, + new BeanInstanceProducer() { + + @Override + public B produceBeanInstance(Class beanType) { + return (B) (fallbackSupplier != null ? fallbackSupplier.get() : null); + } + + @Override + public B produceBeanInstance(String name, Class beanType) { + throw new UnsupportedOperationException("The method shouldn't be called"); + } + } + ).getBeanInstance(); + } + }