Search in sources :

Example 1 with AlterableContext

use of javax.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(javax.ejb.NoSuchEJBException) NoSuchEJBException(javax.ejb.NoSuchEJBException) EJBException(javax.ejb.EJBException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) AlterableContext(javax.enterprise.context.spi.AlterableContext) Test(org.junit.Test)

Example 2 with AlterableContext

use of javax.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(javax.enterprise.context.spi.AlterableContext) Test(org.junit.Test)

Example 3 with AlterableContext

use of javax.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.getHandler() instanceof ProxyMethodHandler) {
            ProxyMethodHandler handler = (ProxyMethodHandler) proxy.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 : Context(javax.enterprise.context.spi.Context) CreationalContext(javax.enterprise.context.spi.CreationalContext) WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) AlterableContext(javax.enterprise.context.spi.AlterableContext) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) Dependent(javax.enterprise.context.Dependent) AlterableContext(javax.enterprise.context.spi.AlterableContext) ProxyMethodHandler(org.jboss.weld.bean.proxy.ProxyMethodHandler)

Example 4 with AlterableContext

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

the class DestroyingNormalScopedInstanceTest 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(javax.enterprise.context.spi.AlterableContext) Test(org.junit.Test)

Example 5 with AlterableContext

use of javax.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(javax.enterprise.context.spi.AlterableContext)

Aggregations

AlterableContext (javax.enterprise.context.spi.AlterableContext)6 Test (org.junit.Test)3 Context (javax.enterprise.context.spi.Context)2 EJBException (javax.ejb.EJBException)1 NoSuchEJBException (javax.ejb.NoSuchEJBException)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Dependent (javax.enterprise.context.Dependent)1 CreationalContext (javax.enterprise.context.spi.CreationalContext)1 InvocationContext (javax.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