Search in sources :

Example 1 with BeanIdentifier

use of org.jboss.weld.serialization.spi.BeanIdentifier in project core by weld.

the class AttributeBeanStore method iterator.

public Iterator<BeanIdentifier> iterator() {
    Iterator<BeanIdentifier> iterator;
    if (isAttributeLazyFetchingEnabled()) {
        // Merge the bean identifiers from the local bean store and the backing store
        Set<BeanIdentifier> identifiers = new HashSet<>();
        for (BeanIdentifier id : beanStore) {
            identifiers.add(id);
        }
        for (String prefixedId : getPrefixedAttributeNames()) {
            identifiers.add(getNamingScheme().deprefix(prefixedId));
        }
        iterator = identifiers.iterator();
    } else {
        iterator = beanStore.iterator();
    }
    return iterator;
}
Also used : BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier) HashSet(java.util.HashSet)

Example 2 with BeanIdentifier

use of org.jboss.weld.serialization.spi.BeanIdentifier in project core by weld.

the class AttributeBeanStore method fetchUninitializedAttributes.

/**
 * Fetch all relevant attributes from the backing store and copy instances which are not present in the local bean store.
 */
public void fetchUninitializedAttributes() {
    for (String prefixedId : getPrefixedAttributeNames()) {
        BeanIdentifier id = getNamingScheme().deprefix(prefixedId);
        if (!beanStore.contains(id)) {
            ContextualInstance<?> instance = (ContextualInstance<?>) getAttribute(prefixedId);
            beanStore.put(id, instance);
            ContextLogger.LOG.addingDetachedContextualUnderId(instance, id);
        }
    }
}
Also used : ContextualInstance(org.jboss.weld.context.api.ContextualInstance) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier)

Example 3 with BeanIdentifier

use of org.jboss.weld.serialization.spi.BeanIdentifier 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 4 with BeanIdentifier

use of org.jboss.weld.serialization.spi.BeanIdentifier in project core by weld.

the class ContextualStoreImpl method putIfAbsent.

/**
 * Add a contextual (if not already present) to the store, and return it's
 * id. If the contextual is passivation capable, it's id will be used,
 * otherwise an id will be generated
 *
 * @param contextual the contextual to add
 * @return the current id for the contextual
 */
@SuppressFBWarnings(value = "RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED", justification = "Using non-standard semantics of putIfAbsent")
public BeanIdentifier putIfAbsent(Contextual<?> contextual) {
    if (contextual instanceof CommonBean<?>) {
        // this is a Bean<?> created by Weld
        CommonBean<?> bean = (CommonBean<?>) contextual;
        passivationCapableContextuals.putIfAbsent(bean.getIdentifier(), contextual);
        return bean.getIdentifier();
    }
    if (contextual instanceof PassivationCapable) {
        // this is an extension-provided passivation capable bean
        PassivationCapable passivationCapable = (PassivationCapable) contextual;
        String id = passivationCapable.getId();
        BeanIdentifier identifier = new StringBeanIdentifier(id);
        passivationCapableContextuals.putIfAbsent(identifier, contextual);
        return identifier;
    } else {
        BeanIdentifier id = contextuals.get(contextual);
        if (id != null) {
            return id;
        } else {
            synchronized (contextual) {
                id = contextuals.get(contextual);
                if (id == null) {
                    id = new StringBeanIdentifier(new StringBuilder().append(GENERATED_ID_PREFIX).append(idGenerator.incrementAndGet()).toString());
                    contextuals.put(contextual, id);
                    contextualsInverse.put(id, contextual);
                }
                return id;
            }
        }
    }
}
Also used : PassivationCapable(javax.enterprise.inject.spi.PassivationCapable) StringBeanIdentifier(org.jboss.weld.bean.StringBeanIdentifier) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier) CommonBean(org.jboss.weld.bean.CommonBean) StringBeanIdentifier(org.jboss.weld.bean.StringBeanIdentifier) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 5 with BeanIdentifier

use of org.jboss.weld.serialization.spi.BeanIdentifier in project core by weld.

the class ProxyFactory method createCompoundProxyName.

private static String createCompoundProxyName(String contextId, Bean<?> bean, TypeInfo typeInfo, StringBuilder name) {
    String className;
    final List<String> interfaces = new ArrayList<String>();
    for (Class<?> type : typeInfo.getInterfaces()) {
        interfaces.add(type.getSimpleName());
    }
    Collections.sort(interfaces);
    for (final String iface : interfaces) {
        name.append(iface);
        name.append('$');
    }
    // However, it is safe to share a proxy class for built-in beans of the same type (e.g. Event)
    if (bean != null && !(bean instanceof AbstractBuiltInBean)) {
        final BeanIdentifier id = Container.instance(contextId).services().get(ContextualStore.class).putIfAbsent(bean);
        int idHash = id.hashCode();
        name.append(Math.abs(idHash == Integer.MIN_VALUE ? 0 : idHash));
    }
    className = name.toString();
    return className;
}
Also used : AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) ContextualStore(org.jboss.weld.serialization.spi.ContextualStore) BeanIdentifier(org.jboss.weld.serialization.spi.BeanIdentifier) ArrayList(java.util.ArrayList)

Aggregations

BeanIdentifier (org.jboss.weld.serialization.spi.BeanIdentifier)10 BeanStore (org.jboss.weld.contexts.beanstore.BeanStore)3 ContextualStore (org.jboss.weld.serialization.spi.ContextualStore)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 PassivationCapable (javax.enterprise.inject.spi.PassivationCapable)1 CommonBean (org.jboss.weld.bean.CommonBean)1 StringBeanIdentifier (org.jboss.weld.bean.StringBeanIdentifier)1 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)1 ContextualInstance (org.jboss.weld.context.api.ContextualInstance)1 LockedBean (org.jboss.weld.contexts.beanstore.LockedBean)1 TypeInfo (org.jboss.weld.util.Proxies.TypeInfo)1