Search in sources :

Example 1 with ContextNotActiveException

use of javax.enterprise.context.ContextNotActiveException in project deltaspike by apache.

the class DeltaSpikePhaseListener method processInitView.

private void processInitView(String viewId) {
    try {
        WindowMetaData windowMetaData = BeanProvider.getContextualReference(WindowMetaData.class);
        //view already initialized in this or any prev. request
        if (viewId.equals(windowMetaData.getInitializedViewId())) {
            return;
        }
        //override the view-id if we have a new view
        windowMetaData.setInitializedViewId(viewId);
        ViewConfigDescriptor viewDefinitionEntry = this.viewConfigResolver.getViewConfigDescriptor(viewId);
        if (viewDefinitionEntry == null) {
            return;
        }
        ViewControllerUtils.executeViewControllerCallback(viewDefinitionEntry, InitView.class);
    } catch (ContextNotActiveException e) {
    //TODO discuss how we handle it
    }
}
Also used : ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) ViewConfigDescriptor(org.apache.deltaspike.core.api.config.view.metadata.ViewConfigDescriptor)

Example 2 with ContextNotActiveException

use of javax.enterprise.context.ContextNotActiveException in project deltaspike by apache.

the class ContainerCtrlTckTest method testRestartContexts.

/**
     * Stops and starts: application-, session- and request-scope.
     * <p/>
     * application-scoped instance has a ref to
     * request-scoped instance which has a ref to
     * session-scoped instance.
     * <p/>
     * If the deepest ref has the expected value, all levels in between were resetted correctly.
     */
@Test
public void testRestartContexts() {
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cdiContainer);
    cdiContainer.boot();
    cdiContainer.getContextControl().startContexts();
    BeanManager beanManager = cdiContainer.getBeanManager();
    Assert.assertNotNull(beanManager);
    Set<Bean<?>> beans = beanManager.getBeans(CarRepair.class);
    Bean<?> bean = beanManager.resolve(beans);
    CarRepair carRepair = (CarRepair) beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean));
    Assert.assertNotNull(carRepair);
    Car car = carRepair.getCar();
    Assert.assertNotNull(car);
    Assert.assertNotNull(car.getUser());
    carRepair.getCar().getUser().setName("tester");
    Assert.assertEquals("tester", car.getUser().getName());
    Assert.assertFalse(CarRepair.isPreDestroyCalled());
    Assert.assertFalse(Car.isPreDestroyCalled());
    Assert.assertFalse(TestUser.isPreDestroyCalled());
    cdiContainer.getContextControl().stopContexts();
    Assert.assertTrue(CarRepair.isPreDestroyCalled());
    Assert.assertTrue(Car.isPreDestroyCalled());
    Assert.assertTrue(TestUser.isPreDestroyCalled());
    try {
        car.getUser();
        // accessing the car should have triggered a ContextNotActiveException
        Assert.fail();
    } catch (ContextNotActiveException e) {
    //do nothing - exception expected
    }
    cdiContainer.getContextControl().startContexts();
    carRepair = (CarRepair) beanManager.getReference(bean, CarRepair.class, beanManager.createCreationalContext(bean));
    Assert.assertNotNull(carRepair.getCar());
    Assert.assertNotNull(carRepair.getCar().getUser());
    Assert.assertNull(carRepair.getCar().getUser().getName());
    cdiContainer.shutdown();
}
Also used : CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) Car(org.apache.deltaspike.cdise.tck.beans.Car) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Bean(javax.enterprise.inject.spi.Bean) Test(org.junit.Test)

Example 3 with ContextNotActiveException

use of javax.enterprise.context.ContextNotActiveException in project deltaspike by apache.

the class ContainerCtrlTckTest method testNewRequests.

@Test
public void testNewRequests() {
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cdiContainer);
    cdiContainer.boot();
    cdiContainer.getContextControl().startContext(SessionScoped.class);
    cdiContainer.getContextControl().startContext(RequestScoped.class);
    BeanManager beanManager = cdiContainer.getBeanManager();
    Assert.assertNotNull(beanManager);
    TestUser testUser = resolveInstance(beanManager, TestUser.class);
    Assert.assertNotNull(testUser);
    testUser.setName("tester");
    CarRepair carRepair = resolveInstance(beanManager, CarRepair.class);
    Assert.assertNotNull(carRepair);
    Car car = carRepair.getCar();
    Assert.assertNotNull(car);
    Assert.assertNotNull(car.getUser());
    Assert.assertEquals("tester", car.getUser().getName());
    carRepair.getCar().getUser().setName("tck-tester");
    Assert.assertEquals("tck-tester", testUser.getName());
    cdiContainer.getContextControl().stopContext(RequestScoped.class);
    cdiContainer.getContextControl().startContext(RequestScoped.class);
    try {
        testUser = resolveInstance(beanManager, TestUser.class);
        Assert.assertNotNull(testUser);
        Assert.assertNotNull(testUser.getName());
        Assert.assertEquals("tck-tester", testUser.getName());
    } catch (ContextNotActiveException e) {
        Assert.fail(e.getMessage());
    }
    try {
        carRepair = resolveInstance(beanManager, CarRepair.class);
        Assert.assertNotNull(carRepair);
        car = carRepair.getCar();
        Assert.assertNotNull(car);
        Assert.assertNotNull(car.getUser());
        Assert.assertNotNull(car.getUser().getName());
        Assert.assertEquals("tck-tester", car.getUser().getName());
    } catch (ContextNotActiveException e) {
        Assert.fail(e.getMessage());
    }
    cdiContainer.shutdown();
}
Also used : CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) Car(org.apache.deltaspike.cdise.tck.beans.Car) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) BeanManager(javax.enterprise.inject.spi.BeanManager) TestUser(org.apache.deltaspike.cdise.tck.beans.TestUser) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 4 with ContextNotActiveException

