HHH-12133 - Create ManagedBeanRegistry and ManagedBean
- addressed concerns discussed with Yoann
This commit is contained in:
parent
521ee44f8e
commit
f1263f8c7c
|
@ -8,11 +8,11 @@ package org.hibernate.resource.beans.container.internal;
|
|||
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainerImplementor;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface CdiBasedBeanContainer extends BeanContainerImplementor {
|
||||
public interface CdiBasedBeanContainer extends BeanContainer {
|
||||
BeanManager getUsableBeanManager();
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ import org.hibernate.engine.config.spi.ConfigurationService;
|
|||
import org.hibernate.engine.config.spi.StandardConverters;
|
||||
import org.hibernate.internal.util.ReflectHelper;
|
||||
import org.hibernate.jpa.AvailableSettings;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainerImplementor;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.spi.ManagedBeanRegistryInitiator;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
|
@ -38,14 +38,14 @@ public class CdiBeanContainerBuilder {
|
|||
private static final String BEAN_MANAGER_EXTENSION_FQN = "org.hibernate.jpa.event.spi.jpa.ExtendedBeanManager";
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static BeanContainerImplementor fromBeanManagerReference(
|
||||
public static BeanContainer fromBeanManagerReference(
|
||||
Object beanManagerRef,
|
||||
ServiceRegistry serviceRegistry) {
|
||||
final ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class );
|
||||
final Class beanManagerClass = ManagedBeanRegistryInitiator.cdiBeanManagerClass( classLoaderService );
|
||||
final Class extendedBeanManagerClass = getHibernateClass( BEAN_MANAGER_EXTENSION_FQN );
|
||||
|
||||
final Class<? extends BeanContainerImplementor> containerClass;
|
||||
final Class<? extends BeanContainer> containerClass;
|
||||
final Class ctorArgType;
|
||||
|
||||
if ( extendedBeanManagerClass.isInstance( beanManagerRef ) ) {
|
||||
|
@ -66,7 +66,7 @@ public class CdiBeanContainerBuilder {
|
|||
}
|
||||
|
||||
try {
|
||||
final Constructor<? extends BeanContainerImplementor> ctor = containerClass.getDeclaredConstructor( ctorArgType );
|
||||
final Constructor<? extends BeanContainer> ctor = containerClass.getDeclaredConstructor( ctorArgType );
|
||||
try {
|
||||
ReflectHelper.ensureAccessibility( ctor );
|
||||
return ctor.newInstance( ctorArgType.cast( beanManagerRef ) );
|
||||
|
|
|
@ -10,7 +10,6 @@ import javax.enterprise.inject.spi.BeanManager;
|
|||
|
||||
import org.hibernate.resource.beans.container.spi.AbstractBeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBeanImplementor;
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
|
||||
|
@ -31,7 +30,7 @@ public class CdiBeanContainerDelayedAccessImpl extends AbstractBeanContainer {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected <B> ContainedBean<B> createBean(
|
||||
protected <B> ContainedBeanImplementor<B> createBean(
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
|
@ -39,7 +38,7 @@ public class CdiBeanContainerDelayedAccessImpl extends AbstractBeanContainer {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected <B> ContainedBean<B> createBean(
|
||||
protected <B> ContainedBeanImplementor<B> createBean(
|
||||
String name,
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
|
|
|
@ -13,7 +13,6 @@ import org.hibernate.resource.beans.container.spi.AbstractBeanContainer;
|
|||
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBeanImplementor;
|
||||
import org.hibernate.resource.beans.internal.Helper;
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
|
@ -38,14 +37,12 @@ public class CdiBeanContainerExtendedAccessImpl
|
|||
}
|
||||
|
||||
@Override
|
||||
protected <B> ContainedBean<B> createBean(
|
||||
protected <B> ContainedBeanImplementor<B> createBean(
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
if ( usableBeanManager == null ) {
|
||||
final BeanImpl<B> bean = new BeanImpl<>( beanType, lifecycleStrategy, fallbackProducer );
|
||||
registerContainedBean( beanType.getName(), bean );
|
||||
return bean;
|
||||
return new BeanImpl<>( beanType, lifecycleStrategy, fallbackProducer );
|
||||
}
|
||||
else {
|
||||
return lifecycleStrategy.createBean( beanType, fallbackProducer, this );
|
||||
|
@ -53,20 +50,18 @@ public class CdiBeanContainerExtendedAccessImpl
|
|||
}
|
||||
|
||||
@Override
|
||||
protected <B> ContainedBean<B> createBean(
|
||||
protected <B> ContainedBeanImplementor<B> createBean(
|
||||
String name,
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
if ( usableBeanManager == null ) {
|
||||
final NamedBeanImpl<B> bean = new NamedBeanImpl<>(
|
||||
return new NamedBeanImpl<>(
|
||||
name,
|
||||
beanType,
|
||||
lifecycleStrategy,
|
||||
fallbackProducer
|
||||
);
|
||||
registerContainedBean( Helper.INSTANCE.determineBeanRegistrationKey( name, beanType), bean );
|
||||
return bean;
|
||||
}
|
||||
else {
|
||||
return lifecycleStrategy.createBean( name, beanType, fallbackProducer, this );
|
||||
|
@ -187,26 +182,22 @@ public class CdiBeanContainerExtendedAccessImpl
|
|||
}
|
||||
|
||||
@Override
|
||||
public ContainedBeanImplementor findRegistered(String key) {
|
||||
return CdiBeanContainerExtendedAccessImpl.this.findRegistered( key );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerContainedBean(String key, ContainedBeanImplementor bean) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> ContainedBean<B> getBean(Class<B> beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer) {
|
||||
return CdiBeanContainerExtendedAccessImpl.this.getBean( beanType, lifecycleStrategy, fallbackProducer );
|
||||
public <B> ContainedBean<B> getBean(
|
||||
Class<B> beanType,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
// todo (5.3) : should this throw an exception instead?
|
||||
return CdiBeanContainerExtendedAccessImpl.this.getBean( beanType, lifecycleOptions, fallbackProducer );
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> ContainedBean<B> getBean(
|
||||
String name,
|
||||
String beanName,
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
return CdiBeanContainerExtendedAccessImpl.this.getBean( name, beanType, lifecycleStrategy, fallbackProducer );
|
||||
// todo (5.3) : should this throw an exception instead?
|
||||
return CdiBeanContainerExtendedAccessImpl.this.getBean( beanName, beanType, lifecycleOptions, fallbackProducer );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -10,7 +10,6 @@ import javax.enterprise.inject.spi.BeanManager;
|
|||
|
||||
import org.hibernate.resource.beans.container.spi.AbstractBeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBeanImplementor;
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
|
||||
|
@ -36,23 +35,17 @@ public class CdiBeanContainerImmediateAccessImpl extends AbstractBeanContainer {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected <B> ContainedBean<B> createBean(
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <B> ContainedBean<B> createBean(
|
||||
protected <B> ContainedBeanImplementor<B> createBean(
|
||||
String name,
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
|
@ -63,9 +56,7 @@ public class CdiBeanContainerImmediateAccessImpl extends AbstractBeanContainer {
|
|||
fallbackProducer,
|
||||
this
|
||||
);
|
||||
|
||||
bean.initialize();
|
||||
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ import javax.enterprise.context.ContextNotActiveException;
|
|||
import javax.enterprise.inject.Instance;
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainerImplementor;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBeanImplementor;
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
|
@ -36,27 +36,13 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
|
|||
// private constructor, do not use
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> ContainedBeanImplementor<B> findRegisteredBean(
|
||||
Class<B> beanClass,
|
||||
BeanContainerImplementor container) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> ContainedBeanImplementor<B> findRegisteredBean(
|
||||
String beanName,
|
||||
Class<B> beanClass,
|
||||
BeanContainerImplementor container) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> ContainedBeanImplementor<B> createBean(
|
||||
Class<B> beanClass,
|
||||
BeanInstanceProducer fallbackProducer,
|
||||
BeanContainerImplementor container) {
|
||||
return new BeanImpl<>( beanClass, fallbackProducer, ( (CdiBasedBeanContainer) container ).getUsableBeanManager() );
|
||||
BeanContainer beanContainer) {
|
||||
return new BeanImpl<>( beanClass, fallbackProducer, ( (CdiBasedBeanContainer) beanContainer ).getUsableBeanManager() );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,8 +50,8 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
|
|||
String beanName,
|
||||
Class<B> beanClass,
|
||||
BeanInstanceProducer fallbackProducer,
|
||||
BeanContainerImplementor container) {
|
||||
return new NamedBeanImpl<>( beanName, beanClass, fallbackProducer, ( (CdiBasedBeanContainer) container ).getUsableBeanManager() );
|
||||
BeanContainer beanContainer) {
|
||||
return new NamedBeanImpl<>( beanName, beanClass, fallbackProducer, ( (CdiBasedBeanContainer) beanContainer ).getUsableBeanManager() );
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ import javax.enterprise.inject.spi.Bean;
|
|||
import javax.enterprise.inject.spi.BeanManager;
|
||||
import javax.enterprise.inject.spi.InjectionTarget;
|
||||
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainerImplementor;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBeanImplementor;
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
|
@ -41,31 +41,16 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
|
|||
// private constructor, do not use
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <B> ContainedBeanImplementor<B> findRegisteredBean(
|
||||
Class<B> beanClass,
|
||||
BeanContainerImplementor container) {
|
||||
return container.findRegistered( beanClass.getName() );
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <B> ContainedBeanImplementor<B> findRegisteredBean(
|
||||
String beanName,
|
||||
Class<B> beanClass,
|
||||
BeanContainerImplementor container) {
|
||||
return container.findRegistered( beanClass.getName() + ':' + beanName );
|
||||
}
|
||||
|
||||
@Override
|
||||
public <B> ContainedBeanImplementor<B> createBean(
|
||||
Class<B> beanClass,
|
||||
BeanInstanceProducer fallbackProducer,
|
||||
BeanContainerImplementor container) {
|
||||
final BeanImpl<B> bean = new BeanImpl<>( beanClass, fallbackProducer, ( (CdiBasedBeanContainer) container ).getUsableBeanManager() );
|
||||
container.registerContainedBean( beanClass.getName(), bean );
|
||||
return bean;
|
||||
BeanContainer beanContainer) {
|
||||
return new BeanImpl<>(
|
||||
beanClass,
|
||||
fallbackProducer,
|
||||
( (CdiBasedBeanContainer) beanContainer ).getUsableBeanManager()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -73,12 +58,17 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
|
|||
String beanName,
|
||||
Class<B> beanClass,
|
||||
BeanInstanceProducer fallbackProducer,
|
||||
BeanContainerImplementor container) {
|
||||
final NamedBeanImpl<B> bean = new NamedBeanImpl<>( beanName, beanClass, fallbackProducer, ( (CdiBasedBeanContainer) container ).getUsableBeanManager() );
|
||||
container.registerContainedBean( beanClass.getName(), bean );
|
||||
return bean;
|
||||
BeanContainer beanContainer) {
|
||||
return new NamedBeanImpl<>(
|
||||
beanName,
|
||||
beanClass,
|
||||
fallbackProducer,
|
||||
( (CdiBasedBeanContainer) beanContainer ).getUsableBeanManager()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private static class BeanImpl<B> implements ContainedBeanImplementor<B> {
|
||||
private final Class<B> beanType;
|
||||
|
||||
|
|
|
@ -6,65 +6,128 @@
|
|||
*/
|
||||
package org.hibernate.resource.beans.container.spi;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public abstract class AbstractBeanContainer implements CdiBasedBeanContainer {
|
||||
private Map<String,ContainedBeanImplementor<?>> registrations = new HashMap<>();
|
||||
private Map<String,ContainedBeanImplementor<?>> beanCache = new HashMap<>();
|
||||
private List<ContainedBeanImplementor<?>> registeredBeans = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public final void registerContainedBean(String key, ContainedBeanImplementor bean) {
|
||||
registrations.put( key, bean );
|
||||
}
|
||||
|
||||
@Override
|
||||
public final ContainedBeanImplementor findRegistered(String key) {
|
||||
return registrations.get( key );
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public final <B> ContainedBean<B> getBean(
|
||||
public <B> ContainedBean<B> getBean(
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
final ContainedBean existing = lifecycleStrategy.findRegisteredBean( beanType, this );
|
||||
if ( lifecycleOptions.canUseCachedReferences() ) {
|
||||
return getCacheableBean( beanType, lifecycleOptions, fallbackProducer );
|
||||
}
|
||||
else {
|
||||
return createBean( beanType, lifecycleOptions, fallbackProducer );
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <B> ContainedBean<B> getCacheableBean(
|
||||
Class<B> beanType,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
final String beanCacheKey = Helper.INSTANCE.determineBeanCacheKey( beanType );
|
||||
|
||||
final ContainedBeanImplementor existing = beanCache.get( beanCacheKey );
|
||||
if ( existing != null ) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
return createBean( beanType, lifecycleStrategy, fallbackProducer );
|
||||
final ContainedBeanImplementor bean = createBean( beanType, lifecycleOptions, fallbackProducer );
|
||||
beanCache.put( beanCacheKey, bean );
|
||||
return bean;
|
||||
}
|
||||
|
||||
protected abstract <B> ContainedBean<B> createBean(
|
||||
@SuppressWarnings("unchecked")
|
||||
private <B> ContainedBeanImplementor<B> createBean(
|
||||
Class<B> beanType,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
final ContainedBeanImplementor bean = createBean(
|
||||
beanType,
|
||||
lifecycleOptions.useJpaCompliantCreation()
|
||||
? JpaCompliantLifecycleStrategy.INSTANCE
|
||||
: ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
fallbackProducer
|
||||
);
|
||||
registeredBeans.add( bean );
|
||||
return bean;
|
||||
}
|
||||
|
||||
protected abstract <B> ContainedBeanImplementor<B> createBean(
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
BeanInstanceProducer fallbackProducer);
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public final <B> ContainedBean<B> getBean(
|
||||
String name,
|
||||
public <B> ContainedBean<B> getBean(
|
||||
String beanName,
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
final ContainedBean existing = lifecycleStrategy.findRegisteredBean( name, beanType, this );
|
||||
if ( lifecycleOptions.canUseCachedReferences() ) {
|
||||
return getCacheableBean( beanName, beanType, lifecycleOptions, fallbackProducer );
|
||||
}
|
||||
else {
|
||||
return createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <B> ContainedBeanImplementor<B> getCacheableBean(
|
||||
String beanName,
|
||||
Class<B> beanType,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
final String beanCacheKey = Helper.INSTANCE.determineBeanCacheKey( beanName, beanType );
|
||||
|
||||
final ContainedBeanImplementor existing = beanCache.get( beanCacheKey );
|
||||
if ( existing != null ) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
return createBean( name, beanType, lifecycleStrategy, fallbackProducer );
|
||||
final ContainedBeanImplementor bean = createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
|
||||
beanCache.put( beanCacheKey, bean );
|
||||
return bean;
|
||||
}
|
||||
|
||||
protected abstract <B> ContainedBean<B> createBean(
|
||||
@SuppressWarnings("unchecked")
|
||||
private <B> ContainedBeanImplementor<B> createBean(
|
||||
String beanName,
|
||||
Class<B> beanType,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer) {
|
||||
final ContainedBeanImplementor bean = createBean(
|
||||
beanName,
|
||||
beanType,
|
||||
lifecycleOptions.useJpaCompliantCreation()
|
||||
? JpaCompliantLifecycleStrategy.INSTANCE
|
||||
: ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
fallbackProducer
|
||||
);
|
||||
registeredBeans.add( bean );
|
||||
return bean;
|
||||
}
|
||||
|
||||
protected abstract <B> ContainedBeanImplementor<B> createBean(
|
||||
String name,
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
|
@ -73,14 +136,15 @@ public abstract class AbstractBeanContainer implements CdiBasedBeanContainer {
|
|||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
protected final void forEachBean(Consumer<ContainedBeanImplementor<?>> consumer) {
|
||||
registrations.values().forEach( consumer );
|
||||
registeredBeans.forEach( consumer );
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void stop() {
|
||||
BeansMessageLogger.BEANS_LOGGER.stoppingBeanContainer( this );
|
||||
forEachBean( ContainedBeanImplementor::release );
|
||||
registrations.clear();
|
||||
registeredBeans.clear();
|
||||
beanCache.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,14 +15,19 @@ import org.hibernate.service.spi.Stoppable;
|
|||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface BeanContainer extends Stoppable {
|
||||
interface LifecycleOptions {
|
||||
boolean canUseCachedReferences();
|
||||
boolean useJpaCompliantCreation();
|
||||
}
|
||||
|
||||
<B> ContainedBean<B> getBean(
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer);
|
||||
|
||||
<B> ContainedBean<B> getBean(
|
||||
String name,
|
||||
Class<B> beanType,
|
||||
BeanLifecycleStrategy lifecycleStrategy,
|
||||
LifecycleOptions lifecycleOptions,
|
||||
BeanInstanceProducer fallbackProducer);
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.resource.beans.container.spi;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public interface BeanContainerImplementor extends BeanContainer {
|
||||
ContainedBeanImplementor findRegistered(String key);
|
||||
void registerContainedBean(String key, ContainedBeanImplementor bean);
|
||||
}
|
|
@ -6,27 +6,20 @@
|
|||
*/
|
||||
package org.hibernate.resource.beans.container.spi;
|
||||
|
||||
import javax.enterprise.inject.spi.BeanManager;
|
||||
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
|
||||
/**
|
||||
* Models how the lifecycle for a bean should be managed.
|
||||
*/
|
||||
public interface BeanLifecycleStrategy {
|
||||
|
||||
<B> ContainedBeanImplementor<B> findRegisteredBean(Class<B> beanClass, BeanContainerImplementor container);
|
||||
<B> ContainedBeanImplementor<B> findRegisteredBean(String beanName, Class<B> beanClass, BeanContainerImplementor container);
|
||||
|
||||
<B> ContainedBeanImplementor<B> createBean(
|
||||
Class<B> beanClass,
|
||||
BeanInstanceProducer fallbackProducer,
|
||||
BeanContainerImplementor container);
|
||||
BeanContainer beanContainer);
|
||||
|
||||
<B> ContainedBeanImplementor<B> createBean(
|
||||
String beanName,
|
||||
Class<B> beanClass,
|
||||
BeanInstanceProducer fallbackProducer,
|
||||
BeanContainerImplementor container);
|
||||
|
||||
BeanContainer beanContainer);
|
||||
}
|
||||
|
|
|
@ -22,11 +22,11 @@ public class Helper {
|
|||
private Helper() {
|
||||
}
|
||||
|
||||
public String determineBeanRegistrationKey(Class beanType) {
|
||||
public String determineBeanCacheKey(Class beanType) {
|
||||
return beanType.getName();
|
||||
}
|
||||
|
||||
public String determineBeanRegistrationKey(String name, Class beanType) {
|
||||
public String determineBeanCacheKey(String name, Class beanType) {
|
||||
return beanType.getName() + ':' + name;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,9 +8,7 @@ package org.hibernate.resource.beans.internal;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.container.spi.FallbackContainedBean;
|
||||
|
@ -23,7 +21,7 @@ import org.hibernate.service.spi.Stoppable;
|
|||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable {
|
||||
public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, BeanContainer.LifecycleOptions, Stoppable {
|
||||
private Map<String,ManagedBean<?>> registrations = new HashMap<>();
|
||||
|
||||
private final BeanContainer beanContainer;
|
||||
|
@ -37,6 +35,16 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable {
|
|||
return beanContainer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUseCachedReferences() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean useJpaCompliantCreation() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> ManagedBean<T> getBean(Class<T> beanClass) {
|
||||
|
@ -52,7 +60,7 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable {
|
|||
else {
|
||||
final ContainedBean<T> containedBean = beanContainer.getBean(
|
||||
beanClass,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
@ -87,7 +95,7 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable {
|
|||
final ContainedBean<T> containedBean = beanContainer.getBean(
|
||||
beanName,
|
||||
beanContract,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
@ -104,11 +112,6 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable {
|
|||
return bean;
|
||||
}
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
protected void forEachBean(Consumer<ManagedBean<?>> consumer) {
|
||||
registrations.values().forEach( consumer );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if ( beanContainer != null ) {
|
||||
|
@ -121,7 +124,7 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable {
|
|||
private final Class<B> beanClass;
|
||||
private final ContainedBean<B> containedBean;
|
||||
|
||||
public ContainedBeanManagedBeanAdapter(Class<B> beanClass, ContainedBean<B> containedBean) {
|
||||
private ContainedBeanManagedBeanAdapter(Class<B> beanClass, ContainedBean<B> containedBean) {
|
||||
this.beanClass = beanClass;
|
||||
this.containedBean = containedBean;
|
||||
}
|
||||
|
|
|
@ -16,8 +16,7 @@ import org.hibernate.cfg.AvailableSettings;
|
|||
import org.hibernate.jpa.event.spi.JpaIntegrator;
|
||||
import org.hibernate.resource.beans.container.internal.CdiBeanContainerBuilder;
|
||||
import org.hibernate.resource.beans.container.internal.CdiBeanContainerDelayedAccessImpl;
|
||||
import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainerImplementor;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
|
||||
import org.hibernate.tool.schema.Action;
|
||||
|
@ -31,7 +30,17 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
|||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class DelayedMixedAccessTest {
|
||||
public class DelayedMixedAccessTest implements BeanContainer.LifecycleOptions {
|
||||
@Override
|
||||
public boolean canUseCachedReferences() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean useJpaCompliantCreation() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelayedMixedAccess() {
|
||||
try ( final SeContainer cdiContainer = Helper.createSeContainer() ) {
|
||||
|
@ -45,7 +54,7 @@ public class DelayedMixedAccessTest {
|
|||
.applySetting( AvailableSettings.DELAY_CDI_ACCESS, "true" )
|
||||
.build();
|
||||
|
||||
final BeanContainerImplementor beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(
|
||||
final BeanContainer beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(
|
||||
cdiContainer.getBeanManager(),
|
||||
ssr
|
||||
);
|
||||
|
@ -54,7 +63,7 @@ public class DelayedMixedAccessTest {
|
|||
|
||||
final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(
|
||||
HostedBean.class,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
@ -65,7 +74,7 @@ public class DelayedMixedAccessTest {
|
|||
|
||||
final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(
|
||||
NonHostedBean.class,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ import org.hibernate.boot.registry.StandardServiceRegistry;
|
|||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.resource.beans.container.internal.CdiBeanContainerExtendedAccessImpl;
|
||||
import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
|
||||
|
@ -30,7 +29,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
|||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ExtendedMixedAccessTest {
|
||||
public class ExtendedMixedAccessTest implements BeanContainer.LifecycleOptions {
|
||||
@Test
|
||||
public void testExtendedMixedAccess() {
|
||||
final Helper.TestingExtendedBeanManager extendedBeanManager = Helper.createExtendedBeanManager();
|
||||
|
@ -52,7 +51,7 @@ public class ExtendedMixedAccessTest {
|
|||
|
||||
final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(
|
||||
HostedBean.class,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
@ -63,7 +62,7 @@ public class ExtendedMixedAccessTest {
|
|||
|
||||
final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(
|
||||
NonHostedBean.class,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
@ -73,4 +72,14 @@ public class ExtendedMixedAccessTest {
|
|||
extendedBeanManager.notifyListenerShuttingDown( beanManager );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUseCachedReferences() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean useJpaCompliantCreation() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import org.hibernate.jpa.event.spi.JpaIntegrator;
|
|||
import org.hibernate.resource.beans.container.internal.CdiBeanContainerBuilder;
|
||||
import org.hibernate.resource.beans.container.internal.CdiBeanContainerImmediateAccessImpl;
|
||||
import org.hibernate.resource.beans.container.internal.JpaCompliantLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainerImplementor;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBean;
|
||||
import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
|
||||
import org.hibernate.tool.schema.Action;
|
||||
|
@ -33,7 +33,17 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
|||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ImmediateMixedAccessTests {
|
||||
public class ImmediateMixedAccessTests implements BeanContainer.LifecycleOptions {
|
||||
@Override
|
||||
public boolean canUseCachedReferences() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean useJpaCompliantCreation() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImmediateMixedAccess() {
|
||||
try ( final SeContainer cdiContainer = Helper.createSeContainer() ) {
|
||||
|
@ -46,7 +56,7 @@ public class ImmediateMixedAccessTests {
|
|||
.applySetting( AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager() )
|
||||
.build();
|
||||
|
||||
final BeanContainerImplementor beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(
|
||||
final BeanContainer beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(
|
||||
cdiContainer.getBeanManager(),
|
||||
ssr
|
||||
);
|
||||
|
@ -55,7 +65,7 @@ public class ImmediateMixedAccessTests {
|
|||
|
||||
final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(
|
||||
HostedBean.class,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
@ -66,7 +76,7 @@ public class ImmediateMixedAccessTests {
|
|||
|
||||
final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(
|
||||
NonHostedBean.class,
|
||||
JpaCompliantLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
FallbackBeanInstanceProducer.INSTANCE
|
||||
);
|
||||
|
||||
|
|
|
@ -9,11 +9,9 @@ package org.hibernate.test.cdi.general.nonregistrymanaged;
|
|||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.integrator.spi.Integrator;
|
||||
import org.hibernate.resource.beans.container.internal.ContainerManagedLifecycleStrategy;
|
||||
import org.hibernate.resource.beans.container.spi.BeanContainer;
|
||||
import org.hibernate.resource.beans.container.spi.ContainedBeanImplementor;
|
||||
import org.hibernate.resource.beans.container.spi.ExtendedBeanManager;
|
||||
import org.hibernate.resource.beans.internal.FallbackBeanInstanceProducer;
|
||||
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
|
||||
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
|
||||
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
|
||||
|
@ -28,7 +26,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
|||
*
|
||||
* @author Yoann Rodiere
|
||||
*/
|
||||
public class NonRegistryManagedBeanConsumingIntegrator implements Integrator {
|
||||
public class NonRegistryManagedBeanConsumingIntegrator implements Integrator, BeanContainer.LifecycleOptions {
|
||||
|
||||
private final BeanInstanceProducer fallbackBeanInstanceProducer;
|
||||
|
||||
|
@ -49,6 +47,16 @@ public class NonRegistryManagedBeanConsumingIntegrator implements Integrator {
|
|||
this.fallbackBeanInstanceProducer = fallbackBeanInstanceProducer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUseCachedReferences() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean useJpaCompliantCreation() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
|
||||
|
@ -60,68 +68,68 @@ public class NonRegistryManagedBeanConsumingIntegrator implements Integrator {
|
|||
|
||||
applicationScopedBean1 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheApplicationScopedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
applicationScopedBean2 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheApplicationScopedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
dependentBean1 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheDependentBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
dependentBean2 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheDependentBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
reflectionInstantiatedBean1 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheReflectionInstantiatedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
reflectionInstantiatedBean2 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheReflectionInstantiatedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
namedApplicationScopedBean1 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheMainNamedApplicationScopedBeanImpl.NAME,
|
||||
TheNamedApplicationScopedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
namedApplicationScopedBean2 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheMainNamedApplicationScopedBeanImpl.NAME,
|
||||
TheNamedApplicationScopedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
namedDependentBean1 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheMainNamedDependentBeanImpl.NAME,
|
||||
TheNamedDependentBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
namedDependentBean2 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheMainNamedDependentBeanImpl.NAME,
|
||||
TheNamedDependentBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
namedReflectionInstantiatedBean1 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheReflectionInstantiatedBean.class.getName(),
|
||||
TheReflectionInstantiatedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
namedReflectionInstantiatedBean2 = (ContainedBeanImplementor) beanContainer.getBean(
|
||||
TheReflectionInstantiatedBean.class.getName(),
|
||||
TheReflectionInstantiatedBean.class,
|
||||
ContainerManagedLifecycleStrategy.INSTANCE,
|
||||
this,
|
||||
fallbackBeanInstanceProducer
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue