Skip to content

Commit

Permalink
as suggested by TODO in code, make ContainedBean extend ManagedBean
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinking committed Jan 26, 2025
1 parent c5eba23 commit 91cdac4
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ private BeanImpl(
this.fallbackProducer = fallbackProducer;
}

@Override
public Class<B> getBeanClass() {
return beanType;
}

@Override
public void initialize() {
if ( delegateBean == null ) {
Expand Down Expand Up @@ -99,6 +104,11 @@ private NamedBeanImpl(
this.fallbackProducer = fallbackProducer;
}

@Override
public Class<B> getBeanClass() {
return beanType;
}

@Override
public void initialize() {
if ( delegateBean == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ private BeanImpl(
this.fallbackProducer = fallbackProducer;
}

@Override
public Class<B> getBeanClass() {
return beanType;
}

@Override
public void initialize() {
Expand Down Expand Up @@ -147,15 +151,16 @@ private NamedBeanImpl(
this.fallbackProducer = fallbackProducer;
}

@Override
public Class<B> 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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ private AbstractBeanImpl(
this.beanManager = beanManager;
}

@Override
public Class<B> getBeanClass() {
return beanType;
}

@Override
public B getBeanInstance() {
if ( beanInstance == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -77,15 +79,17 @@ private static class BeanImpl<B> implements ContainedBeanImplementor<B> {

private B beanInstance;

public BeanImpl(
Class<B> beanType,
BeanInstanceProducer fallbackProducer,
BeanManager beanManager) {
public BeanImpl(Class<B> beanType, BeanInstanceProducer fallbackProducer, BeanManager beanManager) {
this.beanType = beanType;
this.fallbackProducer = fallbackProducer;
this.beanManager = beanManager;
}

@Override
public Class<B> getBeanClass() {
return beanType;
}

@Override
public B getBeanInstance() {
if ( beanInstance == null ) {
Expand Down Expand Up @@ -205,6 +209,11 @@ private NamedBeanImpl(
this.beanManager = beanManager;
}

@Override
public Class<B> getBeanClass() {
return beanType;
}

@Override
public B getBeanInstance() {
if ( beanInstance == null ) {
Expand All @@ -214,7 +223,6 @@ public B getBeanInstance() {
}

@Override
@SuppressWarnings("unchecked")
public void initialize() {
if ( beanInstance != null ) {
return;
Expand Down Expand Up @@ -243,8 +251,7 @@ public void initialize() {
}

try {
bean = (Bean<B>) beanManager.resolve( beanManager.getBeans( beanType,
new NamedBeanQualifier( beanName ) ) );
bean = resolveBean();
beanInstance = bean.create( creationalContext );
}
catch (Exception e) {
Expand All @@ -264,6 +271,12 @@ public void initialize() {
}
}

@SuppressWarnings("unchecked")
private Bean<B> resolveBean() {
final Set<Bean<?>> beans = beanManager.getBeans( beanType, new NamedBeanQualifier( beanName ) );
return (Bean<B>) beanManager.resolve( beans );
}

@Override
public void release() {
if ( beanInstance == null ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
*/
package org.hibernate.resource.beans.container.spi;

import org.hibernate.resource.beans.spi.ManagedBean;

/**
* @author Steve Ebersole
*/
public interface ContainedBean<B> {
// 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<B> extends ManagedBean<B> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,38 +86,16 @@ public <T> ManagedBean<T> getBean(
}

private <T> ManagedBean<T> createBean(Class<T> beanClass, BeanInstanceProducer fallbackBeanInstanceProducer) {
if ( beanContainer == null ) {
return new FallbackContainedBean<>( beanClass, fallbackBeanInstanceProducer );
}
else {
final ContainedBean<T> containedBean =
beanContainer.getBean( beanClass, this, fallbackBeanInstanceProducer );
if ( containedBean instanceof ManagedBean ) {
//noinspection unchecked
return (ManagedBean<T>) containedBean;
}
else {
return new ContainedBeanManagedBeanAdapter<>( beanClass, containedBean );
}
}
return beanContainer == null
? new FallbackContainedBean<>( beanClass, fallbackBeanInstanceProducer )
: beanContainer.getBean( beanClass, this, fallbackBeanInstanceProducer );
}

private <T> ManagedBean<T> createBean(
String beanName, Class<T> beanContract, BeanInstanceProducer fallbackBeanInstanceProducer) {
if ( beanContainer == null ) {
return new FallbackContainedBean<>( beanName, beanContract, fallbackBeanInstanceProducer );
}
else {
final ContainedBean<T> containedBean =
beanContainer.getBean( beanName, beanContract, this, fallbackBeanInstanceProducer );
if ( containedBean instanceof ManagedBean ) {
//noinspection unchecked
return (ManagedBean<T>) containedBean;
}
else {
return new ContainedBeanManagedBeanAdapter<>( beanContract, containedBean );
}
}
return beanContainer == null
? new FallbackContainedBean<>( beanName, beanContract, fallbackBeanInstanceProducer )
: beanContainer.getBean( beanName, beanContract, this, fallbackBeanInstanceProducer );
}

@Override
Expand All @@ -128,24 +105,4 @@ public void stop() {
}
registrations.clear();
}

private static class ContainedBeanManagedBeanAdapter<B> implements ManagedBean<B> {
private final Class<B> beanClass;
private final ContainedBean<B> containedBean;

private ContainedBeanManagedBeanAdapter(Class<B> beanClass, ContainedBean<B> containedBean) {
this.beanClass = beanClass;
this.containedBean = containedBean;
}

@Override
public Class<B> getBeanClass() {
return beanClass;
}

@Override
public B getBeanInstance() {
return containedBean.getBeanInstance();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,35 @@ public <B> ContainedBean<B> getBean(
Class<B> 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<B> getBeanClass() {
return beanType;
}
};
}

@Override
public <B> ContainedBean<B> getBean(
String name,
Class<B> beanType,
LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) {
return () -> (B) fallbackProducer.produceBeanInstance( beanType );
return new ContainedBean<>() {
@Override
public B getBeanInstance() {
return fallbackProducer.produceBeanInstance( beanType );
}
@Override
public Class<B> getBeanClass() {
return beanType;
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ public <B> ContainedBean<B> getBean(
Class<B> 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<B> getBeanClass() {
return beanType;
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestIdentifierGenerator>() {
@Override
public TestIdentifierGenerator getBeanInstance() {
return new TestIdentifierGenerator();
}
@Override
public Class<TestIdentifierGenerator> 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;
}
};
}
} );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ public <B> ContainedBean<B> getBean(
Class<B> 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<B> getBeanClass() {
return beanType;
}
};
}

@Override
Expand All @@ -52,7 +61,16 @@ public <B> ContainedBean<B> getBean(
Class<B> beanType,
LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) {
return () -> (B) fallbackProducer.produceBeanInstance( beanType );
return new ContainedBean<>() {
@Override
public B getBeanInstance() {
return fallbackProducer.produceBeanInstance( beanType );
}
@Override
public Class<B> getBeanClass() {
return beanType;
}
};
}

@Override
Expand Down
Loading

0 comments on commit 91cdac4

Please sign in to comment.