Search in sources :

Example 1 with PassivationCapable

use of jakarta.enterprise.inject.spi.PassivationCapable 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(jakarta.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 2 with PassivationCapable

use of jakarta.enterprise.inject.spi.PassivationCapable in project myfaces by apache.

the class ViewScopeContext method get.

@Override
public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    checkActive(facesContext);
    if (passivatingScope && !(bean instanceof PassivationCapable)) {
        throw new IllegalStateException(bean.toString() + " doesn't implement " + PassivationCapable.class.getName());
    }
    // force session creation if ViewScoped is used
    facesContext.getExternalContext().getSession(true);
    ViewScopeContextualStorage storage = getContextualStorage(facesContext, 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);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) PassivationCapable(jakarta.enterprise.inject.spi.PassivationCapable) ContextualInstanceInfo(org.apache.myfaces.cdi.util.ContextualInstanceInfo)

Example 3 with PassivationCapable

use of jakarta.enterprise.inject.spi.PassivationCapable in project mojarra by eclipse-ee4j.

the class ViewScopeContextManager method getBean.

/**
 * Get the value from the view map (or null if not found).
 *
 * @param <T> the type.
 * @param facesContext the faces context.
 * @param contextual the contextual.
 * @return the value or null if not found.
 */
@SuppressWarnings("unchecked")
public <T> T getBean(FacesContext facesContext, Contextual<T> contextual) {
    T result = null;
    Map<String, ViewScopeContextObject> contextMap = getContextMap(facesContext);
    if (contextMap != null) {
        if (!(contextual instanceof PassivationCapable)) {
            throw new IllegalArgumentException("ViewScoped bean " + contextual.toString() + " must be PassivationCapable, but is not.");
        }
        ViewScopeContextObject contextObject = contextMap.get(((PassivationCapable) contextual).getId());
        if (contextObject != null) {
            String name = contextObject.getName();
            LOGGER.log(FINEST, "Getting value for @ViewScoped bean with name: {0}", name);
            result = (T) facesContext.getViewRoot().getViewMap(true).get(name);
        }
    }
    return result;
}
Also used : PassivationCapable(jakarta.enterprise.inject.spi.PassivationCapable) FINEST(java.util.logging.Level.FINEST)

Example 4 with PassivationCapable

use of jakarta.enterprise.inject.spi.PassivationCapable in project mojarra by eclipse-ee4j.

the class ClientWindowScopeContextManager method getBean.

/**
 * Get the value from the ClientWindow-map (or null if not found).
 *
 * @param <T> the type.
 * @param facesContext the faces context.
 * @param contextual the contextual.
 * @return the value or null if not found.
 */
@SuppressWarnings("unchecked")
public <T> T getBean(FacesContext facesContext, Contextual<T> contextual) {
    T result = null;
    Map<String, ClientWindowScopeContextObject> contextMap = getContextMap(facesContext);
    if (contextMap != null) {
        if (!(contextual instanceof PassivationCapable)) {
            throw new IllegalArgumentException("ClientWindowScoped bean " + contextual.toString() + " must be PassivationCapable, but is not.");
        }
        ClientWindowScopeContextObject<T> contextObject = contextMap.get(((PassivationCapable) contextual).getId());
        if (contextObject != null) {
            return contextObject.getContextualInstance();
        }
    }
    return result;
}
Also used : PassivationCapable(jakarta.enterprise.inject.spi.PassivationCapable) FINEST(java.util.logging.Level.FINEST)

Example 5 with PassivationCapable

use of jakarta.enterprise.inject.spi.PassivationCapable in project mojarra by eclipse-ee4j.

the class ClientWindowScopeContextManager method createBean.

/**
 * Create the bean.
 *
 * @param <T> the type.
 * @param facesContext the faces context.
 * @param contextual the contextual.
 * @param creational the creational.
 * @return the value or null if not found.
 */
public <T> T createBean(FacesContext facesContext, Contextual<T> contextual, CreationalContext<T> creational) {
    LOGGER.log(FINEST, "Creating @ClientWindowScoped CDI bean using contextual: {0}", contextual);
    if (!(contextual instanceof PassivationCapable)) {
        throw new IllegalArgumentException("ClientWindowScoped bean " + contextual.toString() + " must be PassivationCapable, but is not.");
    }
    T contextualInstance = contextual.create(creational);
    if (contextualInstance != null) {
        String passivationCapableId = ((PassivationCapable) contextual).getId();
        getContextMap(facesContext).put(passivationCapableId, new ClientWindowScopeContextObject(passivationCapableId, contextualInstance));
    }
    return contextualInstance;
}
Also used : PassivationCapable(jakarta.enterprise.inject.spi.PassivationCapable) FINEST(java.util.logging.Level.FINEST)

Aggregations

PassivationCapable (jakarta.enterprise.inject.spi.PassivationCapable)8 FINEST (java.util.logging.Level.FINEST)4 FacesContext (jakarta.faces.context.FacesContext)3 FlowHandler (jakarta.faces.flow.FlowHandler)2 ContextualInstanceInfo (org.apache.myfaces.cdi.util.ContextualInstanceInfo)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ContextNotActiveException (jakarta.enterprise.context.ContextNotActiveException)1 CreationalContext (jakarta.enterprise.context.spi.CreationalContext)1 Flow (jakarta.faces.flow.Flow)1 ContextualStorage (org.apache.myfaces.cdi.util.ContextualStorage)1 FlowReference (org.apache.myfaces.flow.FlowReference)1 CommonBean (org.jboss.weld.bean.CommonBean)1 StringBeanIdentifier (org.jboss.weld.bean.StringBeanIdentifier)1 BeanIdentifier (org.jboss.weld.serialization.spi.BeanIdentifier)1