Search in sources :

Example 1 with ContextualInstanceInfo

use of org.apache.myfaces.cdi.util.ContextualInstanceInfo in project myfaces by apache.

the class FacesScopeContext method get.

@Override
public <T> T get(Contextual<T> bean, CreationalContext<T> creationalContext) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    checkActive(facesContext);
    ContextualStorage storage = getContextualStorage(true, facesContext);
    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) ContextualStorage(org.apache.myfaces.cdi.util.ContextualStorage) ContextualInstanceInfo(org.apache.myfaces.cdi.util.ContextualInstanceInfo)

Example 2 with ContextualInstanceInfo

use of org.apache.myfaces.cdi.util.ContextualInstanceInfo in project myfaces by apache.

the class ClientWindowScopeContext method get.

@Override
public <T> T get(Contextual<T> bean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    checkActive(facesContext);
    if (facesContext != null) {
        ContextualStorage storage = getStorageHolder(facesContext).getContextualStorage(getCurrentClientWindowId(facesContext), false);
        if (storage != null) {
            Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
            ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
            if (contextualInstanceInfo != null) {
                return (T) contextualInstanceInfo.getContextualInstance();
            }
        }
    } else {
        throw new IllegalStateException("FacesContext cannot be found when resolving bean " + bean.toString());
    }
    return null;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ContextualStorage(org.apache.myfaces.cdi.util.ContextualStorage) ContextualInstanceInfo(org.apache.myfaces.cdi.util.ContextualInstanceInfo)

Example 3 with ContextualInstanceInfo

use of org.apache.myfaces.cdi.util.ContextualInstanceInfo in project myfaces by apache.

the class ViewScopeCDIMap method put.

@Override
public Object put(String key, Object value) {
    ContextualInstanceInfo info = new ContextualInstanceInfo();
    info.setContextualInstance(value);
    return getCreationalContextInstances().put(key, info);
}
Also used : ContextualInstanceInfo(org.apache.myfaces.cdi.util.ContextualInstanceInfo)

Example 4 with ContextualInstanceInfo

use of org.apache.myfaces.cdi.util.ContextualInstanceInfo in project myfaces by apache.

the class ViewScopeContext method get.

@Override
public <T> T get(Contextual<T> bean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    checkActive(facesContext);
    // force session creation if ViewScoped is used
    facesContext.getExternalContext().getSession(true);
    ViewScopeContextualStorage storage = getContextualStorage(facesContext, false);
    if (storage == null) {
        return null;
    }
    Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
    ContextualInstanceInfo<?> contextualInstanceInfo = contextMap.get(storage.getBeanKey(bean));
    if (contextualInstanceInfo == null) {
        return null;
    }
    return (T) contextualInstanceInfo.getContextualInstance();
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ContextualInstanceInfo(org.apache.myfaces.cdi.util.ContextualInstanceInfo)

Example 5 with ContextualInstanceInfo

use of org.apache.myfaces.cdi.util.ContextualInstanceInfo 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)

Aggregations

ContextualInstanceInfo (org.apache.myfaces.cdi.util.ContextualInstanceInfo)15 ContextualStorage (org.apache.myfaces.cdi.util.ContextualStorage)11 FacesContext (jakarta.faces.context.FacesContext)10 Map (java.util.Map)4 Contextual (jakarta.enterprise.context.spi.Contextual)3 PassivationCapable (jakarta.enterprise.inject.spi.PassivationCapable)2 FlowReference (org.apache.myfaces.flow.FlowReference)2 LRULinkedHashMap (org.apache.myfaces.util.lang.LRULinkedHashMap)2 Flow (jakarta.faces.flow.Flow)1 FlowHandler (jakarta.faces.flow.FlowHandler)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1