Search in sources :

Example 6 with SeContainer

use of javax.enterprise.inject.se.SeContainer 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 7 with SeContainer

use of javax.enterprise.inject.se.SeContainer in project hibernate-orm by hibernate.

the class BasicCdiTest method testIt.

@Test
@SuppressWarnings("unchecked")
public void testIt() {
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(Monitor.class, EventQueue.class, Event.class);
    count = 0;
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).applySetting(AvailableSettings.DELAY_CDI_ACCESS, "true").applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).build();
        final SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(MyEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        try {
            inTransaction(sessionFactory, session -> session.persist(new MyEntity(1)));
            assertEquals(1, count);
            inTransaction(sessionFactory, session -> {
                MyEntity it = session.find(MyEntity.class, 1);
                assertNotNull(it);
            });
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete MyEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 8 with SeContainer

use of javax.enterprise.inject.se.SeContainer in project meecrowave by apache.

the class MeecrowaveSeContainerInitializerTest method run.

@Test
public void run() {
    try (final SeContainer container = SeContainerInitializer.newInstance().addProperty("httpPort", new Meecrowave.Builder().randomHttpPort().getHttpPort()).disableDiscovery().addBeanClasses(Configured.class).initialize()) {
        final Client client = ClientBuilder.newClient();
        assertNotNull(container.select(Meecrowave.class).get());
        assertEquals("configured", client.target(String.format("http://localhost:%d/configured", container.select(Meecrowave.Builder.class).get().getHttpPort())).request(TEXT_PLAIN_TYPE).get(String.class));
    }
}
Also used : ClientBuilder(javax.ws.rs.client.ClientBuilder) SeContainer(javax.enterprise.inject.se.SeContainer) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 9 with SeContainer

use of javax.enterprise.inject.se.SeContainer in project hibernate-orm by hibernate.

the class NonRegistryManagedExtendedCdiSupportTest method testIt.

@Test
public void testIt() {
    Monitor.reset();
    final ExtendedBeanManagerImpl standIn = new ExtendedBeanManagerImpl();
    final TheFallbackBeanInstanceProducer fallbackBeanInstanceProducer = new TheFallbackBeanInstanceProducer();
    final NonRegistryManagedBeanConsumingIntegrator beanConsumingIntegrator = new NonRegistryManagedBeanConsumingIntegrator(fallbackBeanInstanceProducer);
    try (SessionFactoryImplementor sessionFactory = buildSessionFactory(standIn, beanConsumingIntegrator)) {
        final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(TheApplicationScopedBean.class).addBeanClasses(TheNamedApplicationScopedBean.class, TheMainNamedApplicationScopedBeanImpl.class, TheAlternativeNamedApplicationScopedBeanImpl.class).addBeanClasses(TheSharedApplicationScopedBean.class).addBeanClasses(TheDependentBean.class).addBeanClasses(TheNamedDependentBean.class, TheMainNamedDependentBeanImpl.class, TheAlternativeNamedDependentBeanImpl.class).addBeanClasses(TheNestedDependentBean.class).addBeanClasses(TheNonHibernateBeanConsumer.class);
        try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
            // Simulate CDI bean consumers outside of Hibernate ORM
            Instance<TheNonHibernateBeanConsumer> nonHibernateBeanConsumerInstance = cdiContainer.getBeanManager().createInstance().select(TheNonHibernateBeanConsumer.class);
            nonHibernateBeanConsumerInstance.get();
            // Here, the NonRegistryManagedBeanConsumingIntegrator has just been integrated and has requested beans
            // BUT it has not fetched instances of beans yet, so non-shared beans should not have been instantiated yet.
            assertEquals(0, Monitor.theApplicationScopedBean().currentInstantiationCount());
            assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
            assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
            assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
            assertEquals(0, Monitor.theDependentBean().currentInstantiationCount());
            assertEquals(0, Monitor.theMainNamedDependentBean().currentInstantiationCount());
            assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
            assertEquals(0, fallbackBeanInstanceProducer.currentInstantiationCount());
            assertEquals(0, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
            // Nested dependent bean: 1 instance per bean that depends on it
            assertEquals(1, Monitor.theNestedDependentBean().currentInstantiationCount());
            standIn.beanManagerReady(cdiContainer.getBeanManager());
            beanConsumingIntegrator.ensureInstancesInitialized();
            // Here the NonRegistryManagedBeanConsumingIntegrator *did* fetch an instance of each bean,
            // so all beans should have been instantiated.
            // See NonRegistryManagedBeanConsumingIntegrator for a detailed list of requested beans
            // Application scope: maximum 1 instance as soon as at least one was requested
            assertEquals(1, Monitor.theApplicationScopedBean().currentInstantiationCount());
            assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentInstantiationCount());
            assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentInstantiationCount());
            assertEquals(1, Monitor.theSharedApplicationScopedBean().currentInstantiationCount());
            // Dependent scope: 1 instance per bean we requested explicitly
            assertEquals(2, Monitor.theDependentBean().currentInstantiationCount());
            assertEquals(2, Monitor.theMainNamedDependentBean().currentInstantiationCount());
            assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentInstantiationCount());
            // Reflection-instantiated: 1 instance per bean we requested explicitly
            assertEquals(2, fallbackBeanInstanceProducer.currentInstantiationCount());
            assertEquals(2, fallbackBeanInstanceProducer.currentNamedInstantiationCount());
            // Nested dependent bean: 1 instance per bean that depends on it
            assertEquals(7, Monitor.theNestedDependentBean().currentInstantiationCount());
            // Expect one PostConstruct call per CDI bean instance
            assertEquals(1, Monitor.theApplicationScopedBean().currentPostConstructCount());
            assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPostConstructCount());
            assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPostConstructCount());
            assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPostConstructCount());
            assertEquals(2, Monitor.theDependentBean().currentPostConstructCount());
            assertEquals(2, Monitor.theMainNamedDependentBean().currentPostConstructCount());
            assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPostConstructCount());
            assertEquals(7, Monitor.theNestedDependentBean().currentPostConstructCount());
            // Expect no PreDestroy call yet
            assertEquals(0, Monitor.theApplicationScopedBean().currentPreDestroyCount());
            assertEquals(0, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
            assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
            assertEquals(0, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
            assertEquals(0, Monitor.theDependentBean().currentPreDestroyCount());
            assertEquals(0, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
            assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
            assertEquals(0, Monitor.theNestedDependentBean().currentPreDestroyCount());
        }
        // After the CDI context has ended, PreDestroy should have been called on every "normal-scoped" CDI bean
        // (i.e. all CDI beans excepting the dependent ones we requested explicitly and haven't released yet)
        assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
        assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
        assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
        assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
        assertEquals(0, Monitor.theDependentBean().currentPreDestroyCount());
        assertEquals(0, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
        assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
        assertEquals(3, Monitor.theNestedDependentBean().currentPreDestroyCount());
    }
    // Here, the NonRegistryManagedBeanConsumingIntegrator has just been disintegrated and has released beans
    // The dependent beans should now have been released as well.
    assertEquals(1, Monitor.theApplicationScopedBean().currentPreDestroyCount());
    assertEquals(1, Monitor.theMainNamedApplicationScopedBean().currentPreDestroyCount());
    assertEquals(0, Monitor.theAlternativeNamedApplicationScopedBean().currentPreDestroyCount());
    assertEquals(1, Monitor.theSharedApplicationScopedBean().currentPreDestroyCount());
    assertEquals(2, Monitor.theDependentBean().currentPreDestroyCount());
    assertEquals(2, Monitor.theMainNamedDependentBean().currentPreDestroyCount());
    assertEquals(0, Monitor.theAlternativeNamedDependentBean().currentPreDestroyCount());
    assertEquals(7, Monitor.theNestedDependentBean().currentPreDestroyCount());
}
Also used : TheNestedDependentBean(org.hibernate.test.cdi.general.nonregistrymanaged.TheNestedDependentBean) TheNonHibernateBeanConsumer(org.hibernate.test.cdi.general.nonregistrymanaged.TheNonHibernateBeanConsumer) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) TheDependentBean(org.hibernate.test.cdi.general.nonregistrymanaged.TheDependentBean) TheAlternativeNamedApplicationScopedBeanImpl(org.hibernate.test.cdi.general.nonregistrymanaged.TheAlternativeNamedApplicationScopedBeanImpl) NonRegistryManagedBeanConsumingIntegrator(org.hibernate.test.cdi.general.nonregistrymanaged.NonRegistryManagedBeanConsumingIntegrator) TheFallbackBeanInstanceProducer(org.hibernate.test.cdi.general.nonregistrymanaged.TheFallbackBeanInstanceProducer) TheMainNamedApplicationScopedBeanImpl(org.hibernate.test.cdi.general.nonregistrymanaged.TheMainNamedApplicationScopedBeanImpl) TheNamedApplicationScopedBean(org.hibernate.test.cdi.general.nonregistrymanaged.TheNamedApplicationScopedBean) SeContainer(javax.enterprise.inject.se.SeContainer) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) Test(org.junit.Test)

