if (reference != null)
{
String flowMapKey = getFlowScopeBeanHolder().getFlowMapKey(facesContext, reference);
if (flowMapKey != null)
{
ContextualStorage storage = getContextualStorage(false, flowMapKey);
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("Flow "+ reference.getId()+
" cannot be found when resolving bean " + bean.toString());
}
FlowHandler flowHandler = facesContext.getApplication().getFlowHandler();
// Since it is possible to have only the flow id without documentId, the best
// is first get the flow using flowHandler.getFlow and then check if the flow is
// active or not, but using the documentId and id of the retrieved flow.
Flow flow = flowHandler.getFlow(facesContext,
reference.getDocumentId() == null ? "" : reference.getDocumentId(), reference.getId());
if (flow == null)
{
throw new IllegalStateException(bean.toString() + "cannot be created because flow "+ reference.getId()+
" is not registered");
}
if (!flowHandler.isActive(facesContext, flow.getDefiningDocumentId(), flow.getId()))
{
throw new IllegalStateException(bean.toString() + "cannot be created if flow "+ reference.getId()+
" is not active");
}
ContextualStorage storage = getContextualStorage(true, flowMapKey);
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);
}
List<String> activeFlowMapKeys = getFlowScopeBeanHolder().getActiveFlowMapKeys(facesContext);
for (String flowMapKey : activeFlowMapKeys)
{
ContextualStorage storage = getContextualStorage(false, flowMapKey);
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;
}
}
}
ContextualStorage storage = getContextualStorage(true, getCurrentClientWindowFlowId(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);
}