squash warnings in bean LifecycleStrategy stuff

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-08-30 20:58:06 +02:00
parent 7b8c403df7
commit 681bd09b9d
4 changed files with 52 additions and 60 deletions

View File

@ -45,7 +45,6 @@ import org.hibernate.resource.beans.container.spi.BeanContainer;
import org.hibernate.resource.beans.spi.BeanInstanceProducer;
import org.hibernate.resource.beans.spi.ManagedBeanRegistry;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;

View File

@ -20,10 +20,10 @@ import org.jboss.logging.Logger;
/**
* A {@link BeanLifecycleStrategy} to use when CDI compliance is required
* (i.e. when the bean lifecycle is to be managed by the CDI runtime, not the JPA runtime).
*
* <p>
* The main characteristic of this strategy is that every create/destroy operation is delegated
* to the CDI runtime.
*
* <p>
* In particular, @Singleton-scoped or @ApplicationScoped beans are retrieved from the CDI context,
* and are not duplicated, in contrast to {@link JpaCompliantLifecycleStrategy}.
*/
@ -89,16 +89,16 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
}
try {
this.instance = resolveContainerInstance();
this.beanInstance = this.instance.get();
instance = resolveContainerInstance();
beanInstance = instance.get();
}
catch (NotYetReadyException e) {
throw e;
}
catch (Exception e) {
log.debug( "Error resolving CDI bean - using fallback" );
this.beanInstance = produceFallbackInstance();
this.instance = null;
beanInstance = produceFallbackInstance();
instance = null;
}
this.beanManager = null;
@ -154,8 +154,8 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
root = beanManager.createInstance();
}
catch (Exception e) {
// this indicates that the BeanManager is not yet ready to use, which
// should be consider an error
// this indicates that the BeanManager is not yet ready to use,
// which should be considered an error
throw new NotYetReadyException( e );
}
@ -186,15 +186,14 @@ public class ContainerManagedLifecycleStrategy implements BeanLifecycleStrategy
}
@Override
@SuppressWarnings("unchecked")
protected Instance<B> resolveContainerInstance() {
final Instance root;
final Instance<Object> root;
try {
root = beanManager.createInstance();
}
catch (Exception e) {
// this indicates that the BeanManager is not yet ready to use, which
// should be consider an error
// this indicates that the BeanManager is not yet ready to use,
// which should be considered an error
throw new NotYetReadyException( e );
}

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.resource.beans.container.internal;
import java.util.Set;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.AnnotatedType;
import jakarta.enterprise.inject.spi.Bean;
@ -23,12 +22,12 @@ import org.jboss.logging.Logger;
/**
* 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).
*
* <p>
* The main characteristic of this strategy is that each requested bean is instantiated directly
* and guaranteed to not be shared in the CDI context.
*
* In particular, @Singleton-scoped or @ApplicationScoped beans are instantiated directly by this strategy,
* even if there is already an instance in the CDI context.
* <p>
* In particular, {@code @Singleton}-scoped or {@code @ApplicationScoped} beans are instantiated
* directly by this strategy, even if there is already an instance in the CDI context.
* This means singletons are not really singletons, but this seems to be the behavior required by
* the JPA 2.2 spec.
*/
@ -128,10 +127,10 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
}
try {
this.injectionTarget = beanManager.getInjectionTargetFactory( annotatedType ).createInjectionTarget( (Bean) null );
this.creationalContext = beanManager.createCreationalContext( null );
injectionTarget = beanManager.getInjectionTargetFactory( annotatedType ).createInjectionTarget( null );
creationalContext = beanManager.createCreationalContext( null );
this.beanInstance = this.injectionTarget.produce( creationalContext );
beanInstance = injectionTarget.produce( creationalContext );
injectionTarget.inject( beanInstance, creationalContext );
injectionTarget.postConstruct( beanInstance );
@ -141,7 +140,7 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
}
catch (Exception e) {
log.debugf( "Error resolving CDI bean [%s] - using fallback", beanType.getName() );
this.beanInstance = fallbackProducer.produceBeanInstance( beanType );
beanInstance = fallbackProducer.produceBeanInstance( beanType );
try {
if ( this.creationalContext != null ) {
@ -151,11 +150,11 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
catch (Exception ignore) {
}
this.creationalContext = null;
this.injectionTarget = null;
creationalContext = null;
injectionTarget = null;
}
this.beanManager = null;
beanManager = null;
}
@Override
@ -171,15 +170,15 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
}
injectionTarget.preDestroy( beanInstance );
injectionTarget.dispose( beanInstance );
this.creationalContext.release();
creationalContext.release();
}
catch (Exception ignore) {
}
finally {
this.beanInstance = null;
this.creationalContext = null;
this.injectionTarget = null;
beanInstance = null;
creationalContext = null;
injectionTarget = null;
}
}
}
@ -239,31 +238,31 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
}
try {
this.creationalContext = beanManager.createCreationalContext( null );
creationalContext = beanManager.createCreationalContext( null );
}
catch (Exception e) {
throw new NotYetReadyException( e );
}
try {
Set<Bean<?>> beans = beanManager.getBeans( beanType, new NamedBeanQualifier( beanName ) );
this.bean = (Bean<B>) beanManager.resolve( beans );
this.beanInstance = bean.create( creationalContext );
bean = (Bean<B>) beanManager.resolve( beanManager.getBeans( beanType,
new NamedBeanQualifier( beanName ) ) );
beanInstance = bean.create( creationalContext );
}
catch (Exception e) {
log.debugf( "Error resolving CDI bean [%s] - using fallback", beanName );
this.beanInstance = fallbackProducer.produceBeanInstance( beanName, beanType );
beanInstance = fallbackProducer.produceBeanInstance( beanName, beanType );
try {
if ( this.creationalContext != null ) {
this.creationalContext.release();
if ( creationalContext != null ) {
creationalContext.release();
}
}
catch (Exception ignore) {
}
this.creationalContext = null;
this.bean = null;
creationalContext = null;
bean = null;
}
}
@ -291,10 +290,10 @@ public class JpaCompliantLifecycleStrategy implements BeanLifecycleStrategy {
}
}
this.beanInstance = null;
this.creationalContext = null;
this.bean = null;
this.beanManager = null;
beanInstance = null;
creationalContext = null;
bean = null;
beanManager = null;
}
}

