Search in sources :

Example 1 with BeanStore

use of org.jboss.weld.contexts.beanstore.BeanStore in project core by weld.

the class AbstractContext method get.

/**
 * Get the bean if it exists in the contexts.
 *
 * @return An instance of the bean
 * @throws ContextNotActiveException if the context is not active
 * @see javax.enterprise.context.spi.Context#get(BaseBean, boolean)
 */
@Override
@SuppressFBWarnings(value = "UL_UNRELEASED_LOCK", justification = "False positive from FindBugs")
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    if (!isActive()) {
        throw new ContextNotActiveException();
    }
    checkContextInitialized();
    final BeanStore beanStore = getBeanStore();
    if (beanStore == null) {
        return null;
    }
    if (contextual == null) {
        throw ContextLogger.LOG.contextualIsNull();
    }
    BeanIdentifier id = getId(contextual);
    ContextualInstance<T> beanInstance = beanStore.get(id);
    if (beanInstance != null) {
        return beanInstance.getInstance();
    } else if (creationalContext != null) {
        LockedBean lock = null;
        try {
            if (multithreaded) {
                lock = beanStore.lock(id);
                beanInstance = beanStore.get(id);
                if (beanInstance != null) {
                    return beanInstance.getInstance();
                }
            }
            T instance = contextual.create(creationalContext);
            if (instance != null) {
                beanInstance = new SerializableContextualInstanceImpl<Contextual<T>, T>(contextual, instance, creationalContext, serviceRegistry.get(ContextualStore.class));
                beanStore.put(id, beanInstance);
            }
            return instance;
        } finally {
            if (lock != null) {
                lock.unlock();
            }
        }
    } else {
        return null;
    }
}
Also used : BeanStore(org.jboss.weld.contexts.beanstore.BeanStore) ContextualStore(org.jboss.weld.serialization.spi.ContextualStore) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier) LockedBean(org.jboss.weld.contexts.beanstore.LockedBean) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 2 with BeanStore

use of org.jboss.weld.contexts.beanstore.BeanStore in project core by weld.

the class AbstractContext method destroy.

@Override
public void destroy(Contextual<?> contextual) {
    if (!isActive()) {
        throw new ContextNotActiveException();
    }
    checkContextInitialized();
    if (contextual == null) {
        throw ContextLogger.LOG.contextualIsNull();
    }
    final BeanStore beanStore = getBeanStore();
    if (beanStore == null) {
        throw ContextLogger.LOG.noBeanStoreAvailable(this);
    }
    BeanIdentifier id = getId(contextual);
    ContextualInstance<?> beanInstance = beanStore.remove(id);
    if (beanInstance != null) {
        RequestScopedCache.invalidate();
        destroyContextualInstance(beanInstance);
    }
}
Also used : BeanStore(org.jboss.weld.contexts.beanstore.BeanStore) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier)

Example 3 with BeanStore

use of org.jboss.weld.contexts.beanstore.BeanStore in project core by weld.

the class AbstractContext method destroy.

/**
 * Destroys the context
 */
protected void destroy() {
    ContextLogger.LOG.contextCleared(this);
    final BeanStore beanStore = getBeanStore();
    if (beanStore == null) {
        throw ContextLogger.LOG.noBeanStoreAvailable(this);
    }
    for (BeanIdentifier id : beanStore) {
        destroyContextualInstance(beanStore.get(id));
    }
    beanStore.clear();
}
Also used : BeanStore(org.jboss.weld.contexts.beanstore.BeanStore) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier)

Aggregations

BeanStore (org.jboss.weld.contexts.beanstore.BeanStore)3 BeanIdentifier (org.jboss.weld.serialization.spi.BeanIdentifier)3 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 LockedBean (org.jboss.weld.contexts.beanstore.LockedBean)1 ContextualStore (org.jboss.weld.serialization.spi.ContextualStore)1