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();
}
}
}
}
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);
}
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;
}
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();
}
}
}
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));
}
Aggregations