javax.enterprise.inject.spi.PassivationCapable Java Examples

The following examples show how to use javax.enterprise.inject.spi.PassivationCapable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedirectScopeManager.java    From krazo with Apache License 2.0 6 votes vote down vote up
/**
 * Destroy the instance.
 *
 * @param contextual the contextual.
 */
public void destroy(Contextual contextual) {
    String scopeId = (String) request.getAttribute(SCOPE_ID);
    if (null != scopeId) {
        HttpSession session = request.getSession();
        if (contextual instanceof PassivationCapable == false) {
            throw new RuntimeException("Unexpected type for contextual");
        }
        PassivationCapable pc = (PassivationCapable) contextual;
        final String sessionKey = SCOPE_ID + "-" + scopeId;
        Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
        if (null != scopeMap) {
            Object instance = scopeMap.get(INSTANCE + pc.getId());
            CreationalContext<?> creational = (CreationalContext<?>) scopeMap.get(CREATIONAL + pc.getId());
            if (null != instance && null != creational) {
                contextual.destroy(instance, creational);
                creational.release();
            }
        }
    }
}
 
Example #2
Source File: RedirectScopeManager.java    From krazo with Apache License 2.0 6 votes vote down vote up
/**
 * Get the instance.
 *
 * @param <T> the type.
 * @param contextual the contextual.
 * @return the instance, or null.
 */
public <T> T get(Contextual<T> contextual) {
    T result = null;

    String scopeId = (String) request.getAttribute(SCOPE_ID);
    if (null != scopeId) {
        HttpSession session = request.getSession();
        if (contextual instanceof PassivationCapable == false) {
            throw new RuntimeException("Unexpected type for contextual");
        }
        PassivationCapable pc = (PassivationCapable) contextual;
        final String sessionKey = SCOPE_ID + "-" + scopeId;
        Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
        if (null != scopeMap) {
            result = (T) scopeMap.get(INSTANCE + pc.getId());
        } else {
            request.setAttribute(SCOPE_ID, null);       // old cookie, force new scope generation
        }
    }

    return result;
}
 
Example #3
Source File: RedirectScopeManager.java    From krazo with Apache License 2.0 6 votes vote down vote up
/**
 * Get the instance (create it if it does not exist).
 *
 * @param <T> the type.
 * @param contextual the contextual.
 * @param creational the creational.
 * @return the instance.
 */
public <T> T get(Contextual<T> contextual, CreationalContext<T> creational) {
    T result = get(contextual);

    if (result == null) {
        String scopeId = (String) request.getAttribute(SCOPE_ID);
        if (null == scopeId) {
            scopeId = generateScopeId();
        }
        HttpSession session = request.getSession();
        result = contextual.create(creational);
        if (contextual instanceof PassivationCapable == false) {
            throw new RuntimeException("Unexpected type for contextual");
        }
        PassivationCapable pc = (PassivationCapable) contextual;
        final String sessionKey = SCOPE_ID + "-" + scopeId;
        Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
        if (null != scopeMap) {
            session.setAttribute(sessionKey, scopeMap);
            scopeMap.put(INSTANCE + pc.getId(), result);
            scopeMap.put(CREATIONAL + pc.getId(), creational);
        }
    }

    return result;
}
 
Example #4
Source File: RedirectScopeManager.java    From ozark with Apache License 2.0 6 votes vote down vote up
/**
 * Destroy the instance.
 *
 * @param contextual the contextual.
 */
public void destroy(Contextual contextual) {
    String scopeId = (String) request.getAttribute(SCOPE_ID);
    if (null != scopeId) {
        HttpSession session = request.getSession();
        if (contextual instanceof PassivationCapable == false) {
            throw new RuntimeException("Unexpected type for contextual");
        }
        PassivationCapable pc = (PassivationCapable) contextual;
        final String sessionKey = SCOPE_ID + "-" + scopeId;
        Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
        if (null != scopeMap) {
            Object instance = scopeMap.get(INSTANCE + pc.getId());
            CreationalContext<?> creational = (CreationalContext<?>) scopeMap.get(CREATIONAL + pc.getId());
            if (null != instance && null != creational) {
                contextual.destroy(instance, creational);
                creational.release();
            }
        }
    }
}
 
