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.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> B produceBeanInstance(Class<B> beanType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> B produceBeanInstance(String name, Class<B> 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 );
|
||||
|
|
|
@ -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<? extends Generator> generatorClass,
|
||||
MemberDetails memberDetails,
|
||||
Class<? extends Annotation> 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> 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();
|
||||
return Helper.getBean(
|
||||
beanContainer,
|
||||
generatorClass,
|
||||
false,
|
||||
true,
|
||||
() -> instantiateGenerator(
|
||||
annotation,
|
||||
memberDetails,
|
||||
annotationType,
|
||||
creationContext,
|
||||
generatorClass
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -539,29 +521,13 @@ public class GeneratorBinder {
|
|||
private static <T extends Generator> T instantiateGeneratorAsBean(
|
||||
BeanContainer beanContainer,
|
||||
Class<T> 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> B produceBeanInstance(Class<B> beanType) {
|
||||
return (B) instantiateGeneratorViaDefaultConstructor( generatorClass );
|
||||
}
|
||||
@Override
|
||||
public <B> B produceBeanInstance(String name, Class<B> 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() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<Con
|
|||
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 Object explicitSetting = configurationValues.get( CONNECTION_PROVIDER );
|
||||
if ( explicitSetting != null ) {
|
||||
|
@ -185,33 +182,13 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator<Con
|
|||
}
|
||||
else {
|
||||
if (beanContainer != null) {
|
||||
return beanContainer.getBean(
|
||||
ConnectionProvider.class,
|
||||
new BeanContainer.LifecycleOptions() {
|
||||
@Override
|
||||
public boolean canUseCachedReferences() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@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();
|
||||
return Helper.getBean(
|
||||
beanContainer,
|
||||
ConnectionProvider.class,
|
||||
true,
|
||||
true,
|
||||
this::noAppropriateConnectionProvider
|
||||
);
|
||||
}
|
||||
else {
|
||||
return noAppropriateConnectionProvider();
|
||||
|
@ -236,21 +213,20 @@ public class ConnectionProviderInitiator implements StandardServiceInitiator<Con
|
|||
private ConnectionProvider instantiateExplicitConnectionProvider(Class<?> 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();
|
||||
|
|
|
@ -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<String, Object> 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> B produceBeanInstance(Class<B> beanType) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> B produceBeanInstance(String name, Class<B> 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 );
|
||||
|
|
|
@ -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> 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