diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBasedBeanContainer.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBasedBeanContainer.java index e0dc1863c5..dae03cdd13 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBasedBeanContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBasedBeanContainer.java @@ -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(); } 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 3477cc5e01..86f1decfdd 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 @@ -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 containerClass; + final Class containerClass; final Class ctorArgType; if ( extendedBeanManagerClass.isInstance( beanManagerRef ) ) { @@ -66,7 +66,7 @@ public class CdiBeanContainerBuilder { } try { - final Constructor ctor = containerClass.getDeclaredConstructor( ctorArgType ); + final Constructor ctor = containerClass.getDeclaredConstructor( ctorArgType ); try { ReflectHelper.ensureAccessibility( ctor ); return ctor.newInstance( ctorArgType.cast( beanManagerRef ) ); diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerDelayedAccessImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerDelayedAccessImpl.java index 8c9cc47184..cbbaa896b7 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerDelayedAccessImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/internal/CdiBeanContainerDelayedAccessImpl.java @@ -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 ContainedBean createBean( + protected ContainedBeanImplementor createBean( Class beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer) { @@ -39,7 +38,7 @@ public class CdiBeanContainerDelayedAccessImpl extends AbstractBeanContainer { } @Override - protected ContainedBean createBean( + protected ContainedBeanImplementor createBean( String name, Class beanType, BeanLifecycleStrategy lifecycleStrategy, 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 95b985248e..7ed5a80cf4 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 @@ -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 ContainedBean createBean( + protected ContainedBeanImplementor createBean( Class beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer) { if ( usableBeanManager == null ) { - final BeanImpl 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 ContainedBean createBean( + protected ContainedBeanImplementor createBean( String name, Class beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer) { if ( usableBeanManager == null ) { - final NamedBeanImpl 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 ContainedBean getBean(Class beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer) { - return CdiBeanContainerExtendedAccessImpl.this.getBean( beanType, lifecycleStrategy, fallbackProducer ); + public ContainedBean getBean( + Class beanType, + LifecycleOptions lifecycleOptions, + BeanInstanceProducer fallbackProducer) { + // todo (5.3) : should this throw an exception instead? + return CdiBeanContainerExtendedAccessImpl.this.getBean( beanType, lifecycleOptions, fallbackProducer ); } @Override public ContainedBean getBean( - String name, + String beanName, Class 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 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 15ff40f8dc..8fcd87ed03 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 @@ -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 ContainedBean createBean( + 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; } @Override - protected ContainedBean createBean( + protected ContainedBeanImplementor createBean( String name, Class beanType, BeanLifecycleStrategy lifecycleStrategy, @@ -63,9 +56,7 @@ public class CdiBeanContainerImmediateAccessImpl extends AbstractBeanContainer { 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 0a200c7ce8..38f4a36995 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 @@ -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 ContainedBeanImplementor findRegisteredBean( - Class beanClass, - BeanContainerImplementor container) { - return null; - } - - @Override - public ContainedBeanImplementor findRegisteredBean( - String beanName, - Class beanClass, - BeanContainerImplementor container) { - return null; - } @Override public ContainedBeanImplementor createBean( Class 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 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() ); } 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 2f76731b43..b848a5b9cc 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 @@ -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 ContainedBeanImplementor findRegisteredBean( - Class beanClass, - BeanContainerImplementor container) { - return container.findRegistered( beanClass.getName() ); - } - - @Override - @SuppressWarnings("unchecked") - public ContainedBeanImplementor findRegisteredBean( - String beanName, - Class beanClass, - BeanContainerImplementor container) { - return container.findRegistered( beanClass.getName() + ':' + beanName ); - } - @Override public ContainedBeanImplementor createBean( Class beanClass, BeanInstanceProducer fallbackProducer, - BeanContainerImplementor container) { - final BeanImpl 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 beanClass, BeanInstanceProducer fallbackProducer, - BeanContainerImplementor container) { - final NamedBeanImpl 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 implements ContainedBeanImplementor { private final Class beanType; diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractBeanContainer.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractBeanContainer.java index 72197d5ddb..605549aa31 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractBeanContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/AbstractBeanContainer.java @@ -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> registrations = new HashMap<>(); + private Map> beanCache = new HashMap<>(); + private List> 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 ContainedBean getBean( + public ContainedBean getBean( Class 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 ContainedBean getCacheableBean( + Class 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 ContainedBean createBean( + @SuppressWarnings("unchecked") + private ContainedBeanImplementor createBean( + Class beanType, + LifecycleOptions lifecycleOptions, + BeanInstanceProducer fallbackProducer) { + final ContainedBeanImplementor bean = createBean( + beanType, + lifecycleOptions.useJpaCompliantCreation() + ? JpaCompliantLifecycleStrategy.INSTANCE + : ContainerManagedLifecycleStrategy.INSTANCE, + fallbackProducer + ); + registeredBeans.add( bean ); + return bean; + } + + protected abstract ContainedBeanImplementor createBean( Class beanType, BeanLifecycleStrategy lifecycleStrategy, BeanInstanceProducer fallbackProducer); @Override - @SuppressWarnings("unchecked") - public final ContainedBean getBean( - String name, + public ContainedBean getBean( + String beanName, Class 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 ContainedBeanImplementor getCacheableBean( + String beanName, + Class 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 ContainedBean createBean( + @SuppressWarnings("unchecked") + private ContainedBeanImplementor createBean( + String beanName, + Class 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 ContainedBeanImplementor createBean( String name, Class beanType, BeanLifecycleStrategy lifecycleStrategy, @@ -73,14 +136,15 @@ public abstract class AbstractBeanContainer implements CdiBasedBeanContainer { @SuppressWarnings("WeakerAccess") protected final void forEachBean(Consumer> 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(); } } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanContainer.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanContainer.java index 1e2701a459..0119033c9a 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanContainer.java @@ -15,14 +15,19 @@ import org.hibernate.service.spi.Stoppable; * @author Steve Ebersole */ public interface BeanContainer extends Stoppable { + interface LifecycleOptions { + boolean canUseCachedReferences(); + boolean useJpaCompliantCreation(); + } + ContainedBean getBean( Class beanType, - BeanLifecycleStrategy lifecycleStrategy, + LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer); ContainedBean getBean( String name, Class beanType, - BeanLifecycleStrategy lifecycleStrategy, + LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer); } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanContainerImplementor.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanContainerImplementor.java deleted file mode 100644 index 8d2a4c39ab..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanContainerImplementor.java +++ /dev/null @@ -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); -} diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanLifecycleStrategy.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanLifecycleStrategy.java index da39a7c5f9..117e0b0346 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanLifecycleStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/BeanLifecycleStrategy.java @@ -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 { - - ContainedBeanImplementor findRegisteredBean(Class beanClass, BeanContainerImplementor container); - ContainedBeanImplementor findRegisteredBean(String beanName, Class beanClass, BeanContainerImplementor container); - ContainedBeanImplementor createBean( Class beanClass, BeanInstanceProducer fallbackProducer, - BeanContainerImplementor container); + BeanContainer beanContainer); ContainedBeanImplementor createBean( String beanName, Class beanClass, BeanInstanceProducer fallbackProducer, - BeanContainerImplementor container); - + BeanContainer beanContainer); } 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 907cdefbbe..2c5c19b79d 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 @@ -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; } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java index a0f8fdb1a8..c90730721e 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryImpl.java @@ -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> 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 ManagedBean getBean(Class beanClass) { @@ -52,7 +60,7 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable { else { final ContainedBean containedBean = beanContainer.getBean( beanClass, - JpaCompliantLifecycleStrategy.INSTANCE, + this, FallbackBeanInstanceProducer.INSTANCE ); @@ -87,7 +95,7 @@ public class ManagedBeanRegistryImpl implements ManagedBeanRegistry, Stoppable { final ContainedBean 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> 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 beanClass; private final ContainedBean containedBean; - public ContainedBeanManagedBeanAdapter(Class beanClass, ContainedBean containedBean) { + private ContainedBeanManagedBeanAdapter(Class beanClass, ContainedBean containedBean) { this.beanClass = beanClass; this.containedBean = containedBean; } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/DelayedMixedAccessTest.java b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/DelayedMixedAccessTest.java index 10249cc80a..760a19af87 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/DelayedMixedAccessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/DelayedMixedAccessTest.java @@ -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 = beanContainer.getBean( HostedBean.class, - JpaCompliantLifecycleStrategy.INSTANCE, + this, FallbackBeanInstanceProducer.INSTANCE ); @@ -65,7 +74,7 @@ public class DelayedMixedAccessTest { final ContainedBean nonHostedBean = beanContainer.getBean( NonHostedBean.class, - JpaCompliantLifecycleStrategy.INSTANCE, + this, FallbackBeanInstanceProducer.INSTANCE ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ExtendedMixedAccessTest.java b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ExtendedMixedAccessTest.java index 0ad1d795b5..404f06c8ed 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ExtendedMixedAccessTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ExtendedMixedAccessTest.java @@ -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 = beanContainer.getBean( HostedBean.class, - JpaCompliantLifecycleStrategy.INSTANCE, + this, FallbackBeanInstanceProducer.INSTANCE ); @@ -63,7 +62,7 @@ public class ExtendedMixedAccessTest { final ContainedBean 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; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ImmediateMixedAccessTests.java b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ImmediateMixedAccessTests.java index 39e87f24a9..45e5dcf8da 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ImmediateMixedAccessTests.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/mixed/ImmediateMixedAccessTests.java @@ -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 = beanContainer.getBean( HostedBean.class, - JpaCompliantLifecycleStrategy.INSTANCE, + this, FallbackBeanInstanceProducer.INSTANCE ); @@ -66,7 +76,7 @@ public class ImmediateMixedAccessTests { final ContainedBean nonHostedBean = beanContainer.getBean( NonHostedBean.class, - JpaCompliantLifecycleStrategy.INSTANCE, + this, FallbackBeanInstanceProducer.INSTANCE ); diff --git a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/nonregistrymanaged/NonRegistryManagedBeanConsumingIntegrator.java b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/nonregistrymanaged/NonRegistryManagedBeanConsumingIntegrator.java index df7f4642f8..bc80fe1d96 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cdi/general/nonregistrymanaged/NonRegistryManagedBeanConsumingIntegrator.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cdi/general/nonregistrymanaged/NonRegistryManagedBeanConsumingIntegrator.java @@ -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 ); }