Search in sources :

Example 1 with RequestContext

use of org.jboss.weld.context.RequestContext in project core by weld.

the class WeldStartup method createContexts.

protected Collection<ContextHolder<? extends Context>> createContexts(ServiceRegistry services) {
    List<ContextHolder<? extends Context>> contexts = new ArrayList<ContextHolder<? extends Context>>();
    BeanIdentifierIndex beanIdentifierIndex = services.get(BeanIdentifierIndex.class);
    /*
        * Register a full set of bound and unbound contexts. Although we may not use all of
        * these (e.g. if we are running in a servlet environment) they may be
        * useful for an application.
        */
    Set<Annotation> boundQualifires = ImmutableSet.<Annotation>builder().addAll(Bindings.DEFAULT_QUALIFIERS).add(BoundLiteral.INSTANCE).build();
    Set<Annotation> unboundQualifiers = ImmutableSet.<Annotation>builder().addAll(Bindings.DEFAULT_QUALIFIERS).add(UnboundLiteral.INSTANCE).build();
    contexts.add(new ContextHolder<ApplicationContext>(new ApplicationContextImpl(contextId), ApplicationContext.class, unboundQualifiers));
    contexts.add(new ContextHolder<SingletonContext>(new SingletonContextImpl(contextId), SingletonContext.class, unboundQualifiers));
    contexts.add(new ContextHolder<BoundSessionContext>(new BoundSessionContextImpl(contextId, beanIdentifierIndex), BoundSessionContext.class, boundQualifires));
    contexts.add(new ContextHolder<BoundConversationContext>(new BoundConversationContextImpl(contextId, services), BoundConversationContext.class, boundQualifires));
    contexts.add(new ContextHolder<BoundRequestContext>(new BoundRequestContextImpl(contextId), BoundRequestContext.class, boundQualifires));
    contexts.add(new ContextHolder<RequestContext>(new RequestContextImpl(contextId), RequestContext.class, unboundQualifiers));
    contexts.add(new ContextHolder<DependentContext>(new DependentContextImpl(services.get(ContextualStore.class)), DependentContext.class, unboundQualifiers));
    services.get(WeldModules.class).postContextRegistration(contextId, services, contexts);
    /*
        * Register the contexts with the bean manager and add the beans to the
        * deployment manager so that they are easily accessible (contexts are app
        * scoped)
        */
    for (ContextHolder<? extends Context> context : contexts) {
        deploymentManager.addContext(context.getContext());
        deploymentManager.addBean(ContextBean.of(context, deploymentManager));
    }
    return contexts;
}
Also used : ArrayList(java.util.ArrayList) BoundRequestContextImpl(org.jboss.weld.contexts.bound.BoundRequestContextImpl) RequestContextImpl(org.jboss.weld.contexts.unbound.RequestContextImpl) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) BeanIdentifierIndex(org.jboss.weld.serialization.BeanIdentifierIndex) BoundRequestContextImpl(org.jboss.weld.contexts.bound.BoundRequestContextImpl) BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) ApplicationContext(org.jboss.weld.context.ApplicationContext) SingletonContext(org.jboss.weld.context.SingletonContext) BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) RequestContext(org.jboss.weld.context.RequestContext) DependentContext(org.jboss.weld.context.DependentContext) DependentContext(org.jboss.weld.context.DependentContext) BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) SingletonContext(org.jboss.weld.context.SingletonContext) ApplicationContext(org.jboss.weld.context.ApplicationContext) Context(javax.enterprise.context.spi.Context) BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) RequestContext(org.jboss.weld.context.RequestContext) SingletonContextImpl(org.jboss.weld.contexts.unbound.SingletonContextImpl) BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext) WeldModules(org.jboss.weld.module.WeldModules) Annotation(java.lang.annotation.Annotation) DependentContextImpl(org.jboss.weld.contexts.unbound.DependentContextImpl) ContextualStore(org.jboss.weld.serialization.spi.ContextualStore) ApplicationContextImpl(org.jboss.weld.contexts.unbound.ApplicationContextImpl) BoundSessionContextImpl(org.jboss.weld.contexts.bound.BoundSessionContextImpl) BoundConversationContextImpl(org.jboss.weld.contexts.bound.BoundConversationContextImpl)

Example 2 with RequestContext

use of org.jboss.weld.context.RequestContext in project core by weld.

the class ManagedBean method create.

/**
 * Creates an instance of the bean
 *
 * @return The instance
 */
@Override
public T create(CreationalContext<T> creationalContext) {
    T instance = getProducer().produce(creationalContext);
    getProducer().inject(instance, creationalContext);
    if (!hasPostConstructCallback || beanManager.isContextActive(RequestScoped.class)) {
        getProducer().postConstruct(instance);
    } else {
        /*
             * CDI-219
             * The request scope is active during @PostConstruct callback of any bean.
             */
        RequestContext context = getUnboundRequestContext();
        try {
            context.activate();
            beanManager.fireRequestContextInitialized(getId());
            getProducer().postConstruct(instance);
        } finally {
            beanManager.fireRequestContextBeforeDestroyed(getId());
            context.invalidate();
            context.deactivate();
            beanManager.fireRequestContextDestroyed(getId());
        }
    }
    return instance;
}
Also used : RequestScoped(javax.enterprise.context.RequestScoped) RequestContext(org.jboss.weld.context.RequestContext)

Example 3 with RequestContext

use of org.jboss.weld.context.RequestContext in project core by weld.

the class RemoteClient method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String pathInfo = req.getPathInfo();
    // return on empty path request
    if (pathInfo == null)
        return;
    try {
        RequestContext requestContext = getActiveContext(beanManager, RequestContext.class);
        Bean<KleinStadt> stadtBean = Utils.getBean(beanManager, KleinStadt.class);
        if (pathInfo.equals("/request1")) {
            assertNotNull("Expected a bean for stateful session bean Kassel", stadtBean);
            CreationalContext<KleinStadt> creationalContext = beanManager.createCreationalContext(stadtBean);
            KleinStadt kassel = requestContext.get(stadtBean, creationalContext);
            stadtBean.destroy(kassel, creationalContext);
            assertTrue("Expected SFSB bean to be destroyed", frankfurt.isKleinStadtDestroyed());
            return;
        } else if (pathInfo.equals("/request2")) {
            KleinStadt kassel = requestContext.get(stadtBean);
            assertNull("SFSB bean should not exist after being destroyed", kassel);
            return;
        }
    } catch (AssertionError e) {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
    resp.sendError(HttpServletResponse.SC_NOT_FOUND);
}
Also used : RequestContext(org.jboss.weld.context.RequestContext)

Aggregations

RequestContext (org.jboss.weld.context.RequestContext)3 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 RequestScoped (javax.enterprise.context.RequestScoped)1 Context (javax.enterprise.context.spi.Context)1 ApplicationContext (org.jboss.weld.context.ApplicationContext)1 DependentContext (org.jboss.weld.context.DependentContext)1 SingletonContext (org.jboss.weld.context.SingletonContext)1 BoundConversationContext (org.jboss.weld.context.bound.BoundConversationContext)1 BoundRequestContext (org.jboss.weld.context.bound.BoundRequestContext)1 BoundSessionContext (org.jboss.weld.context.bound.BoundSessionContext)1 BoundConversationContextImpl (org.jboss.weld.contexts.bound.BoundConversationContextImpl)1 BoundRequestContextImpl (org.jboss.weld.contexts.bound.BoundRequestContextImpl)1 BoundSessionContextImpl (org.jboss.weld.contexts.bound.BoundSessionContextImpl)1 ApplicationContextImpl (org.jboss.weld.contexts.unbound.ApplicationContextImpl)1 DependentContextImpl (org.jboss.weld.contexts.unbound.DependentContextImpl)1 RequestContextImpl (org.jboss.weld.contexts.unbound.RequestContextImpl)1 SingletonContextImpl (org.jboss.weld.contexts.unbound.SingletonContextImpl)1 WeldModules (org.jboss.weld.module.WeldModules)1 BeanIdentifierIndex (org.jboss.weld.serialization.BeanIdentifierIndex)1