Example #5
Source File: RedirectScopeManager.java    From ozark with Apache License 2.0 6 votes vote down vote up
/**
 * Get the instance.
 *
 * @param <T> the type.
 * @param contextual the contextual.
 * @return the instance, or null.
 */
public <T> T get(Contextual<T> contextual) {
    T result = null;

    String scopeId = (String) request.getAttribute(SCOPE_ID);
    if (null != scopeId) {
        HttpSession session = request.getSession();
        if (contextual instanceof PassivationCapable == false) {
            throw new RuntimeException("Unexpected type for contextual");
        }
        PassivationCapable pc = (PassivationCapable) contextual;
        final String sessionKey = SCOPE_ID + "-" + scopeId;
        Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
        if (null != scopeMap) {
            result = (T) scopeMap.get(INSTANCE + pc.getId());
        } else {
            request.setAttribute(SCOPE_ID, null);       // old cookie, force new scope generation
        }
    }

    return result;
}
 
Example #6
Source File: RedirectScopeManager.java    From ozark with Apache License 2.0 6 votes vote down vote up
/**
 * Get the instance (create it if it does not exist).
 *
 * @param <T> the type.
 * @param contextual the contextual.
 * @param creational the creational.
 * @return the instance.
 */
public <T> T get(Contextual<T> contextual, CreationalContext<T> creational) {
    T result = get(contextual);

    if (result == null) {
        String scopeId = (String) request.getAttribute(SCOPE_ID);
        if (null == scopeId) {
            scopeId = generateScopeId();
        }
        HttpSession session = request.getSession();
        result = contextual.create(creational);
        if (contextual instanceof PassivationCapable == false) {
            throw new RuntimeException("Unexpected type for contextual");
        }
        PassivationCapable pc = (PassivationCapable) contextual;
        final String sessionKey = SCOPE_ID + "-" + scopeId;
        Map<String, Object> scopeMap = (Map<String, Object>) session.getAttribute(sessionKey);
        if (null != scopeMap) {
            session.setAttribute(sessionKey, scopeMap);
            scopeMap.put(INSTANCE + pc.getId(), result);
            scopeMap.put(CREATIONAL + pc.getId(), creational);
        }
    }

    return result;
}
 
Example #7
Source File: WebappBeanManager.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public boolean accept(final Bean<?> bean) {
    if (BuiltInOwbBean.class.isInstance(bean) || ExtensionBean.class.isInstance(bean)) {
        return false;
    }
    if (OwbBean.class.isInstance(bean)) {
        final OwbBean owbBean = OwbBean.class.cast(bean);
        if (owbBean.isPassivationCapable())
        {
            if (hasBean(owbBean.getId()))
            {
                return false;
            }
        }
    } else if (PassivationCapable.class.isInstance(bean)) {
        if (hasBean(PassivationCapable.class.cast(bean).getId())) {
            return false;
        }
    }

    final Set<Annotation> qualifiers = bean.getQualifiers();
    return beanManager.getBeans(
            bean.getBeanClass(),
            qualifiers.isEmpty() ? EMPTY_ANNOTATIONS : qualifiers.toArray(new Annotation[qualifiers.size()])).isEmpty();
}
 
Example #8
Source File: DependentProvider.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
private void writeObject(ObjectOutputStream out) throws IOException
{
    if (!(bean instanceof PassivationCapable))
    {
        throw new NotSerializableException("Bean is not PassivationCapable: " + bean.toString());
    }
    String passivationId = ((PassivationCapable) bean).getId();
    if (passivationId == null)
    {
        throw new NotSerializableException(bean.toString());
    }

    out.writeLong(serialVersionUID);
    out.writeObject(passivationId);
    out.writeObject(instance);
    out.writeObject(creationalContext);
}
 
