Package org.jboss.errai.bus.server.websocket.jsr356.weld.conversation

Source Code of org.jboss.errai.bus.server.websocket.jsr356.weld.conversation.WeldConversationScopeAdapter

package org.jboss.errai.bus.server.websocket.jsr356.weld.conversation;

import org.jboss.errai.bus.server.websocket.jsr356.weld.request.WeldRequestScopeAdapter;
import org.jboss.errai.bus.server.websocket.jsr356.weld.session.WeldSessionScopeAdapter;
import org.jboss.weld.context.ManagedConversation;
import org.jboss.weld.context.bound.BoundConversationContext;
import org.jboss.weld.context.bound.BoundRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.naming.OperationNotSupportedException;

import java.util.Map;

/**
* Adapter for {@link javax.enterprise.context.ConversationScoped}
*
* @authorMichel Werren
*/
public class WeldConversationScopeAdapter implements ConversationScopeAdapter {

  private static final Logger LOGGER = LoggerFactory.getLogger(WeldConversationScopeAdapter.class.getName());

  private final BoundConversationContext boundConversationContext;

  private static final ThreadLocal<BoundRequest> CURRENT_BOUND_REQUEST = new ThreadLocal<BoundRequest>();

  private static final ThreadLocal<ConversationState> CURRENT_CONVERSATION_STATE = new ThreadLocal<ConversationState>();

  private static WeldConversationScopeAdapter instance;

  private WeldConversationScopeAdapter(BoundConversationContext boundConversationContext) {
    this.boundConversationContext = boundConversationContext;
  }

  public static WeldConversationScopeAdapter getInstance() {
    if (instance == null) {
      throw new IllegalStateException("Adapter not initialized!");
    }
    return instance;
  }

  public static void init(BoundConversationContext context) {
    if (instance == null) {
      instance = new WeldConversationScopeAdapter(context);
    }
  }

  private void associateContext() {
    final BoundRequest storage = new BoundRequest() {
      @Override
      public Map<String, Object> getRequestMap() {
        return WeldRequestScopeAdapter.getCurrentBeanStore();
      }

      @Override
      public Map<String, Object> getSessionMap(boolean create) {
        return WeldSessionScopeAdapter.getCurrentBeanStore();
      }
    };
    final boolean successful = boundConversationContext.associate(storage);
    if (!successful) {
      LOGGER.error("could not attach conversation storage");
    }
    CURRENT_BOUND_REQUEST.set(storage);
  }

  @Override
  public void activateContext() {
    throw new RuntimeException(new OperationNotSupportedException("Conversation state needed"));
  }

  @Override
  public void activateContext(ConversationState conversationState) {
    CURRENT_CONVERSATION_STATE.set(conversationState);
    if (!boundConversationContext.isActive()) {
      associateContext();
      if (conversationState.isLongRunning()) {
        boundConversationContext.activate(conversationState.getConversationId());
      }
      else {
        boundConversationContext.activate();
      }
    }
  }

  @Override
  public void invalidateContext() {
    boundConversationContext.invalidate();
    deactivateContext();
  }

  @Override
  public void deactivateContext() {
    final ConversationState conversationState = CURRENT_CONVERSATION_STATE.get();
    final ManagedConversation currentConversation = boundConversationContext.getCurrentConversation();
    if (!currentConversation.isTransient() && !conversationState.isLongRunning()) {
      startLongRunningConversation(conversationState, currentConversation);
    }
    else if (currentConversation.isTransient() && conversationState.isLongRunning()) {
      endLongRunningConversation(conversationState);
    }
    else if (currentConversation.getId() != null
            && !currentConversation.getId().equals(conversationState.getConversationId())) {
      LOGGER.warn("current conversation id: {} and registered: {}. There shouldn't be two activated conversations",
              currentConversation.getId(), conversationState.getConversationId());
    }
    boundConversationContext.deactivate();
    boundConversationContext.dissociate(CURRENT_BOUND_REQUEST.get());
    CURRENT_BOUND_REQUEST.remove();
    CURRENT_CONVERSATION_STATE.remove();
  }

  /**
   * Tasks when a long running conversation has started during this event.
   *
   * @param conversationState
   * @param managedConversation
   */
  private void startLongRunningConversation(ConversationState conversationState, ManagedConversation managedConversation) {
    conversationState.registerLongRunningConversaton(managedConversation.getId());
    LOGGER.trace("register long running conversation: {}", managedConversation.getId());
  }

  /**
   * Tasks when a long running conversation has ended during this event.
   *
   * @param conversationState
   */
  private void endLongRunningConversation(ConversationState conversationState) {
    conversationState.removeLongRunningConversation();
    boundConversationContext.invalidate();
    LOGGER.trace("end long running conversation id: {}", conversationState.getConversationId());
  }
}
TOP

Related Classes of org.jboss.errai.bus.server.websocket.jsr356.weld.conversation.WeldConversationScopeAdapter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.