HHH-18737 Introduce util methods for get bean from BeanContainer (#9100)
This commit is contained in:
parent
e4cba2c8d8
commit
2f7052c0ce
|
@ -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.SqmMultiTableInsertStrategy;
|
||||||
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
|
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
|
||||||
import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
|
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.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.PhysicalConnectionHandlingMode;
|
||||||
import org.hibernate.resource.jdbc.spi.StatementInspector;
|
import org.hibernate.resource.jdbc.spi.StatementInspector;
|
||||||
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
|
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
|
||||||
|
@ -378,36 +375,13 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
|
||||||
configurationSettings.get( MULTI_TENANT_IDENTIFIER_RESOLVER )
|
configurationSettings.get( MULTI_TENANT_IDENTIFIER_RESOLVER )
|
||||||
);
|
);
|
||||||
if ( this.currentTenantIdentifierResolver == null ) {
|
if ( this.currentTenantIdentifierResolver == null ) {
|
||||||
final BeanContainer beanContainer = Helper.allowExtensionsInCdi( serviceRegistry ) ? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer() : null;
|
this.currentTenantIdentifierResolver = Helper.getBean(
|
||||||
if (beanContainer != null) {
|
Helper.getBeanContainer( serviceRegistry ),
|
||||||
this.currentTenantIdentifierResolver = beanContainer.getBean(
|
CurrentTenantIdentifierResolver.class,
|
||||||
CurrentTenantIdentifierResolver.class,
|
true,
|
||||||
new BeanContainer.LifecycleOptions() {
|
false,
|
||||||
@Override
|
null
|
||||||
public boolean canUseCachedReferences() {
|
);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean useJpaCompliantCreation() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
new BeanInstanceProducer() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(Class<B> beanType) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(String name, Class<B> beanType) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
).getBeanInstance();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.delayBatchFetchLoaderCreations = configurationService.getSetting( DELAY_ENTITY_LOADER_CREATIONS, BOOLEAN, true );
|
this.delayBatchFetchLoaderCreations = configurationService.getSetting( DELAY_ENTITY_LOADER_CREATIONS, BOOLEAN, true );
|
||||||
|
|
|
@ -51,9 +51,7 @@ import org.hibernate.models.spi.AnnotationTarget;
|
||||||
import org.hibernate.models.spi.MemberDetails;
|
import org.hibernate.models.spi.MemberDetails;
|
||||||
import org.hibernate.models.spi.SourceModelBuildingContext;
|
import org.hibernate.models.spi.SourceModelBuildingContext;
|
||||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
import org.hibernate.resource.beans.internal.Helper;
|
||||||
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
|
|
||||||
import org.hibernate.service.ServiceRegistry;
|
|
||||||
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.GeneratedValue;
|
||||||
import jakarta.persistence.GenerationType;
|
import jakarta.persistence.GenerationType;
|
||||||
|
@ -498,35 +496,19 @@ public class GeneratorBinder {
|
||||||
Class<? extends Generator> generatorClass,
|
Class<? extends Generator> generatorClass,
|
||||||
MemberDetails memberDetails,
|
MemberDetails memberDetails,
|
||||||
Class<? extends Annotation> annotationType) {
|
Class<? extends Annotation> annotationType) {
|
||||||
return beanContainer.getBean( generatorClass,
|
return Helper.getBean(
|
||||||
new BeanContainer.LifecycleOptions() {
|
beanContainer,
|
||||||
@Override
|
generatorClass,
|
||||||
public boolean canUseCachedReferences() {
|
false,
|
||||||
return false;
|
true,
|
||||||
}
|
() -> instantiateGenerator(
|
||||||
@Override
|
annotation,
|
||||||
public boolean useJpaCompliantCreation() {
|
memberDetails,
|
||||||
return true;
|
annotationType,
|
||||||
}
|
creationContext,
|
||||||
},
|
generatorClass
|
||||||
new BeanInstanceProducer() {
|
)
|
||||||
@SuppressWarnings( "unchecked" )
|
);
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(Class<B> beanType) {
|
|
||||||
return (B) instantiateGenerator(
|
|
||||||
annotation,
|
|
||||||
memberDetails,
|
|
||||||
annotationType,
|
|
||||||
creationContext,
|
|
||||||
generatorClass
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(String name, Class<B> beanType) {
|
|
||||||
return produceBeanInstance( beanType );
|
|
||||||
}
|
|
||||||
} )
|
|
||||||
.getBeanInstance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -539,29 +521,13 @@ public class GeneratorBinder {
|
||||||
private static <T extends Generator> T instantiateGeneratorAsBean(
|
private static <T extends Generator> T instantiateGeneratorAsBean(
|
||||||
BeanContainer beanContainer,
|
BeanContainer beanContainer,
|
||||||
Class<T> generatorClass) {
|
Class<T> generatorClass) {
|
||||||
return beanContainer.getBean( generatorClass,
|
return Helper.getBean(
|
||||||
new BeanContainer.LifecycleOptions() {
|
beanContainer,
|
||||||
@Override
|
generatorClass,
|
||||||
public boolean canUseCachedReferences() {
|
false,
|
||||||
return false;
|
true,
|
||||||
}
|
() -> instantiateGeneratorViaDefaultConstructor( generatorClass )
|
||||||
@Override
|
);
|
||||||
public boolean useJpaCompliantCreation() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
new BeanInstanceProducer() {
|
|
||||||
@SuppressWarnings( "unchecked" )
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(Class<B> beanType) {
|
|
||||||
return (B) instantiateGeneratorViaDefaultConstructor( generatorClass );
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(String name, Class<B> beanType) {
|
|
||||||
return produceBeanInstance( beanType );
|
|
||||||
}
|
|
||||||
} )
|
|
||||||
.getBeanInstance();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -819,10 +785,7 @@ public class GeneratorBinder {
|
||||||
* Obtain a {@link BeanContainer} to be used for instantiating generators.
|
* Obtain a {@link BeanContainer} to be used for instantiating generators.
|
||||||
*/
|
*/
|
||||||
public static BeanContainer beanContainer(MetadataBuildingContext buildingContext) {
|
public static BeanContainer beanContainer(MetadataBuildingContext buildingContext) {
|
||||||
final ServiceRegistry serviceRegistry = buildingContext.getBootstrapContext().getServiceRegistry();
|
return Helper.getBeanContainer( buildingContext.getBootstrapContext().getServiceRegistry() );
|
||||||
return allowExtensionsInCdi( serviceRegistry )
|
|
||||||
? serviceRegistry.requireService( ManagedBeanRegistry.class ).getBeanContainer()
|
|
||||||
: null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -21,10 +21,7 @@ import org.hibernate.internal.CoreLogging;
|
||||||
import org.hibernate.internal.CoreMessageLogger;
|
import org.hibernate.internal.CoreMessageLogger;
|
||||||
import org.hibernate.internal.util.StringHelper;
|
import org.hibernate.internal.util.StringHelper;
|
||||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
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.internal.Helper;
|
||||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
|
||||||
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
|
|
||||||
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
||||||
|
|
||||||
import static java.sql.Connection.TRANSACTION_NONE;
|
import static java.sql.Connection.TRANSACTION_NONE;
|
||||||
|
@ -108,7 +105,7 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator<Con
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final BeanContainer beanContainer = Helper.allowExtensionsInCdi( registry ) ? registry.requireService( ManagedBeanRegistry.class ).getBeanContainer() : null;
|
final BeanContainer beanContainer = Helper.getBeanContainer( registry );
|
||||||
final StrategySelector strategySelector = registry.requireService( StrategySelector.class );
|
final StrategySelector strategySelector = registry.requireService( StrategySelector.class );
|
||||||
final Object explicitSetting = configurationValues.get( CONNECTION_PROVIDER );
|
final Object explicitSetting = configurationValues.get( CONNECTION_PROVIDER );
|
||||||
if ( explicitSetting != null ) {
|
if ( explicitSetting != null ) {
|
||||||
|
@ -185,33 +182,13 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator<Con
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (beanContainer != null) {
|
if (beanContainer != null) {
|
||||||
return beanContainer.getBean(
|
return Helper.getBean(
|
||||||
ConnectionProvider.class,
|
beanContainer,
|
||||||
new BeanContainer.LifecycleOptions() {
|
ConnectionProvider.class,
|
||||||
@Override
|
true,
|
||||||
public boolean canUseCachedReferences() {
|
true,
|
||||||
return true;
|
this::noAppropriateConnectionProvider
|
||||||
}
|
);
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean useJpaCompliantCreation() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
new BeanInstanceProducer() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(Class<B> beanType) {
|
|
||||||
return (B) noAppropriateConnectionProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(String name, Class<B> beanType) {
|
|
||||||
return (B) noAppropriateConnectionProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
).getBeanInstance();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return noAppropriateConnectionProvider();
|
return noAppropriateConnectionProvider();
|
||||||
|
@ -236,21 +213,20 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator<Con
|
||||||
private ConnectionProvider instantiateExplicitConnectionProvider(Class<?> providerClass, BeanContainer beanContainer) {
|
private ConnectionProvider instantiateExplicitConnectionProvider(Class<?> providerClass, BeanContainer beanContainer) {
|
||||||
try {
|
try {
|
||||||
if ( beanContainer != null ) {
|
if ( beanContainer != null ) {
|
||||||
return (ConnectionProvider) beanContainer.getBean(
|
return Helper.getBean(
|
||||||
providerClass,
|
beanContainer,
|
||||||
new BeanContainer.LifecycleOptions() {
|
providerClass,
|
||||||
@Override
|
true,
|
||||||
public boolean canUseCachedReferences() {
|
true,
|
||||||
return true;
|
() -> {
|
||||||
}
|
try {
|
||||||
|
return (ConnectionProvider) providerClass.getConstructor().newInstance();
|
||||||
@Override
|
}
|
||||||
public boolean useJpaCompliantCreation() {
|
catch (Exception e) {
|
||||||
return true;
|
throw new HibernateException( "Could not instantiate connection provider [" + providerClass.getName() + "]", e );
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
FallbackBeanInstanceProducer.INSTANCE
|
);
|
||||||
).getBeanInstance();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return (ConnectionProvider) providerClass.getConstructor().newInstance();
|
return (ConnectionProvider) providerClass.getConstructor().newInstance();
|
||||||
|
|
|
@ -12,10 +12,7 @@ import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl;
|
import org.hibernate.engine.jdbc.connections.spi.DataSourceBasedMultiTenantConnectionProviderImpl;
|
||||||
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
|
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.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.ServiceException;
|
||||||
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
import org.hibernate.service.spi.ServiceRegistryImplementor;
|
||||||
|
|
||||||
|
@ -43,37 +40,13 @@ public class MultiTenantConnectionProviderInitiator implements StandardServiceIn
|
||||||
@Override
|
@Override
|
||||||
public MultiTenantConnectionProvider<?> initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry) {
|
public MultiTenantConnectionProvider<?> initiateService(Map<String, Object> configurationValues, ServiceRegistryImplementor registry) {
|
||||||
if ( !configurationValues.containsKey( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER ) ) {
|
if ( !configurationValues.containsKey( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER ) ) {
|
||||||
final BeanContainer beanContainer = Helper.allowExtensionsInCdi( registry ) ? registry.requireService( ManagedBeanRegistry.class ).getBeanContainer() : null;
|
return Helper.getBean(
|
||||||
if (beanContainer != null) {
|
Helper.getBeanContainer( registry ),
|
||||||
return beanContainer.getBean(
|
MultiTenantConnectionProvider.class,
|
||||||
MultiTenantConnectionProvider.class,
|
true,
|
||||||
new BeanContainer.LifecycleOptions() {
|
true,
|
||||||
@Override
|
null
|
||||||
public boolean canUseCachedReferences() {
|
);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean useJpaCompliantCreation() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
new BeanInstanceProducer() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(Class<B> beanType) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <B> B produceBeanInstance(String name, Class<B> beanType) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
).getBeanInstance();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Object configValue = configurationValues.get( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER );
|
final Object configValue = configurationValues.get( AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER );
|
||||||
|
|
|
@ -4,12 +4,18 @@
|
||||||
*/
|
*/
|
||||||
package org.hibernate.resource.beans.internal;
|
package org.hibernate.resource.beans.internal;
|
||||||
|
|
||||||
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
import org.hibernate.engine.config.spi.ConfigurationService;
|
import org.hibernate.engine.config.spi.ConfigurationService;
|
||||||
import org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy;
|
import org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy;
|
||||||
import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy;
|
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.BeanLifecycleStrategy;
|
||||||
|
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||||
|
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
|
||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
import static org.hibernate.cfg.ManagedBeanSettings.ALLOW_EXTENSIONS_IN_CDI;
|
import static org.hibernate.cfg.ManagedBeanSettings.ALLOW_EXTENSIONS_IN_CDI;
|
||||||
import static org.hibernate.engine.config.spi.StandardConverters.BOOLEAN;
|
import static org.hibernate.engine.config.spi.StandardConverters.BOOLEAN;
|
||||||
|
|
||||||
|
@ -40,4 +46,44 @@ public final class Helper {
|
||||||
? JpaCompliantLifecycleStrategy.INSTANCE
|
? JpaCompliantLifecycleStrategy.INSTANCE
|
||||||
: ContainerManagedLifecycleStrategy.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> T getBean(@Nullable BeanContainer beanContainer, Class<?> beanType, boolean canUseCachedReferences, boolean useJpaCompliantCreation, @Nullable Supplier<T> 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> B produceBeanInstance(Class<B> beanType) {
|
||||||
|
return (B) (fallbackSupplier != null ? fallbackSupplier.get() : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <B> B produceBeanInstance(String name, Class<B> beanType) {
|
||||||
|
throw new UnsupportedOperationException("The method shouldn't be called");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
).getBeanInstance();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue