Search in sources :

Example 1 with ManagedConversation

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

the class AbstractConversationContext method invalidate.

@Override
public void invalidate() {
    ManagedConversation currentConversation = getCurrentConversation();
    Map<String, ManagedConversation> conversations = getConversationMap();
    synchronized (conversations) {
        for (Entry<String, ManagedConversation> stringManagedConversationEntry : conversations.entrySet()) {
            ManagedConversation conversation = stringManagedConversationEntry.getValue();
            if (!currentConversation.equals(conversation) && !conversation.isTransient() && isExpired(conversation)) {
                // Try to lock the conversation and log warning if not successful - unlocking should not be necessary
                if (!conversation.lock(0)) {
                    ConversationLogger.LOG.endLockedConversation(conversation.getId());
                }
                conversation.end();
            }
        }
    }
}
Also used : ManagedConversation(org.jboss.weld.context.ManagedConversation)

Example 2 with ManagedConversation

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

the class AbstractConversationContext method associateRequestWithNewConversation.

protected void associateRequestWithNewConversation() {
    ManagedConversation conversation = new ConversationImpl(manager);
    lock(conversation);
    setRequestAttribute(getRequest(), CURRENT_CONVERSATION_ATTRIBUTE_NAME, conversation);
    // Set a temporary bean store, this will be attached at the end of the request if needed
    NamingScheme namingScheme = new ConversationNamingScheme(getNamingSchemePrefix(), "transient", beanIdentifierIndex);
    setBeanStore(createRequestBeanStore(namingScheme, getRequest()));
    setRequestAttribute(getRequest(), ConversationNamingScheme.PARAMETER_NAME, namingScheme);
}
Also used : ConversationNamingScheme(org.jboss.weld.contexts.beanstore.ConversationNamingScheme) NamingScheme(org.jboss.weld.contexts.beanstore.NamingScheme) ConversationNamingScheme(org.jboss.weld.contexts.beanstore.ConversationNamingScheme) ManagedConversation(org.jboss.weld.context.ManagedConversation) ConversationImpl(org.jboss.weld.contexts.conversation.ConversationImpl)

Example 3 with ManagedConversation

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

the class JsonObjects method createContextJson.

static JsonObjectBuilder createContextJson(String id, Class<? extends Annotation> scope, BeanManagerImpl beanManager, Probe probe, HttpServletRequest req) {
    JsonObjectBuilder builder = createSimpleContextJson(id, scope);
    builder.add(INSTANCES, inspectContext(scope, beanManager, probe));
    if (req != null && ConversationScoped.class.equals(scope)) {
        HttpSession session = req.getSession(false);
        if (session != null) {
            // Get all available conversation ids
            Object conversationsAttribute = session.getAttribute(AbstractConversationContext.CONVERSATIONS_ATTRIBUTE_NAME);
            if (conversationsAttribute != null && conversationsAttribute instanceof Map) {
                @SuppressWarnings("unchecked") Map<String, ManagedConversation> conversationsMap = (Map<String, ManagedConversation>) conversationsAttribute;
                if (!conversationsMap.isEmpty()) {
                    JsonArrayBuilder cidsBuilder = Json.arrayBuilder();
                    for (String cid : conversationsMap.keySet()) {
                        cidsBuilder.add(cid);
                    }
                    builder.add(CIDS, cidsBuilder);
                }
            }
        }
    }
    return builder;
}
Also used : ConversationScoped(javax.enterprise.context.ConversationScoped) HttpSession(javax.servlet.http.HttpSession) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) ManagedConversation(org.jboss.weld.context.ManagedConversation) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder) Map(java.util.Map)

Example 4 with ManagedConversation

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

the class AbstractConversationContext method destroy.

public boolean destroy(S session) {
    // the context may be active
    // if it is, we need to re-attach the bean store once the other conversations are destroyed
    final BoundBeanStore beanStore = getBeanStore();
    final boolean active = isActive();
    if (beanStore != null) {
        beanStore.detach();
    }
    try {
        Object conversationMap = getSessionAttributeFromSession(session, CONVERSATIONS_ATTRIBUTE_NAME);
        if (conversationMap instanceof Map) {
            Map<String, ManagedConversation> conversations = cast(conversationMap);
            synchronized (conversations) {
                if (!conversations.isEmpty()) {
                    // There are some conversations to destroy
                    setActive(true);
                    if (beanStore == null) {
                        // There is no request associated - destroy conversation contexts immediately
                        for (Entry<String, ManagedConversation> entry : conversations.entrySet()) {
                            destroyConversation(session, entry.getKey());
                        }
                    } else {
                        // All conversation contexts created during the current session should be destroyed after the servlet service() completes
                        // However, at that time the session will not be available - store all remaining contextual instances in the request
                        setDestructionQueue(conversations, session);
                    }
                }
            }
        }
        return true;
    } finally {
        setBeanStore(beanStore);
        setActive(active);
        if (beanStore != null) {
            beanStore.attach();
        } else if (!active) {
            removeState();
            cleanup();
        }
    }
}
Also used : ManagedConversation(org.jboss.weld.context.ManagedConversation) BoundBeanStore(org.jboss.weld.contexts.beanstore.BoundBeanStore) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ManagedConversation

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

the class AbstractConversationContext method setDestructionQueue.

private void setDestructionQueue(Map<String, ManagedConversation> conversations, S session) {
    Map<String, List<ContextualInstance<?>>> contexts = new HashMap<>();
    for (Entry<String, ManagedConversation> entry : conversations.entrySet()) {
        ManagedConversation conversation = entry.getValue();
        // First make all conversations transient
        if (!conversation.isTransient()) {
            conversation.end();
        }
        // Extract contextual instances
        List<ContextualInstance<?>> contextualInstances = new ArrayList<>();
        for (String id : new ConversationNamingScheme(getNamingSchemePrefix(), entry.getKey(), beanIdentifierIndex).filterIds(getSessionAttributeNames(session))) {
            contextualInstances.add((ContextualInstance<?>) getSessionAttributeFromSession(session, id));
        }
        contexts.put(entry.getKey(), contextualInstances);
    }
    // Store remaining conversation contexts for later destruction
    setRequestAttribute(getRequest(), DESTRUCTION_QUEUE_ATTRIBUTE_NAME, Collections.synchronizedMap(contexts));
}
Also used : ContextualInstance(org.jboss.weld.context.api.ContextualInstance) HashMap(java.util.HashMap) ConversationNamingScheme(org.jboss.weld.contexts.beanstore.ConversationNamingScheme) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ManagedConversation(org.jboss.weld.context.ManagedConversation)

Aggregations

ManagedConversation (org.jboss.weld.context.ManagedConversation)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConversationNamingScheme (org.jboss.weld.contexts.beanstore.ConversationNamingScheme)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConversationScoped (javax.enterprise.context.ConversationScoped)1 HttpSession (javax.servlet.http.HttpSession)1 ProxyObject (org.jboss.weld.bean.proxy.ProxyObject)1 ContextualInstance (org.jboss.weld.context.api.ContextualInstance)1 BoundBeanStore (org.jboss.weld.contexts.beanstore.BoundBeanStore)1 NamingScheme (org.jboss.weld.contexts.beanstore.NamingScheme)1 ConversationImpl (org.jboss.weld.contexts.conversation.ConversationImpl)1 JsonArrayBuilder (org.jboss.weld.probe.Json.JsonArrayBuilder)1 JsonObjectBuilder (org.jboss.weld.probe.Json.JsonObjectBuilder)1