All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.contexts.bound.BoundSessionContextImpl Maven / Gradle / Ivy

Go to download

This jar bundles all the bits of Weld and CDI required for running in a Servlet container.

There is a newer version: 6.0.0.Beta4
Show newest version
package org.jboss.weld.contexts.bound;

import java.lang.annotation.Annotation;
import java.util.Map;

import javax.enterprise.context.SessionScoped;

import org.jboss.weld.contexts.AbstractBoundContext;
import org.jboss.weld.contexts.beanstore.NamingScheme;
import org.jboss.weld.contexts.beanstore.SessionMapBeanStore;
import org.jboss.weld.contexts.beanstore.SimpleBeanIdentifierIndexNamingScheme;
import org.jboss.weld.context.bound.BoundSessionContext;
import org.jboss.weld.logging.ContextLogger;
import org.jboss.weld.serialization.BeanIdentifierIndex;

public class BoundSessionContextImpl extends AbstractBoundContext> implements BoundSessionContext {

    // There is no need to store FQCN in a session key
    static final String NAMING_SCHEME_PREFIX = "WELD_BS";

    static final String KEY_BEAN_ID_INDEX_HASH = NAMING_SCHEME_PREFIX + "_HASH";

    private final NamingScheme namingScheme;

    public BoundSessionContextImpl(String contextId, BeanIdentifierIndex index) {
        super(contextId, true);
        this.namingScheme = new SimpleBeanIdentifierIndexNamingScheme(NAMING_SCHEME_PREFIX, index);
    }

    public Class getScope() {
        return SessionScoped.class;
    }

    public boolean associate(Map storage) {
        if (getBeanStore() == null) {
            setBeanStore(new SessionMapBeanStore(namingScheme, storage));
            checkBeanIdentifierIndexConsistency(storage);
            return true;
        } else {
            return false;
        }
    }

    private void checkBeanIdentifierIndexConsistency(Map storage) {
        BeanIdentifierIndex index = getServiceRegistry().get(BeanIdentifierIndex.class);
        if (index != null && index.isBuilt()) {
            Object hash = storage.get(KEY_BEAN_ID_INDEX_HASH);
            if (hash != null) {
                if (!index.getIndexHash().equals(hash)) {
                    throw ContextLogger.LOG.beanIdentifierIndexInconsistencyDetected(hash.toString(), index.getDebugInfo());
                }
            } else {
                storage.put(KEY_BEAN_ID_INDEX_HASH, index.getIndexHash());
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy