From 91cdac46827c7b4db8ffb27f69ff7ddaf45713b5 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sun, 26 Jan 2025 12:26:43 +0100 Subject: [PATCH] as suggested by TODO in code, make ContainedBean extend ManagedBean --- .../CdiBeanContainerDelayedAccessImpl.java | 10 ++++ .../CdiBeanContainerExtendedAccessImpl.java | 17 ++++-- .../ContainerManagedLifecycleStrategy.java | 5 ++ .../JpaCompliantLifecycleStrategy.java | 27 ++++++--- .../beans/container/spi/ContainedBean.java | 9 +-- .../spi/ContainedBeanImplementor.java | 6 +- .../internal/ManagedBeanRegistryImpl.java | 55 ++----------------- ...nnectionProviderFromBeanContainerTest.java | 26 +++++++-- .../userdefined/SimpleBeanContainer.java | 13 ++++- .../UserDefinedGeneratorsTests.java | 22 +++++++- ...nnectionProviderFromBeanContainerTest.java | 22 +++++++- .../beancontainer/TestBeanContainer.java | 15 ++++- 12 files changed, 143 insertions(+), 84 deletions(-) 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 8fdfce395eba..160c64ba47a9 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 @@ -59,6 +59,11 @@ private BeanImpl( this.fallbackProducer = fallbackProducer; } + @Override + public Class getBeanClass() { + return beanType; + } + @Override public void initialize() { if ( delegateBean == null ) { @@ -99,6 +104,11 @@ private NamedBeanImpl( this.fallbackProducer = fallbackProducer; } + @Override + public Class getBeanClass() { + return beanType; + } + @Override public void initialize() { if ( delegateBean == null ) { 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 99838d271e63..8c36208bb6b2 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 @@ -104,6 +104,10 @@ private BeanImpl( this.fallbackProducer = fallbackProducer; } + @Override + public Class getBeanClass() { + return beanType; + } @Override public void initialize() { @@ -147,15 +151,16 @@ private NamedBeanImpl( this.fallbackProducer = fallbackProducer; } + @Override + public Class getBeanClass() { + return beanType; + } + @Override public void initialize() { if ( delegateContainedBean == null ) { - delegateContainedBean = lifecycleStrategy.createBean( - name, - beanType, - fallbackProducer, - DUMMY_BEAN_CONTAINER - ); + delegateContainedBean = + lifecycleStrategy.createBean( name, beanType, fallbackProducer, DUMMY_BEAN_CONTAINER ); delegateContainedBean.initialize(); } } 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 4474e8336a52..12bed317b0b1 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 @@ -72,6 +72,11 @@ private AbstractBeanImpl( this.beanManager = beanManager; } + @Override + public Class getBeanClass() { + return beanType; + } + @Override public B getBeanInstance() { if ( beanInstance == null ) { 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 8c915f1663e0..365d0e66db88 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 @@ -17,6 +17,8 @@ import org.jboss.logging.Logger; +import java.util.Set; + /** * A {@link BeanLifecycleStrategy} to use when JPA compliance is required * (i.e. when the bean lifecycle is to be managed by the JPA runtime, not the CDI runtime). @@ -77,15 +79,17 @@ private static class BeanImpl implements ContainedBeanImplementor { private B beanInstance; - public BeanImpl( - Class beanType, - BeanInstanceProducer fallbackProducer, - BeanManager beanManager) { + public BeanImpl(Class beanType, BeanInstanceProducer fallbackProducer, BeanManager beanManager) { this.beanType = beanType; this.fallbackProducer = fallbackProducer; this.beanManager = beanManager; } + @Override + public Class getBeanClass() { + return beanType; + } + @Override public B getBeanInstance() { if ( beanInstance == null ) { @@ -205,6 +209,11 @@ private NamedBeanImpl( this.beanManager = beanManager; } + @Override + public Class getBeanClass() { + return beanType; + } + @Override public B getBeanInstance() { if ( beanInstance == null ) { @@ -214,7 +223,6 @@ public B getBeanInstance() { } @Override - @SuppressWarnings("unchecked") public void initialize() { if ( beanInstance != null ) { return; @@ -243,8 +251,7 @@ public void initialize() { } try { - bean = (Bean) beanManager.resolve( beanManager.getBeans( beanType, - new NamedBeanQualifier( beanName ) ) ); + bean = resolveBean(); beanInstance = bean.create( creationalContext ); } catch (Exception e) { @@ -264,6 +271,12 @@ public void initialize() { } } + @SuppressWarnings("unchecked") + private Bean resolveBean() { + final Set> beans = beanManager.getBeans( beanType, new NamedBeanQualifier( beanName ) ); + return (Bean) beanManager.resolve( beans ); + } + @Override public void release() { if ( beanInstance == null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBean.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBean.java index 99df240d7ebf..4289d5974b7a 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBean.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBean.java @@ -4,13 +4,10 @@ */ package org.hibernate.resource.beans.container.spi; +import org.hibernate.resource.beans.spi.ManagedBean; + /** * @author Steve Ebersole */ -public interface ContainedBean { - // todo (5.3) : can we combine ContainedBean and org.hibernate.resource.beans.spi.ManagedBean into the same thing? - /** - * Get the bean instance producer associated with this container-backed bean - */ - B getBeanInstance(); +public interface ContainedBean extends ManagedBean { } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBeanImplementor.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBeanImplementor.java index 7cf2f5f34d53..962cb051e308 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBeanImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/container/spi/ContainedBeanImplementor.java @@ -5,9 +5,9 @@ package org.hibernate.resource.beans.container.spi; /** - * Release-able extension to {@link ContainedBean}. We make the split to make it - * clear that generally speaking the callers to BeanContainer should not perform - * the release + * Releasable extension to {@link ContainedBean}. We make this split to clarify + * that clients of {@link BeanContainer} are not usually responsible for calling + * {@link #initialize()} and {@link #release()}. * * @author Steve Ebersole */ 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 56ea990daf69..8dd743bd6e61 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,7 +8,6 @@ import java.util.Map; import org.hibernate.resource.beans.container.spi.BeanContainer; -import org.hibernate.resource.beans.container.spi.ContainedBean; import org.hibernate.resource.beans.container.spi.FallbackContainedBean; import org.hibernate.resource.beans.spi.BeanInstanceProducer; import org.hibernate.resource.beans.spi.ManagedBean; @@ -87,38 +86,16 @@ public ManagedBean getBean( } private ManagedBean createBean(Class beanClass, BeanInstanceProducer fallbackBeanInstanceProducer) { - if ( beanContainer == null ) { - return new FallbackContainedBean<>( beanClass, fallbackBeanInstanceProducer ); - } - else { - final ContainedBean containedBean = - beanContainer.getBean( beanClass, this, fallbackBeanInstanceProducer ); - if ( containedBean instanceof ManagedBean ) { - //noinspection unchecked - return (ManagedBean) containedBean; - } - else { - return new ContainedBeanManagedBeanAdapter<>( beanClass, containedBean ); - } - } + return beanContainer == null + ? new FallbackContainedBean<>( beanClass, fallbackBeanInstanceProducer ) + : beanContainer.getBean( beanClass, this, fallbackBeanInstanceProducer ); } private ManagedBean createBean( String beanName, Class beanContract, BeanInstanceProducer fallbackBeanInstanceProducer) { - if ( beanContainer == null ) { - return new FallbackContainedBean<>( beanName, beanContract, fallbackBeanInstanceProducer ); - } - else { - final ContainedBean containedBean = - beanContainer.getBean( beanName, beanContract, this, fallbackBeanInstanceProducer ); - if ( containedBean instanceof ManagedBean ) { - //noinspection unchecked - return (ManagedBean) containedBean; - } - else { - return new ContainedBeanManagedBeanAdapter<>( beanContract, containedBean ); - } - } + return beanContainer == null + ? new FallbackContainedBean<>( beanName, beanContract, fallbackBeanInstanceProducer ) + : beanContainer.getBean( beanName, beanContract, this, fallbackBeanInstanceProducer ); } @Override @@ -128,24 +105,4 @@ public void stop() { } registrations.clear(); } - - private static class ContainedBeanManagedBeanAdapter implements ManagedBean { - private final Class beanClass; - private final ContainedBean containedBean; - - private ContainedBeanManagedBeanAdapter(Class beanClass, ContainedBean containedBean) { - this.beanClass = beanClass; - this.containedBean = containedBean; - } - - @Override - public Class getBeanClass() { - return beanClass; - } - - @Override - public B getBeanInstance() { - return containedBean.getBeanInstance(); - } - } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionProviderFromBeanContainerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionProviderFromBeanContainerTest.java index 808a1f623c28..85098aade52d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionProviderFromBeanContainerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/connections/ConnectionProviderFromBeanContainerTest.java @@ -42,17 +42,35 @@ public ContainedBean getBean( Class beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { - return () -> (B) ( beanType == DummyConnectionProvider.class ? - dummyConnectionProvider : fallbackProducer.produceBeanInstance( beanType ) ); + return new ContainedBean<>() { + @Override + public B getBeanInstance() { + return (B) (beanType == DummyConnectionProvider.class ? + dummyConnectionProvider : fallbackProducer.produceBeanInstance( beanType ) ); + } + + @Override + public Class getBeanClass() { + return beanType; + } + }; } - @Override public ContainedBean getBean( String name, Class beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { - return () -> (B) fallbackProducer.produceBeanInstance( beanType ); + return new ContainedBean<>() { + @Override + public B getBeanInstance() { + return fallbackProducer.produceBeanInstance( beanType ); + } + @Override + public Class getBeanClass() { + return beanType; + } + }; } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/SimpleBeanContainer.java b/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/SimpleBeanContainer.java index a25d524fa6fe..294e4c9bcca1 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/SimpleBeanContainer.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/SimpleBeanContainer.java @@ -23,8 +23,17 @@ public ContainedBean getBean( Class beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { - return () -> (B) ( beanType == SimpleGenerator.class ? - new SimpleGenerator( new AtomicLong( INITIAL_VALUE ) ) : fallbackProducer.produceBeanInstance( beanType ) ); + return new ContainedBean<>() { + @Override + public B getBeanInstance() { + return (B) (beanType == SimpleGenerator.class ? + new SimpleGenerator( new AtomicLong( INITIAL_VALUE ) ) : fallbackProducer.produceBeanInstance( beanType ) ); + } + @Override + public Class getBeanClass() { + return beanType; + } + }; } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/UserDefinedGeneratorsTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/UserDefinedGeneratorsTests.java index 910c0c43452e..2ecea5cfef17 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/UserDefinedGeneratorsTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/idgen/userdefined/UserDefinedGeneratorsTests.java @@ -65,10 +65,28 @@ public void testCreateGeneratorsByBeanContainer() { if (beanType == TestIdentifierGenerator.class) { assertThat( options.canUseCachedReferences(), is( false ) ); assertThat( options.useJpaCompliantCreation(), is( true ) ); - return (ContainedBean) TestIdentifierGenerator::new; + return new ContainedBean() { + @Override + public TestIdentifierGenerator getBeanInstance() { + return new TestIdentifierGenerator(); + } + @Override + public Class getBeanClass() { + return TestIdentifierGenerator.class; + } + }; } else { - return (ContainedBean) () -> ( ( BeanInstanceProducer ) invocation.getArguments()[2] ).produceBeanInstance( beanType ); + return new ContainedBean<>() { + @Override + public Object getBeanInstance() { + return ( ( BeanInstanceProducer ) invocation.getArguments()[2] ).produceBeanInstance( beanType ); + } + @Override + public Class getBeanClass() { + return beanType; + } + }; } } ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/MultiTenantConnectionProviderFromBeanContainerTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/MultiTenantConnectionProviderFromBeanContainerTest.java index 5788ea40a5c2..25f2e02e7230 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/MultiTenantConnectionProviderFromBeanContainerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/MultiTenantConnectionProviderFromBeanContainerTest.java @@ -43,7 +43,16 @@ public ContainedBean getBean( Class beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { - return () -> (B) ( beanType == MultiTenantConnectionProvider.class ? providerFromBeanContainer : fallbackProducer.produceBeanInstance( beanType ) ); + return new ContainedBean<>() { + @Override + public B getBeanInstance() { + return (B) (beanType == MultiTenantConnectionProvider.class ? providerFromBeanContainer : fallbackProducer.produceBeanInstance( beanType ) ); + } + @Override + public Class getBeanClass() { + return beanType; + } + }; } @Override @@ -52,7 +61,16 @@ public ContainedBean getBean( Class beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { - return () -> (B) fallbackProducer.produceBeanInstance( beanType ); + return new ContainedBean<>() { + @Override + public B getBeanInstance() { + return fallbackProducer.produceBeanInstance( beanType ); + } + @Override + public Class getBeanClass() { + return beanType; + } + }; } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/TestBeanContainer.java b/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/TestBeanContainer.java index 788c95727f04..3818bef8620d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/TestBeanContainer.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/multitenancy/beancontainer/TestBeanContainer.java @@ -12,7 +12,7 @@ /** * @author Yanming Zhou */ -@SuppressWarnings("unchecked") +@SuppressWarnings({"unchecked", "unused"}) public class TestBeanContainer implements BeanContainer { @Override @@ -20,8 +20,17 @@ public ContainedBean getBean( Class beanType, LifecycleOptions lifecycleOptions, BeanInstanceProducer fallbackProducer) { - return () -> (B) ( beanType == CurrentTenantIdentifierResolver.class ? - TestCurrentTenantIdentifierResolver.INSTANCE_FOR_BEAN_CONTAINER : fallbackProducer.produceBeanInstance( beanType ) ); + return new ContainedBean<>() { + @Override + public B getBeanInstance() { + return (B) (beanType == CurrentTenantIdentifierResolver.class ? + TestCurrentTenantIdentifierResolver.INSTANCE_FOR_BEAN_CONTAINER : fallbackProducer.produceBeanInstance( beanType ) ); + } + @Override + public Class getBeanClass() { + return beanType; + } + }; } @Override