Search in sources :

Example 1 with AlterableContext

use of jakarta.enterprise.context.spi.AlterableContext 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 2 with AlterableContext

use of jakarta.enterprise.context.spi.AlterableContext in project core by weld.

the class AlterableContextTest method testNothingHappensIfNoInstanceToDestroy.

@Test
public void testNothingHappensIfNoInstanceToDestroy(ApplicationScopedComponent application) {
    Bean<?> bean = manager.resolve(manager.getBeans(ApplicationScopedComponent.class));
    AlterableContext context = (AlterableContext) manager.getContext(bean.getScope());
    AbstractComponent.reset();
    application.setValue("value");
    context.destroy(bean);
    assertTrue(AbstractComponent.isDestroyed());
    // make sure subsequent calls do not raise exception
    context.destroy(bean);
    context.destroy(bean);
}
Also used : AlterableContext(jakarta.enterprise.context.spi.AlterableContext) Test(org.junit.Test)

Example 3 with AlterableContext

use of jakarta.enterprise.context.spi.AlterableContext in project core by weld.

the class StatefulSessionBeanRecreatedAfterDestroyTest method testWithSpi.

@Test
public void testWithSpi() {
    assertEquals("pong", bean.ping());
    // cause SFSB to be removed
    try {
        bean.throwException();
        Assert.fail();
    } catch (EJBException expected) {
    }
    // verify that every access causes NoSuchEJBException
    try {
        bean.ping();
        Assert.fail();
    } catch (NoSuchEJBException expected) {
    }
    // try to destroy the bean
    Bean<?> contextual = manager.resolve(manager.getBeans(StatefulBean.class));
    AlterableContext context = (AlterableContext) manager.getContext(ApplicationScoped.class);
    context.destroy(contextual);
    assertEquals("pong", bean.ping());
}
Also used : NoSuchEJBException(jakarta.ejb.NoSuchEJBException) NoSuchEJBException(jakarta.ejb.NoSuchEJBException) EJBException(jakarta.ejb.EJBException) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) AlterableContext(jakarta.enterprise.context.spi.AlterableContext) Test(org.junit.Test)

Example 4 with AlterableContext

use of jakarta.enterprise.context.spi.AlterableContext in project core by weld.

the class AlterableContextTest method testComponent.

private <T extends AbstractComponent> void testComponent(Class<T> javaClass) {
    Bean<?> bean = manager.resolve(manager.getBeans(javaClass));
    @SuppressWarnings("unchecked") T reference = (T) manager.getReference(bean, javaClass, manager.createCreationalContext(bean));
    AlterableContext context = (AlterableContext) manager.getContext(bean.getScope());
    for (String string : VALUES) {
        assertNull(reference.getValue());
        reference.setValue(string);
        assertEquals(string, reference.getValue());
        AbstractComponent.reset();
        context.destroy(bean);
        assertTrue(AbstractComponent.isDestroyed());
        assertNull(reference.getValue(), reference.getValue());
    }
}
Also used : AlterableContext(jakarta.enterprise.context.spi.AlterableContext)

Example 5 with AlterableContext

use of jakarta.enterprise.context.spi.AlterableContext in project core by weld.

the class TestServlet method doGet.

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String action = req.getParameter("action");
    switch(action) {
        case "init":
            resp.getWriter().print(bean.ping());
            break;
        case "destroy":
            Bean<?> bean = beanManager.getBeans(SessionScopedBean.class).iterator().next();
            AlterableContext sessionContext = (AlterableContext) beanManager.getContext(SessionScoped.class);
            sessionContext.destroy(bean);
            resp.getWriter().print("ok");
            break;
        case "test":
            resp.getWriter().print(SessionScopedBean.DESTROYED);
            break;
        default:
            throw new IllegalStateException("Unknown action");
    }
}
Also used : SessionScoped(jakarta.enterprise.context.SessionScoped) AlterableContext(jakarta.enterprise.context.spi.AlterableContext)

Aggregations

AlterableContext (jakarta.enterprise.context.spi.AlterableContext)7 Test (org.junit.Test)3 Context (jakarta.enterprise.context.spi.Context)2 EJBException (jakarta.ejb.EJBException)1 NoSuchEJBException (jakarta.ejb.NoSuchEJBException)1 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 Dependent (jakarta.enterprise.context.Dependent)1 SessionScoped (jakarta.enterprise.context.SessionScoped)1 CreationalContext (jakarta.enterprise.context.spi.CreationalContext)1 InvocationContext (jakarta.interceptor.InvocationContext)1 ProxyMethodHandler (org.jboss.weld.bean.proxy.ProxyMethodHandler)1 ProxyObject (org.jboss.weld.bean.proxy.ProxyObject)1 WeldCreationalContext (org.jboss.weld.contexts.WeldCreationalContext)1