use of javax.enterprise.context.ContextNotActiveException in project deltaspike by apache.

the class JsfUtils method tryToRestoreMessages.

public static void tryToRestoreMessages(FacesContext facesContext) {
    JsfModuleConfig jsfModuleConfig = BeanProvider.getContextualReference(JsfModuleConfig.class);
    if (!jsfModuleConfig.isAlwaysKeepMessages()) {
        return;
    }
    try {
        WindowMetaData windowMetaData = BeanProvider.getContextualReference(WindowMetaData.class);
        @SuppressWarnings({ "unchecked" }) List<FacesMessageEntry> facesMessageEntryList = windowMetaData.getFacesMessageEntryList();
        List<FacesMessage> originalMessageList = new ArrayList<FacesMessage>(facesContext.getMessageList());
        if (facesMessageEntryList != null) {
            for (FacesMessageEntry messageEntry : facesMessageEntryList) {
                if (isNewMessage(originalMessageList, messageEntry.getFacesMessage())) {
                    facesContext.addMessage(messageEntry.getComponentId(), messageEntry.getFacesMessage());
                }
            }
            facesMessageEntryList.clear();
        }
    } catch (ContextNotActiveException e) {
    //TODO discuss how we handle it
    }
}
Also used : JsfModuleConfig(org.apache.deltaspike.jsf.api.config.JsfModuleConfig) FacesMessageEntry(org.apache.deltaspike.jsf.impl.message.FacesMessageEntry) WindowMetaData(org.apache.deltaspike.jsf.impl.listener.phase.WindowMetaData) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FacesMessage(javax.faces.application.FacesMessage)

Example 5 with ContextNotActiveException

use of javax.enterprise.context.ContextNotActiveException in project deltaspike by apache.

the class JsfUtils method saveFacesMessages.

public static void saveFacesMessages(ExternalContext externalContext) {
    JsfModuleConfig jsfModuleConfig = BeanProvider.getContextualReference(JsfModuleConfig.class);
    if (!jsfModuleConfig.isAlwaysKeepMessages()) {
        return;
    }
    try {
        WindowMetaData windowMetaData = BeanProvider.getContextualReference(WindowMetaData.class);
        Map<String, Object> requestMap = externalContext.getRequestMap();
        @SuppressWarnings({ "unchecked" }) List<FacesMessageEntry> facesMessageEntryList = (List<FacesMessageEntry>) requestMap.get(FacesMessageEntry.class.getName());
        if (facesMessageEntryList == null) {
            facesMessageEntryList = new CopyOnWriteArrayList<FacesMessageEntry>();
        }
        windowMetaData.setFacesMessageEntryList(facesMessageEntryList);
    } catch (ContextNotActiveException e) {
        //TODO log it in case of project-stage development
        //we can't handle it correctly -> delegate to the jsf-api (which has some restrictions esp. before v2.2)
        FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
    }
}
Also used : JsfModuleConfig(org.apache.deltaspike.jsf.api.config.JsfModuleConfig) FacesMessageEntry(org.apache.deltaspike.jsf.impl.message.FacesMessageEntry) WindowMetaData(org.apache.deltaspike.jsf.impl.listener.phase.WindowMetaData) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)6 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)2 Car (org.apache.deltaspike.cdise.tck.beans.Car)2 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)2 JsfModuleConfig (org.apache.deltaspike.jsf.api.config.JsfModuleConfig)2 WindowMetaData (org.apache.deltaspike.jsf.impl.listener.phase.WindowMetaData)2 FacesMessageEntry (org.apache.deltaspike.jsf.impl.message.FacesMessageEntry)2 Test (org.junit.Test)2 List (java.util.List)1 Bean (javax.enterprise.inject.spi.Bean)1 FacesMessage (javax.faces.application.FacesMessage)1 ViewExpiredException (javax.faces.application.ViewExpiredException)1 UIViewRoot (javax.faces.component.UIViewRoot)1 FacesContext (javax.faces.context.FacesContext)1 Flash (javax.faces.context.Flash)1 ExceptionQueuedEvent (javax.faces.event.ExceptionQueuedEvent)1 ExceptionQueuedEventContext (javax.faces.event.ExceptionQueuedEventContext)1