Search in sources :

Example 1 with BeanManager

use of jakarta.enterprise.inject.spi.BeanManager in project hibernate-orm by hibernate.

the class ExtendedMixedAccessTest method doTest.

private void doTest(TestingExtendedBeanManager extendedBeanManager) {
    try (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 : CdiBeanContainerExtendedAccessImpl(org.hibernate.resource.beans.container.internal.CdiBeanContainerExtendedAccessImpl) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ManagedBeanRegistry(org.hibernate.resource.beans.spi.ManagedBeanRegistry) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) SeContainer(jakarta.enterprise.inject.se.SeContainer) TestingExtendedBeanManager(org.hibernate.orm.test.cdi.testsupport.TestingExtendedBeanManager) BeanManager(jakarta.enterprise.inject.spi.BeanManager) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 2 with BeanManager

use of jakarta.enterprise.inject.spi.BeanManager in project resteasy by resteasy.

the class CdiInjectorFactory method lookupBeanManagerViaServletContext.

private static BeanManager lookupBeanManagerViaServletContext() {
    BeanManager beanManager = null;
    try {
        // Look for BeanManager in ServletContext
        ServletContext servletContext = ResteasyContext.getContextData(ServletContext.class);
        // null check for RESTEASY-1009
        if (servletContext != null) {
            beanManager = (BeanManager) servletContext.getAttribute(BEAN_MANAGER_ATTRIBUTE_PREFIX + BeanManager.class.getName());
            if (beanManager != null) {
                LogMessages.LOGGER.debug(Messages.MESSAGES.foundBeanManagerInServletContext());
                return beanManager;
            }
            // Look for BeanManager in ServletContext (the old attribute name for backwards compatibility)
            beanManager = (BeanManager) servletContext.getAttribute(BeanManager.class.getName());
            if (beanManager != null) {
                LogMessages.LOGGER.debug(Messages.MESSAGES.foundBeanManagerInServletContext());
                return beanManager;
            }
        }
    } catch (NoClassDefFoundError e) {
        LogMessages.LOGGER.debug(Messages.MESSAGES.unableToFindServletContextClass(), e);
    } catch (Exception e) {
        LogMessages.LOGGER.debug(Messages.MESSAGES.errorOccurredLookingUpServletContext(), e);
    }
    return beanManager;
}
Also used : ServletContext(jakarta.servlet.ServletContext) BeanManager(jakarta.enterprise.inject.spi.BeanManager) NamingException(javax.naming.NamingException)

Example 3 with BeanManager

use of jakarta.enterprise.inject.spi.BeanManager in project resteasy by resteasy.

the class WeldInitCDITest method testInitWeld.

/**
 * @tpTestDetails Initialized weld should enable lookupBeanManagerCDIUtil method to work correctly.
 * @tpSince RESTEasy 3.0.16
 */
@Test
public void testInitWeld() {
    Weld weld = new Weld();
    // next assert fails without initialized weld
    WeldContainer weldContainer = weld.initialize();
    BeanManager bm = CdiInjectorFactory.lookupBeanManagerCDIUtil();
    Assert.assertNotNull("Bean manager was not initialized successfully", bm);
    weld.shutdown();
}
Also used : WeldContainer(org.jboss.weld.environment.se.WeldContainer) BeanManager(jakarta.enterprise.inject.spi.BeanManager) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 4 with BeanManager

use of jakarta.enterprise.inject.spi.BeanManager in project core by weld.

the class InjectWeldManagerTest method testWeldManagerInjection.

@Test
public void testWeldManagerInjection() {
    try (WeldContainer container = new Weld().initialize()) {
        WeldInstance<WeldManager> managerInstance = container.select(WeldManager.class);
        Assert.assertTrue(managerInstance.isResolvable());
        // try some call on it
        managerInstance.get().createAnnotatedType(SomeBean.class);
        // verify that BeanManager and BeanManagerImpl are still injectable as well
        WeldInstance<BeanManager> bmInstance = container.select(BeanManager.class);
        Assert.assertTrue(bmInstance.isResolvable());
        WeldInstance<BeanManagerImpl> bmImplInstance = container.select(BeanManagerImpl.class);
        Assert.assertTrue(bmImplInstance.isResolvable());
    }
}
Also used : BeanManagerImpl(org.jboss.weld.manager.BeanManagerImpl) WeldContainer(org.jboss.weld.environment.se.WeldContainer) BeanManager(jakarta.enterprise.inject.spi.BeanManager) Weld(org.jboss.weld.environment.se.Weld) WeldManager(org.jboss.weld.manager.api.WeldManager) Test(org.junit.Test)

Example 5 with BeanManager

use of jakarta.enterprise.inject.spi.BeanManager in project core by weld.

the class War1Listener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    // validate accessibility from the war1 module
    Set<Bean<?>> accessibleImplementations = manager.getBeans(Animal.class);
    assertEquals(accessibleImplementations.size(), 4);
    assertTrue(containsBean(accessibleImplementations, War1Impl.class));
    assertTrue(containsBean(accessibleImplementations, Library1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the war1 library module
    Bean<?> library1ImplBean = getUniqueBean(accessibleImplementations, Library1Impl.class);
    Library1Impl library1Impl = (Library1Impl) manager.getReference(library1ImplBean, Animal.class, manager.createCreationalContext(library1ImplBean));
    BeanManager library1BeanManager = library1Impl.getBeanManager();
    accessibleImplementations = library1BeanManager.getBeans(Animal.class);
    assertEquals(accessibleImplementations.size(), 4);
    assertTrue(containsBean(accessibleImplementations, War1Impl.class));
    assertTrue(containsBean(accessibleImplementations, Library1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the shared library 1
    Bean<?> sharedLibrary1ImplBean = getUniqueBean(accessibleImplementations, SharedLibrary1Impl.class);
    SharedLibrary1Impl sharedLibrary1Impl = (SharedLibrary1Impl) manager.getReference(sharedLibrary1ImplBean, Animal.class, manager.createCreationalContext(sharedLibrary1ImplBean));
    BeanManager sharedLibrary1BeanManager = sharedLibrary1Impl.getBeanManager();
    accessibleImplementations = sharedLibrary1BeanManager.getBeans(Animal.class);
    // implementations within wars are not accessible
    assertEquals(accessibleImplementations.size(), 2);
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
    // validate accessibility from the shared library 2
    Bean<?> sharedLibrary2ImplBean = getUniqueBean(accessibleImplementations, SharedLibrary2Impl.class);
    SharedLibrary2Impl sharedLibrary2Impl = (SharedLibrary2Impl) manager.getReference(sharedLibrary2ImplBean, Animal.class, manager.createCreationalContext(sharedLibrary2ImplBean));
    BeanManager sharedLibrary2BeanManager = sharedLibrary2Impl.getBeanManager();
    accessibleImplementations = sharedLibrary2BeanManager.getBeans(Animal.class);
    // implementations within wars are not accessible
    assertEquals(accessibleImplementations.size(), 2);
    assertTrue(containsBean(accessibleImplementations, SharedLibrary1Impl.class));
    assertTrue(containsBean(accessibleImplementations, SharedLibrary2Impl.class));
}
Also used : BeanManager(jakarta.enterprise.inject.spi.BeanManager) Bean(jakarta.enterprise.inject.spi.Bean)

Aggregations

BeanManager (jakarta.enterprise.inject.spi.BeanManager)91 Bean (jakarta.enterprise.inject.spi.Bean)19 Test (org.junit.Test)15 Test (org.junit.jupiter.api.Test)10 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)9 FacesContext (jakarta.faces.context.FacesContext)9 TestContainer (org.jboss.arquillian.container.weld.embedded.mock.TestContainer)9 NamingException (javax.naming.NamingException)8 Test (org.testng.annotations.Test)8 Map (java.util.Map)7 Set (java.util.Set)7 InitialContext (javax.naming.InitialContext)7 Observes (jakarta.enterprise.event.Observes)6 Annotation (java.lang.annotation.Annotation)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 WeldContainer (org.jboss.weld.environment.se.WeldContainer)6 ServerInterceptor (io.grpc.ServerInterceptor)5 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)5 BeanDeploymentArchiveImpl (org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl)5