Search in sources :

Example 1 with Dependent

use of jakarta.enterprise.context.Dependent in project core by weld.

the class BeanConfiguratorExtension method afterBeanDiscovery.

void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager manager) {
    event.addBean().scope(Dependent.class).addType(String.class).addQualifier(Juicy.Literal.INSTANCE).produceWith((i) -> {
        InjectionPoint ip = i.select(InjectionPoint.class).get();
        assertNotNull(ip);
        assertNotNull(ip.getBean());
        return ip.getBean().getBeanClass().getName();
    });
    event.addBean().scope(ApplicationScoped.class).addType(Map.class).addQualifier(Juicy.Literal.INSTANCE).produceWith((i) -> {
        try {
            i.select(InjectionPoint.class).get();
            fail("Cannot inject injection point metadata into non-dependent bean");
        } catch (IllegalArgumentException expected) {
        }
        return new HashMap<>();
    });
}
Also used : InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) HashMap(java.util.HashMap) Dependent(jakarta.enterprise.context.Dependent) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) IllegalArgumentException(org.jboss.weld.exceptions.IllegalArgumentException)

Example 2 with Dependent

use of jakarta.enterprise.context.Dependent in project core by weld.

the class InstanceImpl method destroy.

@Override
public void destroy(T instance) {
    checkNotNull(instance);
    // Attempt to destroy instance which is either a client proxy or a dependent session bean proxy
    if (instance instanceof ProxyObject) {
        ProxyObject proxy = (ProxyObject) instance;
        if (proxy.weld_getHandler() instanceof ProxyMethodHandler) {
            ProxyMethodHandler handler = (ProxyMethodHandler) proxy.weld_getHandler();
            Bean<?> bean = handler.getBean();
            if (isSessionBeanProxy(instance) && Dependent.class.equals(bean.getScope())) {
                // Destroy internal reference to a dependent session bean
                destroyDependentInstance(instance);
                return;
            } else {
                // Destroy contextual instance of a normal-scoped bean
                Context context = getBeanManager().getContext(bean.getScope());
                if (context instanceof AlterableContext) {
                    AlterableContext alterableContext = (AlterableContext) context;
                    alterableContext.destroy(bean);
                    return;
                } else {
                    throw BeanLogger.LOG.destroyUnsupported(context);
                }
            }
        }
    }
    // Attempt to destroy dependent instance which is neither a client proxy nor a dependent session bean proxy
    destroyDependentInstance(instance);
}
Also used : WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) AlterableContext(jakarta.enterprise.context.spi.AlterableContext) Context(jakarta.enterprise.context.spi.Context) CreationalContext(jakarta.enterprise.context.spi.CreationalContext) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) Dependent(jakarta.enterprise.context.Dependent) AlterableContext(jakarta.enterprise.context.spi.AlterableContext) ProxyMethodHandler(org.jboss.weld.bean.proxy.ProxyMethodHandler)

Example 3 with Dependent

use of jakarta.enterprise.context.Dependent in project core by weld.

the class GetContextUtilMethodsTest method getActiveContextsTest.

@Test
public void getActiveContextsTest() {
    try (WeldContainer container = new Weld().initialize()) {
        // TheLoneBean is just to have some bean in the archive
        container.select(TheLoneBean.class).get().ping();
        WeldManager manager = container.select(WeldManager.class).get();
        // there are 7 scopes by default in SE, only 3 have active contexts by default
        // it is dependent, singleton and application
        Collection<Context> activeContexts = manager.getActiveContexts();
        Assert.assertEquals(3, activeContexts.size());
        Set<Class<? extends Annotation>> scopes = activeContexts.stream().map(t -> t.getScope()).collect(Collectors.toSet());
        Assert.assertTrue(scopes.contains(Dependent.class));
        Assert.assertTrue(scopes.contains(Singleton.class));
        Assert.assertTrue(scopes.contains(ApplicationScoped.class));
    }
}
Also used : Context(jakarta.enterprise.context.spi.Context) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Dependent(jakarta.enterprise.context.Dependent) Arquillian(org.jboss.arquillian.junit.Arquillian) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) RunWith(org.junit.runner.RunWith) WeldManager(org.jboss.weld.manager.api.WeldManager) BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) TheLoneBean(org.jboss.weld.environment.se.test.weldManager.contextActive.TheLoneBean) RequestContextController(jakarta.enterprise.context.control.RequestContextController) Weld(org.jboss.weld.environment.se.Weld) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Utils(org.jboss.weld.test.util.Utils) Collection(java.util.Collection) Set(java.util.Set) Singleton(jakarta.inject.Singleton) Context(jakarta.enterprise.context.spi.Context) Test(org.junit.Test) Archive(org.jboss.shrinkwrap.api.Archive) ClassPath(org.jboss.arquillian.container.se.api.ClassPath) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Collectors(java.util.stream.Collectors) Deployment(org.jboss.arquillian.container.test.api.Deployment) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Annotation(java.lang.annotation.Annotation) Assert(org.junit.Assert) RequestScoped(jakarta.enterprise.context.RequestScoped) Singleton(jakarta.inject.Singleton) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Dependent(jakarta.enterprise.context.Dependent) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) Annotation(java.lang.annotation.Annotation) Weld(org.jboss.weld.environment.se.Weld) WeldManager(org.jboss.weld.manager.api.WeldManager) Test(org.junit.Test)

Aggregations

Dependent (jakarta.enterprise.context.Dependent)3 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)2 Context (jakarta.enterprise.context.spi.Context)2 RequestScoped (jakarta.enterprise.context.RequestScoped)1 RequestContextController (jakarta.enterprise.context.control.RequestContextController)1 AlterableContext (jakarta.enterprise.context.spi.AlterableContext)1 CreationalContext (jakarta.enterprise.context.spi.CreationalContext)1 InjectionPoint (jakarta.enterprise.inject.spi.InjectionPoint)1 Singleton (jakarta.inject.Singleton)1 Annotation (java.lang.annotation.Annotation)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ClassPath (org.jboss.arquillian.container.se.api.ClassPath)1 Deployment (org.jboss.arquillian.container.test.api.Deployment)1 Arquillian (org.jboss.arquillian.junit.Arquillian)1 Archive (org.jboss.shrinkwrap.api.Archive)1 BeanArchive (org.jboss.shrinkwrap.api.BeanArchive)1 ShrinkWrap (org.jboss.shrinkwrap.api.ShrinkWrap)1