View File

@ -23,20 +23,17 @@ import org.hibernate.resource.beans.spi.BeanInstanceProducer;
* @author Steve Ebersole
*/
public abstract class AbstractCdiBeanContainer implements CdiBasedBeanContainer {
private Map<String,ContainedBeanImplementor<?>> beanCache = new HashMap<>();
private List<ContainedBeanImplementor<?>> registeredBeans = new ArrayList<>();
private final Map<String,ContainedBeanImplementor<?>> beanCache = new HashMap<>();
private final List<ContainedBeanImplementor<?>> registeredBeans = new ArrayList<>();
@Override
public <B> ContainedBean<B> getBean(
Class<B> beanType,
LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) {
if ( lifecycleOptions.canUseCachedReferences() ) {
return getCacheableBean( beanType, lifecycleOptions, fallbackProducer );
}
else {
return createBean( beanType, lifecycleOptions, fallbackProducer );
}
return lifecycleOptions.canUseCachedReferences()
? getCacheableBean( beanType, lifecycleOptions, fallbackProducer )
: createBean( beanType, lifecycleOptions, fallbackProducer );
}
@SuppressWarnings("unchecked")
@ -46,22 +43,21 @@ public abstract class AbstractCdiBeanContainer implements CdiBasedBeanContainer
BeanInstanceProducer fallbackProducer) {
final String beanCacheKey = Helper.determineBeanCacheKey( beanType );
final ContainedBeanImplementor existing = beanCache.get( beanCacheKey );
final ContainedBeanImplementor<?> existing = beanCache.get( beanCacheKey );
if ( existing != null ) {
return existing;
return (ContainedBeanImplementor<B>) existing;
}
final ContainedBeanImplementor bean = createBean( beanType, lifecycleOptions, fallbackProducer );
final ContainedBeanImplementor<B> bean = createBean( beanType, lifecycleOptions, fallbackProducer );
beanCache.put( beanCacheKey, bean );
return bean;
}
@SuppressWarnings("unchecked")
private <B> ContainedBeanImplementor<B> createBean(
Class<B> beanType,
LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) {
final ContainedBeanImplementor bean = createBean(
final ContainedBeanImplementor<B> bean = createBean(
beanType,
lifecycleOptions.useJpaCompliantCreation()
? JpaCompliantLifecycleStrategy.INSTANCE
@ -99,23 +95,22 @@ public abstract class AbstractCdiBeanContainer implements CdiBasedBeanContainer
BeanInstanceProducer fallbackProducer) {
final String beanCacheKey = Helper.determineBeanCacheKey( beanName, beanType );
final ContainedBeanImplementor existing = beanCache.get( beanCacheKey );
final ContainedBeanImplementor<?> existing = beanCache.get( beanCacheKey );
if ( existing != null ) {
return existing;
return (ContainedBeanImplementor<B>) existing;
}
final ContainedBeanImplementor bean = createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
final ContainedBeanImplementor<B> bean = createBean( beanName, beanType, lifecycleOptions, fallbackProducer );
beanCache.put( beanCacheKey, bean );
return bean;
}
@SuppressWarnings("unchecked")
private <B> ContainedBeanImplementor<B> createBean(
String beanName,
Class<B> beanType,
LifecycleOptions lifecycleOptions,
BeanInstanceProducer fallbackProducer) {
final ContainedBeanImplementor bean = createBean(
final ContainedBeanImplementor<B> bean = createBean(
beanName,
beanType,
lifecycleOptions.useJpaCompliantCreation()