Example 10 with SeContainer

use of javax.enterprise.inject.se.SeContainer in project hibernate-orm by hibernate.

the class CdiHostedConverterTest method testIt.

@Test
public void testIt() {
    MonitorBean.reset();
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(MonitorBean.class, ConverterBean.class);
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        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 SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        // The CDI bean should have been built immediately...
        assertTrue(MonitorBean.wasInstantiated());
        assertEquals(0, MonitorBean.currentFromDbCount());
        assertEquals(0, MonitorBean.currentToDbCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1, "me", 5)));
            assertEquals(0, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
            inTransaction(sessionFactory, session -> {
                TheEntity it = session.find(TheEntity.class, 1);
                assertNotNull(it);
            });
            assertEquals(1, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : TheEntity(org.hibernate.test.cdi.converters.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

SeContainer (javax.enterprise.inject.se.SeContainer)13 Test (org.junit.Test)13 SeContainerInitializer (javax.enterprise.inject.se.SeContainerInitializer)9 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)9 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)9 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)9 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)8 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)8 MetadataSources (org.hibernate.boot.MetadataSources)6 BeanContainer (org.hibernate.resource.beans.container.spi.BeanContainer)3 TheEntity (org.hibernate.test.cdi.events.TheEntity)3 NonRegistryManagedBeanConsumingIntegrator (org.hibernate.test.cdi.general.nonregistrymanaged.NonRegistryManagedBeanConsumingIntegrator)3 TheAlternativeNamedApplicationScopedBeanImpl (org.hibernate.test.cdi.general.nonregistrymanaged.TheAlternativeNamedApplicationScopedBeanImpl)3 TheDependentBean (org.hibernate.test.cdi.general.nonregistrymanaged.TheDependentBean)3 TheFallbackBeanInstanceProducer (org.hibernate.test.cdi.general.nonregistrymanaged.TheFallbackBeanInstanceProducer)3 TheMainNamedApplicationScopedBeanImpl (org.hibernate.test.cdi.general.nonregistrymanaged.TheMainNamedApplicationScopedBeanImpl)3 TheNamedApplicationScopedBean (org.hibernate.test.cdi.general.nonregistrymanaged.TheNamedApplicationScopedBean)3 TheNestedDependentBean (org.hibernate.test.cdi.general.nonregistrymanaged.TheNestedDependentBean)3 TheNonHibernateBeanConsumer (org.hibernate.test.cdi.general.nonregistrymanaged.TheNonHibernateBeanConsumer)3 TheEntity (org.hibernate.test.cdi.converters.TheEntity)2