Search in sources :

Example 1 with BeanContainer

use of org.hibernate.resource.beans.container.spi.BeanContainer in project hibernate-orm by hibernate.

the class DelayedMixedAccessTest method testDelayedMixedAccess.

@Test
public void testDelayedMixedAccess() {
    try (final SeContainer cdiContainer = Helper.createSeContainer()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).applySetting(AvailableSettings.DELAY_CDI_ACCESS, "true").build();
        final BeanContainer beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(cdiContainer.getBeanManager(), ssr);
        assertThat(beanContainer, instanceOf(CdiBeanContainerDelayedAccessImpl.class));
        final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(HostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(hostedBean, notNullValue());
        assertThat(hostedBean.getBeanInstance(), notNullValue());
        assertThat(hostedBean.getBeanInstance().getInjectedHostedBean(), notNullValue());
        final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(NonHostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(nonHostedBean, notNullValue());
        assertThat(nonHostedBean.getBeanInstance(), notNullValue());
    }
}
Also used : CdiBeanContainerDelayedAccessImpl(org.hibernate.resource.beans.container.internal.CdiBeanContainerDelayedAccessImpl) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 2 with BeanContainer

use of org.hibernate.resource.beans.container.spi.BeanContainer in project hibernate-orm by hibernate.

the class ImmediateMixedAccessTests method testImmediateMixedAccess.

@Test
public void testImmediateMixedAccess() {
    try (final SeContainer cdiContainer = Helper.createSeContainer()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).build();
        final BeanContainer beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(cdiContainer.getBeanManager(), ssr);
        assertThat(beanContainer, instanceOf(CdiBeanContainerImmediateAccessImpl.class));
        final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(HostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(hostedBean, notNullValue());
        assertThat(hostedBean.getBeanInstance(), notNullValue());
        assertThat(hostedBean.getBeanInstance().getInjectedHostedBean(), notNullValue());
        final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(NonHostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(nonHostedBean, notNullValue());
        assertThat(nonHostedBean.getBeanInstance(), notNullValue());
    }
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) CdiBeanContainerImmediateAccessImpl(org.hibernate.resource.beans.container.internal.CdiBeanContainerImmediateAccessImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 3 with BeanContainer

use of org.hibernate.resource.beans.container.spi.BeanContainer in project hibernate-orm by hibernate.

the class ExtendedMixedAccessTest method testExtendedMixedAccess.

@Test
public void testExtendedMixedAccess() {
    final Helper.TestingExtendedBeanManager extendedBeanManager = Helper.createExtendedBeanManager();
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, extendedBeanManager).build();
    final BeanContainer beanContainer = ssr.getService(ManagedBeanRegistry.class).getBeanContainer();
    assertThat(beanContainer, instanceOf(CdiBeanContainerExtendedAccessImpl.class));
    try (final SeContainer cdiContainer = Helper.createSeContainer()) {
        final BeanManager beanManager = cdiContainer.getBeanManager();
        extendedBeanManager.notifyListenerReady(beanManager);
        assertThat(beanManager, sameInstance(((CdiBeanContainerExtendedAccessImpl) beanContainer).getUsableBeanManager()));
        final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(HostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(hostedBean, notNullValue());
        assertThat(hostedBean.getBeanInstance(), notNullValue());
        assertThat(hostedBean.getBeanInstance().getInjectedHostedBean(), notNullValue());
        final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(NonHostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(nonHostedBean, notNullValue());
        assertThat(nonHostedBean.getBeanInstance(), notNullValue());
        extendedBeanManager.notifyListenerShuttingDown(beanManager);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) CdiBeanContainerExtendedAccessImpl(org.hibernate.resource.beans.container.internal.CdiBeanContainerExtendedAccessImpl) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) SeContainer(javax.enterprise.inject.se.SeContainer) BeanManager(javax.enterprise.inject.spi.BeanManager) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 4 with BeanContainer

use of org.hibernate.resource.beans.container.spi.BeanContainer in project hibernate-orm by hibernate.

the class NonRegistryManagedBeanConsumingIntegrator method integrate.

@Override
@SuppressWarnings("unchecked")
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    ManagedBeanRegistry registry = sessionFactory.getServiceRegistry().getService(ManagedBeanRegistry.class);
    BeanContainer beanContainer = registry.getBeanContainer();
    assertThat(beanContainer, CoreMatchers.notNullValue());
    applicationScopedBean1 = (ContainedBeanImplementor) beanContainer.getBean(TheApplicationScopedBean.class, this, fallbackBeanInstanceProducer);
    applicationScopedBean2 = (ContainedBeanImplementor) beanContainer.getBean(TheApplicationScopedBean.class, this, fallbackBeanInstanceProducer);
    dependentBean1 = (ContainedBeanImplementor) beanContainer.getBean(TheDependentBean.class, this, fallbackBeanInstanceProducer);
    dependentBean2 = (ContainedBeanImplementor) beanContainer.getBean(TheDependentBean.class, this, fallbackBeanInstanceProducer);
    reflectionInstantiatedBean1 = (ContainedBeanImplementor) beanContainer.getBean(TheReflectionInstantiatedBean.class, this, fallbackBeanInstanceProducer);
    reflectionInstantiatedBean2 = (ContainedBeanImplementor) beanContainer.getBean(TheReflectionInstantiatedBean.class, this, fallbackBeanInstanceProducer);
    namedApplicationScopedBean1 = (ContainedBeanImplementor) beanContainer.getBean(TheMainNamedApplicationScopedBeanImpl.NAME, TheNamedApplicationScopedBean.class, this, fallbackBeanInstanceProducer);
    namedApplicationScopedBean2 = (ContainedBeanImplementor) beanContainer.getBean(TheMainNamedApplicationScopedBeanImpl.NAME, TheNamedApplicationScopedBean.class, this, fallbackBeanInstanceProducer);
    namedDependentBean1 = (ContainedBeanImplementor) beanContainer.getBean(TheMainNamedDependentBeanImpl.NAME, TheNamedDependentBean.class, this, fallbackBeanInstanceProducer);
    namedDependentBean2 = (ContainedBeanImplementor) beanContainer.getBean(TheMainNamedDependentBeanImpl.NAME, TheNamedDependentBean.class, this, fallbackBeanInstanceProducer);
    namedReflectionInstantiatedBean1 = (ContainedBeanImplementor) beanContainer.getBean(TheReflectionInstantiatedBean.class.getName(), TheReflectionInstantiatedBean.class, this, fallbackBeanInstanceProducer);
    namedReflectionInstantiatedBean2 = (ContainedBeanImplementor) beanContainer.getBean(TheReflectionInstantiatedBean.class.getName(), TheReflectionInstantiatedBean.class, this, fallbackBeanInstanceProducer);
}
Also used : ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer)

Example 5 with BeanContainer

use of org.hibernate.resource.beans.container.spi.BeanContainer in project hibernate-orm by hibernate.

the class ManagedBeanRegistryInitiator method interpretExplicitBeanContainer.

private BeanContainer interpretExplicitBeanContainer(Object explicitSetting, ClassLoaderService classLoaderService, ServiceRegistryImplementor serviceRegistry) {
    if (explicitSetting == null) {
        return null;
    }
    if (explicitSetting instanceof BeanContainer) {
        return (BeanContainer) explicitSetting;
    }
    // otherwise we ultimately need to resolve this to a class
    final Class containerClass;
    if (explicitSetting instanceof Class) {
        containerClass = (Class) explicitSetting;
    } else {
        final String name = explicitSetting.toString();
        // try the StrategySelector service
        final Class selected = serviceRegistry.getService(StrategySelector.class).selectStrategyImplementor(BeanContainer.class, name);
        if (selected != null) {
            containerClass = selected;
        } else {
            containerClass = classLoaderService.classForName(name);
        }
    }
    try {
        return (BeanContainer) containerClass.newInstance();
    } catch (Exception e) {
        throw new InstantiationException("Unable to instantiate specified BeanContainer : " + containerClass.getName(), containerClass, e);
    }
}
Also used : BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) InstantiationException(org.hibernate.InstantiationException) InstantiationException(org.hibernate.InstantiationException)

Aggregations

BeanContainer (org.hibernate.resource.beans.container.spi.BeanContainer)13 Test (org.junit.jupiter.api.Test)8 NoDefinitionInSpringContextTestBean (org.springframework.orm.jpa.hibernate.beans.NoDefinitionInSpringContextTestBean)4 SeContainer (javax.enterprise.inject.se.SeContainer)3 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)3 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)3 Test (org.junit.Test)3 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)2 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)2 ManagedBeanRegistry (org.hibernate.resource.beans.spi.ManagedBeanRegistry)2 MultiplePrototypesInSpringContextTestBean (org.springframework.orm.jpa.hibernate.beans.MultiplePrototypesInSpringContextTestBean)2 SinglePrototypeInSpringContextTestBean (org.springframework.orm.jpa.hibernate.beans.SinglePrototypeInSpringContextTestBean)2 BeanManager (javax.enterprise.inject.spi.BeanManager)1 InstantiationException (org.hibernate.InstantiationException)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)1 CdiBeanContainerDelayedAccessImpl (org.hibernate.resource.beans.container.internal.CdiBeanContainerDelayedAccessImpl)1 CdiBeanContainerExtendedAccessImpl (org.hibernate.resource.beans.container.internal.CdiBeanContainerExtendedAccessImpl)1 CdiBeanContainerImmediateAccessImpl (org.hibernate.resource.beans.container.internal.CdiBeanContainerImmediateAccessImpl)1