From b3ac2feddf57794b34a1fcd08c0bf768124ca3c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Mon, 18 Dec 2017 18:20:38 +0100 Subject: [PATCH] HHH-12133 Move CDI lifecycle management code to dedicated strategies This commit should not change the current behavior, it is only about moving code to separate classes to make the following changes clearer. --- .../CdiLifecycleManagementStrategy.java | 19 +++ .../resource/beans/internal/Helper.java | 18 +++ .../JpaCdiLifecycleManagementStrategy.java | 111 ++++++++++++++++++ .../ManagedBeanRegistryCdiDelayedImpl.java | 93 +++++++-------- .../ManagedBeanRegistryCdiExtendedImpl.java | 96 +++++++-------- .../ManagedBeanRegistryCdiStandardImpl.java | 100 +--------------- ...tandardCdiLifecycleManagementStrategy.java | 75 ++++++++++++ .../UnnamedRegistryScopedManagedBeanImpl.java | 53 +++++++++ 8 files changed, 362 insertions(+), 203 deletions(-) create mode 100644 hibernate-core/src/main/java/org/hibernate/resource/beans/internal/CdiLifecycleManagementStrategy.java create mode 100644 hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java create mode 100644 hibernate-core/src/main/java/org/hibernate/resource/beans/internal/StandardCdiLifecycleManagementStrategy.java create mode 100644 hibernate-core/src/main/java/org/hibernate/resource/beans/internal/UnnamedRegistryScopedManagedBeanImpl.java diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/CdiLifecycleManagementStrategy.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/CdiLifecycleManagementStrategy.java new file mode 100644 index 0000000000..f009e805e7 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/CdiLifecycleManagementStrategy.java @@ -0,0 +1,19 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.resource.beans.internal; + +import javax.enterprise.inject.spi.BeanManager; + +import org.hibernate.resource.beans.spi.ManagedBean; + +interface CdiLifecycleManagementStrategy { + + ManagedBean createBean(BeanManager beanManager, Class beanClass); + + ManagedBean createBean(BeanManager beanManager, String beanName, Class beanClass); + +} 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 1c26d3ceb0..4a361f14ed 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,6 +22,24 @@ public class Helper { private Helper() { } + @SuppressWarnings("unchecked") + public Bean getBean(Class beanContract, BeanManager beanManager) { + Set> beans = beanManager.getBeans( beanContract ); + + if ( beans.isEmpty() ) { + throw new IllegalArgumentException( + "BeanManager returned no matching beans: contract = " + beanContract.getName() + ); + } + if ( beans.size() > 1 ) { + throw new IllegalArgumentException( + "BeanManager returned multiple matching beans: contract = " + beanContract.getName() + ); + } + + return (Bean) beans.iterator().next(); + } + @SuppressWarnings("unchecked") public Bean getNamedBean(String beanName, Class beanContract, BeanManager beanManager) { Set> beans = beanManager.getBeans( beanContract, new NamedBeanQualifier( beanName ) ); diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java new file mode 100644 index 0000000000..0ef1567953 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java @@ -0,0 +1,111 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.resource.beans.internal; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionTarget; + +import org.hibernate.resource.beans.spi.ManagedBean; + +class JpaCdiLifecycleManagementStrategy implements CdiLifecycleManagementStrategy { + + static final JpaCdiLifecycleManagementStrategy INSTANCE = new JpaCdiLifecycleManagementStrategy(); + + private JpaCdiLifecycleManagementStrategy() { + // private constructor, do not use + } + + @Override + public ManagedBean createBean(BeanManager beanManager, Class beanClass) { + AnnotatedType annotatedType = beanManager.createAnnotatedType( beanClass ); + InjectionTarget injectionTarget = beanManager.createInjectionTarget( annotatedType ); + CreationalContext creationalContext = beanManager.createCreationalContext( null ); + + T beanInstance = injectionTarget.produce( creationalContext ); + injectionTarget.inject( beanInstance, creationalContext ); + + injectionTarget.postConstruct( beanInstance ); + + return new JpaManagedBeanImpl<>( beanClass, injectionTarget, creationalContext, beanInstance ); + } + + @Override + public ManagedBean createBean(BeanManager beanManager, String beanName, Class beanClass) { + Bean bean = Helper.INSTANCE.getNamedBean( beanName, beanClass, beanManager ); + + CreationalContext creationalContext = beanManager.createCreationalContext( null ); + + T beanInstance = bean.create( creationalContext ); + + return new NamedJpaManagedBeanImpl<>( beanClass, creationalContext, beanInstance ); + } + + private static class JpaManagedBeanImpl implements ManagedBean { + private final Class beanClass; + private final InjectionTarget injectionTarget; + private final CreationalContext creationContext; + private final T beanInstance; + + private JpaManagedBeanImpl( + Class beanClass, + InjectionTarget injectionTarget, CreationalContext creationContext, T beanInstance) { + this.beanClass = beanClass; + this.injectionTarget = injectionTarget; + this.creationContext = creationContext; + this.beanInstance = beanInstance; + } + + @Override + public Class getBeanClass() { + return beanClass; + } + + @Override + public T getBeanInstance() { + return beanInstance; + } + + @Override + public void release() { + injectionTarget.preDestroy( beanInstance ); + injectionTarget.dispose( beanInstance ); + creationContext.release(); + } + } + + private static class NamedJpaManagedBeanImpl implements ManagedBean { + private final Class beanClass; + private final CreationalContext creationContext; + private final T beanInstance; + + private NamedJpaManagedBeanImpl( + Class beanClass, + CreationalContext creationContext, T beanInstance) { + this.beanClass = beanClass; + this.creationContext = creationContext; + this.beanInstance = beanInstance; + } + + @Override + public Class getBeanClass() { + return beanClass; + } + + @Override + public T getBeanInstance() { + return beanInstance; + } + + @Override + public void release() { + creationContext.release(); + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiDelayedImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiDelayedImpl.java index ed40cfed81..27d7c9d09d 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiDelayedImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiDelayedImpl.java @@ -6,12 +6,7 @@ */ package org.hibernate.resource.beans.internal; -import java.util.Set; -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.AnnotatedType; -import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.InjectionTarget; import org.hibernate.resource.beans.spi.AbstractManagedBeanRegistry; import org.hibernate.resource.beans.spi.ManagedBean; @@ -39,25 +34,29 @@ public class ManagedBeanRegistryCdiDelayedImpl extends AbstractManagedBeanRegist @Override protected ManagedBean createBean(Class beanClass) { - return new ManagedBeanImpl<>( beanClass ); + return new LazilyInitializedManagedBeanImpl<>( beanClass, JpaCdiLifecycleManagementStrategy.INSTANCE ); } @Override protected ManagedBean createBean(String beanName, Class beanContract) { - return new NamedManagedBeanImpl<>( beanName, beanContract ); + return new LazilyInitializedNamedManagedBeanImpl<>( beanName, beanContract, StandardCdiLifecycleManagementStrategy.INSTANCE ); } - private class ManagedBeanImpl implements ManagedBean { + /** + * A {@link ManagedBean} that is initialized upon the first call to {@link #getBeanInstance()}, + * relying on a {@link CdiLifecycleManagementStrategy} to initialize a delegate. + * + * @param The type of bean instances + */ + private class LazilyInitializedManagedBeanImpl implements ManagedBean { private final Class beanClass; + private final CdiLifecycleManagementStrategy strategy; - private boolean initialized = false; + private ManagedBean delegate = null; - private InjectionTarget injectionTarget; - private CreationalContext creationContext; - private T beanInstance; - - ManagedBeanImpl(Class beanClass) { + LazilyInitializedManagedBeanImpl(Class beanClass, CdiLifecycleManagementStrategy strategy) { this.beanClass = beanClass; + this.strategy = strategy; } @Override @@ -67,56 +66,50 @@ public class ManagedBeanRegistryCdiDelayedImpl extends AbstractManagedBeanRegist @Override public T getBeanInstance() { - if ( !initialized ) { + if ( delegate == null ) { initialize(); } - return beanInstance; + return delegate.getBeanInstance(); } private void initialize() { - log.debug( "Delayed initialization of CDI bean on first use : " + beanClass ); + log.debugf( "Delayed initialization of CDI bean on first use : %s", beanClass.getName() ); - final AnnotatedType annotatedType = beanManager.createAnnotatedType( beanClass ); - this.injectionTarget = beanManager.createInjectionTarget( annotatedType ); - this.creationContext = beanManager.createCreationalContext( null ); - - this.beanInstance = injectionTarget.produce( creationContext ); - injectionTarget.inject( this.beanInstance, creationContext ); - - injectionTarget.postConstruct( this.beanInstance ); - - this.initialized = true; + delegate = strategy.createBean( beanManager, beanClass ); } @Override public void release() { - if ( !initialized ) { - log.debug( "Skipping release for (delayed) CDI bean [" + beanClass + "] as it was not initialized" ); + if ( delegate == null ) { + log.debugf( "Skipping release for (delayed) CDI bean [%s] as it was not initialized", beanClass.getName() ); return; } - log.debug( "Releasing (delayed) CDI bean : " + beanClass ); + log.debugf( "Releasing (delayed) CDI bean : %s", beanClass.getName() ); - injectionTarget.preDestroy( beanInstance ); - injectionTarget.dispose( beanInstance ); - creationContext.release(); - - initialized = false; + delegate.release(); + delegate = null; } } - private class NamedManagedBeanImpl implements ManagedBean { + /** + * A named {@link ManagedBean} that is lazily initialized upon the first call to {@link #getBeanInstance()}. + * + * @param The type of bean instances + * + * @see ManagedBeanRegistryCdiExtendedImpl.LazilyInitializedManagedBeanImpl + */ + private class LazilyInitializedNamedManagedBeanImpl implements ManagedBean { private final String beanName; private final Class beanContract; + private final CdiLifecycleManagementStrategy strategy; - private boolean initialized = false; + private ManagedBean delegate = null; - private CreationalContext creationContext; - private T beanInstance; - - NamedManagedBeanImpl(String beanName, Class beanContract) { + LazilyInitializedNamedManagedBeanImpl(String beanName, Class beanContract, CdiLifecycleManagementStrategy strategy) { this.beanName = beanName; this.beanContract = beanContract; + this.strategy = strategy; } @Override @@ -126,33 +119,29 @@ public class ManagedBeanRegistryCdiDelayedImpl extends AbstractManagedBeanRegist @Override public T getBeanInstance() { - if ( !initialized ) { + if ( delegate == null ) { initialize(); } - return beanInstance; + return delegate.getBeanInstance(); } private void initialize() { - final Bean bean = Helper.INSTANCE.getNamedBean( beanName, beanContract, beanManager ); + log.debugf( "Delayed initialization of CDI bean on first use : [%s : %s]", beanName, beanContract.getName() ); - this.creationContext = beanManager.createCreationalContext( bean ); - this.beanInstance = beanContract.cast( beanManager.getReference( bean, beanContract, creationContext ) ); - - this.initialized = true; + delegate = strategy.createBean( beanManager, beanName, beanContract ); } @Override public void release() { - if ( !initialized ) { + if ( delegate == null ) { log.debugf( "Skipping release for (delayed) CDI bean [%s : %s] as it was not initialized", beanName, beanContract.getName() ); return; } log.debugf( "Releasing (delayed) CDI bean [%s : %s]", beanName, beanContract.getName() ); - creationContext.release(); - - initialized = false; + delegate.release(); + delegate = null; } } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiExtendedImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiExtendedImpl.java index 0b1362109f..757df3e36d 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiExtendedImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiExtendedImpl.java @@ -6,11 +6,7 @@ */ package org.hibernate.resource.beans.internal; -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.AnnotatedType; -import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.InjectionTarget; import org.hibernate.resource.beans.spi.AbstractManagedBeanRegistry; import org.hibernate.resource.beans.spi.ExtendedBeanManager; @@ -44,12 +40,12 @@ public class ManagedBeanRegistryCdiExtendedImpl @Override protected ManagedBean createBean(Class beanClass) { - return new ManagedBeanImpl<>( beanClass ); + return new LazilyInitializedManagedBeanImpl<>( beanClass, JpaCdiLifecycleManagementStrategy.INSTANCE ); } @Override protected ManagedBean createBean(String beanName, Class beanContract) { - return new NamedManagedBeanImpl<>( beanName, beanContract ); + return new LazilyInitializedNamedManagedBeanImpl<>( beanName, beanContract, StandardCdiLifecycleManagementStrategy.INSTANCE ); } @Override @@ -67,17 +63,24 @@ public class ManagedBeanRegistryCdiExtendedImpl return usableBeanManager; } - private class ManagedBeanImpl implements ManagedBean { + /** + * A {@link ManagedBean} that is lazily initialized upon the first call to {@link #getBeanInstance()}, + * relying on a {@link CdiLifecycleManagementStrategy} to initialize a delegate. + * + * Note that initialization is forced by the {@link ManagedBeanRegistryCdiExtendedImpl} + * as soon as the bean manager is initialized, see {@link #beanManagerInitialized(BeanManager)}. + * + * @param The type of bean instances + */ + private class LazilyInitializedManagedBeanImpl implements ManagedBean { private final Class beanClass; + private final CdiLifecycleManagementStrategy strategy; - private boolean initialized = false; + private ManagedBean delegate = null; - private InjectionTarget injectionTarget; - private CreationalContext creationContext; - private T beanInstance; - - private ManagedBeanImpl(Class beanClass) { + LazilyInitializedManagedBeanImpl(Class beanClass, CdiLifecycleManagementStrategy strategy) { this.beanClass = beanClass; + this.strategy = strategy; } @Override @@ -87,54 +90,48 @@ public class ManagedBeanRegistryCdiExtendedImpl @Override public T getBeanInstance() { - if ( !initialized ) { + if ( delegate == null ) { initialize(); } - return beanInstance; + return delegate.getBeanInstance(); } private void initialize() { - final BeanManager beanManager = getUsableBeanManager(); - AnnotatedType annotatedType = beanManager.createAnnotatedType( beanClass ); - this.injectionTarget = beanManager.createInjectionTarget( annotatedType ); - this.creationContext = beanManager.createCreationalContext( null ); - - this.beanInstance = injectionTarget.produce( creationContext ); - injectionTarget.inject( this.beanInstance, creationContext ); - - injectionTarget.postConstruct( this.beanInstance ); - - this.initialized = true; + delegate = strategy.createBean( getUsableBeanManager(), beanClass ); } + @Override public void release() { - if ( !initialized ) { + if ( delegate == null ) { log.debugf( "Skipping release for (extended) CDI bean [%s] as it was not initialized", beanClass.getName() ); return; } - log.debugf( "Releasing (extended) CDI bean [%s]", beanClass.getName() ); + log.debugf( "Releasing (extended) CDI bean : %s", beanClass.getName() ); - injectionTarget.preDestroy( beanInstance ); - injectionTarget.dispose( beanInstance ); - creationContext.release(); + delegate.release(); + delegate = null; } } - private class NamedManagedBeanImpl implements ManagedBean { + /** + * A named {@link ManagedBean} that is lazily initialized upon the first call to {@link #getBeanInstance()}. + * + * @param The type of bean instances + * + * @see LazilyInitializedManagedBeanImpl + */ + private class LazilyInitializedNamedManagedBeanImpl implements ManagedBean { private final String beanName; private final Class beanContract; + private final CdiLifecycleManagementStrategy strategy; - private boolean initialized = false; + private ManagedBean delegate = null; - private CreationalContext creationContext; - private T beanInstance; - - public NamedManagedBeanImpl( - String beanName, - Class beanContract) { + LazilyInitializedNamedManagedBeanImpl(String beanName, Class beanContract, CdiLifecycleManagementStrategy strategy) { this.beanName = beanName; this.beanContract = beanContract; + this.strategy = strategy; } @Override @@ -144,35 +141,28 @@ public class ManagedBeanRegistryCdiExtendedImpl @Override public T getBeanInstance() { - if ( !initialized ) { + if ( delegate == null ) { initialize(); } - - return beanInstance; + return delegate.getBeanInstance(); } private void initialize() { - final BeanManager beanManager = getUsableBeanManager(); - final Bean bean = Helper.INSTANCE.getNamedBean( beanName, beanContract, beanManager ); - - this.creationContext = beanManager.createCreationalContext( bean ); - this.beanInstance = beanContract.cast( beanManager.getReference( bean, beanContract, creationContext ) ); - - this.initialized = true; + delegate = strategy.createBean( getUsableBeanManager(), beanName, beanContract ); } @Override public void release() { - if ( !initialized ) { + if ( delegate == null ) { log.debugf( "Skipping release for (extended) CDI bean [%s : %s] as it was not initialized", beanName, beanContract.getName() ); return; } log.debugf( "Releasing (extended) CDI bean [%s : %s]", beanName, beanContract.getName() ); - creationContext.release(); - - initialized = false; + delegate.release(); + delegate = null; } } + } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiStandardImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiStandardImpl.java index 2d572cb78f..4a6263f37c 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiStandardImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/ManagedBeanRegistryCdiStandardImpl.java @@ -6,11 +6,7 @@ */ package org.hibernate.resource.beans.internal; -import javax.enterprise.context.spi.CreationalContext; -import javax.enterprise.inject.spi.AnnotatedType; -import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.inject.spi.InjectionTarget; import org.hibernate.resource.beans.spi.AbstractManagedBeanRegistry; import org.hibernate.resource.beans.spi.ManagedBean; @@ -39,103 +35,11 @@ class ManagedBeanRegistryCdiStandardImpl extends AbstractManagedBeanRegistry { @Override protected ManagedBean createBean(Class beanClass) { - return new ManagedBeanImpl<>( beanClass ); + return JpaCdiLifecycleManagementStrategy.INSTANCE.createBean( beanManager, beanClass ); } @Override protected ManagedBean createBean(String beanName, Class beanContract) { - return new NamedManagedBeanImpl<>( beanName, beanContract ); - } - - private class ManagedBeanImpl implements ManagedBean { - private final Class beanClass; - private final InjectionTarget injectionTarget; - - private boolean initialized; - - private final CreationalContext creationContext; - private final T beanInstance; - - public ManagedBeanImpl(Class beanClass) { - this.beanClass = beanClass; - - AnnotatedType annotatedType = beanManager.createAnnotatedType( beanClass ); - this.injectionTarget = beanManager.createInjectionTarget( annotatedType ); - this.creationContext = beanManager.createCreationalContext( null ); - - this.beanInstance = injectionTarget.produce( creationContext ); - injectionTarget.inject( this.beanInstance, creationContext ); - - injectionTarget.postConstruct( this.beanInstance ); - } - - @Override - public Class getBeanClass() { - return beanClass; - } - - @Override - public T getBeanInstance() { - return beanInstance; - } - - @Override - public void release() { - if ( !initialized ) { - log.debugf( "Skipping release for (standard) CDI bean [%s] as it was not initialized", beanClass.getName() ); - return; - } - - log.debugf( "Releasing (standard) CDI bean [%s]", beanClass.getName() ); - - injectionTarget.preDestroy( beanInstance ); - injectionTarget.dispose( beanInstance ); - creationContext.release(); - } - } - - private class NamedManagedBeanImpl implements ManagedBean { - private final String beanName; - private final Class beanContract; - - private boolean initialized; - - private CreationalContext creationContext; - private T beanInstance; - - private NamedManagedBeanImpl(String beanName, Class beanContract) { - this.beanName = beanName; - this.beanContract = beanContract; - - final Bean bean = Helper.INSTANCE.getNamedBean( beanName, beanContract, beanManager ); - - this.creationContext = beanManager.createCreationalContext( bean ); - this.beanInstance = beanContract.cast( beanManager.getReference( bean, beanContract, creationContext ) ); - - this.initialized = true; - } - @Override - public Class getBeanClass() { - return beanContract; - } - - @Override - public T getBeanInstance() { - return beanInstance; - } - - @Override - public void release() { - if ( !initialized ) { - log.debugf( "Skipping release for (standard) CDI bean [%s : %s] as it was not initialized", beanName, beanContract.getName() ); - return; - } - - log.debugf( "Releasing (standard) CDI bean [%s : %s]", beanName, beanContract.getName() ); - - creationContext.release(); - - initialized = false; - } + return StandardCdiLifecycleManagementStrategy.INSTANCE.createBean( beanManager, beanName, beanContract ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/StandardCdiLifecycleManagementStrategy.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/StandardCdiLifecycleManagementStrategy.java new file mode 100644 index 0000000000..33dda2d715 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/StandardCdiLifecycleManagementStrategy.java @@ -0,0 +1,75 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.resource.beans.internal; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; + +import org.hibernate.resource.beans.spi.ManagedBean; + +class StandardCdiLifecycleManagementStrategy implements CdiLifecycleManagementStrategy { + + static final StandardCdiLifecycleManagementStrategy INSTANCE = new StandardCdiLifecycleManagementStrategy(); + + private StandardCdiLifecycleManagementStrategy() { + // private constructor, do not use + } + + @Override + public ManagedBean createBean(BeanManager beanManager, Class beanClass) { + Bean bean = Helper.INSTANCE.getBean( beanClass, beanManager ); + + // Pass the bean to createCreationalContext here so that an existing instance can be returned + CreationalContext creationalContext = beanManager.createCreationalContext( bean ); + + T beanInstance = bean.create( creationalContext ); + + return new BeanManagerManagedBeanImpl<>( beanClass, creationalContext, beanInstance ); + } + + @Override + public ManagedBean createBean(BeanManager beanManager, String beanName, Class beanClass) { + Bean bean = Helper.INSTANCE.getNamedBean( beanName, beanClass, beanManager ); + + // Pass the bean to createCreationalContext here so that an existing instance can be returned + CreationalContext creationalContext = beanManager.createCreationalContext( bean ); + + T beanInstance = bean.create( creationalContext ); + + return new BeanManagerManagedBeanImpl<>( beanClass, creationalContext, beanInstance ); + } + + private static class BeanManagerManagedBeanImpl implements ManagedBean { + private final Class beanClass; + private final CreationalContext creationContext; + private final T beanInstance; + + private BeanManagerManagedBeanImpl( + Class beanClass, + CreationalContext creationContext, T beanInstance) { + this.beanClass = beanClass; + this.creationContext = creationContext; + this.beanInstance = beanInstance; + } + + @Override + public Class getBeanClass() { + return beanClass; + } + + @Override + public T getBeanInstance() { + return beanInstance; + } + + @Override + public void release() { + creationContext.release(); + } + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/UnnamedRegistryScopedManagedBeanImpl.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/UnnamedRegistryScopedManagedBeanImpl.java new file mode 100644 index 0000000000..1974031057 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/UnnamedRegistryScopedManagedBeanImpl.java @@ -0,0 +1,53 @@ +/* + * Hibernate Search, full-text search for your domain model + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.resource.beans.internal; + +import javax.enterprise.context.spi.CreationalContext; +import javax.enterprise.inject.spi.AnnotatedType; +import javax.enterprise.inject.spi.BeanManager; +import javax.enterprise.inject.spi.InjectionTarget; + +import org.hibernate.resource.beans.spi.ManagedBean; + +class UnnamedRegistryScopedManagedBeanImpl implements ManagedBean { + + private final Class beanClass; + + private final InjectionTarget injectionTarget; + private final CreationalContext creationContext; + private T beanInstance; + + UnnamedRegistryScopedManagedBeanImpl(BeanManager beanManager, Class beanClass) { + this.beanClass = beanClass; + + final AnnotatedType annotatedType = beanManager.createAnnotatedType( beanClass ); + this.injectionTarget = beanManager.createInjectionTarget( annotatedType ); + this.creationContext = beanManager.createCreationalContext( null ); + + this.beanInstance = injectionTarget.produce( creationContext ); + injectionTarget.inject( this.beanInstance, creationContext ); + + injectionTarget.postConstruct( this.beanInstance ); + } + + @Override + public Class getBeanClass() { + return beanClass; + } + + @Override + public T getBeanInstance() { + return beanInstance; + } + + @Override + public void release() { + injectionTarget.preDestroy( beanInstance ); + injectionTarget.dispose( beanInstance ); + creationContext.release(); + } +}