Search in sources :

Example 1 with BoundSessionContext

use of org.jboss.weld.context.bound.BoundSessionContext 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 BoundSessionContext

use of org.jboss.weld.context.bound.BoundSessionContext in project core by weld.

the class AbstractClusterTest method replicateSession.

protected void replicateSession(int fromId, TestContainer fromContainer, int toId, TestContainer toContainer) throws Exception {
    // Mimic replicating the session - first serialize the objects
    byte[] bytes = serialize(fromContainer.getSessionStore());
    use(toId);
    // Deactivate the other store
    BoundSessionContext sessionContext = toContainer.instance().select(BoundSessionContext.class).get();
    sessionContext.deactivate();
    sessionContext.dissociate(toContainer.getSessionStore());
    // then copy them into the other session store
    toContainer.getSessionStore().putAll(Reflections.<Map<String, Object>>cast(deserialize(bytes)));
    sessionContext.associate(toContainer.getSessionStore());
    // then activate again
    sessionContext.activate();
    use(fromId);
}
Also used : BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext)

Example 3 with BoundSessionContext

use of org.jboss.weld.context.bound.BoundSessionContext in project wildfly by wildfly.

the class WeldContextSetup method setup.

@SuppressWarnings("unchecked")
public void setup(Map<String, Object> properties) {
    try {
        final BeanManager manager = (BeanManager) new InitialContext().lookup(STANDARD_BEAN_MANAGER_JNDI_NAME);
        if (manager != null && Container.available()) {
            final Bean<BoundSessionContext> sessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager.getBeans(BoundSessionContext.class, BoundLiteral.INSTANCE));
            CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
            final BoundSessionContext sessionContext = (BoundSessionContext) manager.getReference(sessionContextBean, BoundSessionContext.class, ctx);
            sessionContext.associate(sessionContexts.get());
            sessionContext.activate();
            final Bean<BoundRequestContext> requestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager.getBeans(BoundRequestContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(requestContextBean);
            final BoundRequestContext requestContext = (BoundRequestContext) manager.getReference(requestContextBean, BoundRequestContext.class, ctx);
            requestContext.associate(requestContexts.get());
            requestContext.activate();
            final Bean<BoundConversationContext> conversationContextBean = (Bean<BoundConversationContext>) manager.resolve(manager.getBeans(BoundConversationContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(conversationContextBean);
            final BoundConversationContext conversationContext = (BoundConversationContext) manager.getReference(conversationContextBean, BoundConversationContext.class, ctx);
            BoundRequest request = new MutableBoundRequest(requestContexts.get(), sessionContexts.get());
            boundRequests.set(request);
            conversationContext.associate(request);
            conversationContext.activate();
        }
    } catch (NamingException e) {
        WeldLogger.ROOT_LOGGER.failedToSetupWeldContexts(e);
    }
}
Also used : MutableBoundRequest(org.jboss.weld.context.bound.MutableBoundRequest) BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) InitialContext(javax.naming.InitialContext) Bean(javax.enterprise.inject.spi.Bean) BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) BoundRequest(org.jboss.weld.context.bound.BoundRequest) MutableBoundRequest(org.jboss.weld.context.bound.MutableBoundRequest) NamingException(javax.naming.NamingException) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 4 with BoundSessionContext

use of org.jboss.weld.context.bound.BoundSessionContext in project wildfly by wildfly.

the class WeldContextSetup method teardown.

@SuppressWarnings("unchecked")
public void teardown(Map<String, Object> properties) {
    try {
        final BeanManager manager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
        if (manager != null && Container.available()) {
            final Bean<BoundSessionContext> sessionContextBean = (Bean<BoundSessionContext>) manager.resolve(manager.getBeans(BoundSessionContext.class, BoundLiteral.INSTANCE));
            CreationalContext<?> ctx = manager.createCreationalContext(sessionContextBean);
            final BoundSessionContext sessionContext = (BoundSessionContext) manager.getReference(sessionContextBean, BoundSessionContext.class, ctx);
            sessionContext.invalidate();
            sessionContext.deactivate();
            sessionContext.dissociate(sessionContexts.get());
            final Bean<BoundRequestContext> requestContextBean = (Bean<BoundRequestContext>) manager.resolve(manager.getBeans(BoundRequestContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(requestContextBean);
            final BoundRequestContext requestContext = (BoundRequestContext) manager.getReference(requestContextBean, BoundRequestContext.class, ctx);
            requestContext.invalidate();
            requestContext.deactivate();
            requestContext.dissociate(requestContexts.get());
            final Bean<BoundConversationContext> conversationContextBean = (Bean<BoundConversationContext>) manager.resolve(manager.getBeans(BoundConversationContext.class, BoundLiteral.INSTANCE));
            ctx = manager.createCreationalContext(conversationContextBean);
            final BoundConversationContext conversationContext = (BoundConversationContext) manager.getReference(conversationContextBean, BoundConversationContext.class, ctx);
            conversationContext.invalidate();
            conversationContext.deactivate();
            conversationContext.dissociate(boundRequests.get());
        }
    } catch (NamingException e) {
        WeldLogger.ROOT_LOGGER.failedToTearDownWeldContexts(e);
    } finally {
        sessionContexts.remove();
        requestContexts.remove();
        boundRequests.remove();
    }
}
Also used : BoundRequestContext(org.jboss.weld.context.bound.BoundRequestContext) BoundSessionContext(org.jboss.weld.context.bound.BoundSessionContext) BoundConversationContext(org.jboss.weld.context.bound.BoundConversationContext) NamingException(javax.naming.NamingException) BeanManager(javax.enterprise.inject.spi.BeanManager) InitialContext(javax.naming.InitialContext) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

BoundSessionContext (org.jboss.weld.context.bound.BoundSessionContext)4 BoundConversationContext (org.jboss.weld.context.bound.BoundConversationContext)3 BoundRequestContext (org.jboss.weld.context.bound.BoundRequestContext)3 Bean (javax.enterprise.inject.spi.Bean)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 InitialContext (javax.naming.InitialContext)2 NamingException (javax.naming.NamingException)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Context (javax.enterprise.context.spi.Context)1 ApplicationContext (org.jboss.weld.context.ApplicationContext)1 DependentContext (org.jboss.weld.context.DependentContext)1 RequestContext (org.jboss.weld.context.RequestContext)1 SingletonContext (org.jboss.weld.context.SingletonContext)1 BoundRequest (org.jboss.weld.context.bound.BoundRequest)1 MutableBoundRequest (org.jboss.weld.context.bound.MutableBoundRequest)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