Example #9
Source File: ViewAccessContext.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T get(Contextual<T> bean)
{
    try
    {
        return super.get(bean);
    }
    finally
    {
        if (bean instanceof PassivationCapable)
        {
            PassivationCapable pc = (PassivationCapable) bean;
            viewAccessBeanAccessHistory.getAccessedBeans().add(pc.getId());
        }
    }
}
 
Example #10
Source File: ViewAccessContext.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
{
    try
    {
        return super.get(bean, creationalContext);
    }
    finally
    {
        if (bean instanceof PassivationCapable)
        {
            PassivationCapable pc = (PassivationCapable) bean;
            viewAccessBeanAccessHistory.getAccessedBeans().add(pc.getId());
        }
    }
}
 
Example #11
Source File: DeltaSpikeProxyContextualLifecycle.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private synchronized void init()
{
    if (this.deltaSpikeProxyInvocationHandler == null)
    {
        Set<Bean<H>> handlerBeans = BeanProvider.getBeanDefinitions(
                delegateInvocationHandlerClass, false, true, beanManager);
        if (handlerBeans.size() != 1)
        {
            StringBuilder beanInfo = new StringBuilder();
            for (Bean<H> bean : handlerBeans)
            {
                if (beanInfo.length() != 0)
                {
                    beanInfo.append(", ");
                }
                beanInfo.append(bean);

                if (bean instanceof PassivationCapable)
                {
                    beanInfo.append(" bean-id: ").append(((PassivationCapable) bean).getId());
                }
            }

            throw new IllegalStateException(handlerBeans.size() + " beans found for "
                    + delegateInvocationHandlerClass + " found beans: " + beanInfo.toString());
        }
        this.handlerBean = handlerBeans.iterator().next();

        this.deltaSpikeProxyInvocationHandler = BeanProvider.getContextualReference(
                beanManager, DeltaSpikeProxyInvocationHandler.class, false);
    }
}
 
Example #12
Source File: AbstractContext.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext)
{
    if (creationalContext == null)
    {
        return get(bean);
    }
    
    checkActive();

    if (passivatingScope)
    {
        if (!(bean instanceof PassivationCapable))
        {
            throw new IllegalStateException(bean.toString() +
                    " doesn't implement " + PassivationCapable.class.getName());
        }
    }

    ContextualStorage storage = getContextualStorage(bean, true);

    Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
    ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));

    if (contextualInstanceInfo != null)
    {
        @SuppressWarnings("unchecked")
        final T instance =  (T) contextualInstanceInfo.getContextualInstance();

        if (instance != null)
        {
            return instance;
        }
    }

    return storage.createContextualInstance(bean, creationalContext);
}
 
Example #13
Source File: ContextualStorage.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * If the context is a passivating scope then we return
 * the passivationId of the Bean. Otherwise we use
 * the Bean directly.
 * @return the key to use in the context map
 */
public <T> Object getBeanKey(Contextual<T> bean)
{
    if (passivationCapable)
    {
        // if the
        return ((PassivationCapable) bean).getId();
    }

    return bean;
}
 
Example #14
Source File: ConversationUtils.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public static ConversationKey convertToConversationKey(Contextual<?> contextual, BeanManager beanManager)
{
    if (!(contextual instanceof Bean))
    {
        if (contextual instanceof PassivationCapable)
        {
            contextual = beanManager.getPassivationCapableBean(((PassivationCapable) contextual).getId());
        }
        else
        {
            throw new IllegalArgumentException(
                contextual.getClass().getName() + " is not of type " + Bean.class.getName());
        }
    }

    Bean<?> bean = (Bean<?>) contextual;

    //don't cache it (due to the support of different producers)
    ConversationGroup conversationGroupAnnotation = findConversationGroupAnnotation(bean);

    Class<?> conversationGroup;
    if (conversationGroupAnnotation != null)
    {
        conversationGroup = conversationGroupAnnotation.value();
    }
    else
    {
        conversationGroup = bean.getBeanClass();
    }

    Set<Annotation> qualifiers = bean.getQualifiers();
    return new ConversationKey(conversationGroup, qualifiers.toArray(new Annotation[qualifiers.size()]));
}