package com.pearson.epen.cdi.extension;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PreDestroyViewMapEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
import com.pearson.epen.cdi.context.ViewScoped;
/**
* This class provides the contexts lifecycle for the new JSF-2 @ViewScoped
* Context (from seam faces 3.0.0.CR1 source code in context package)
*
* @author Mark Struberg
* @author Lincoln Baxter, III
* Modified by epen development to use epen's own context ViewScoped
*/
public class ViewScopedContext implements Context, SystemEventListener {
private final static String COMPONENT_MAP_NAME = "viewscope.componentInstanceMap";
private final static String CREATIONAL_MAP_NAME = "viewscope.creationalInstanceMap";
private boolean isJsfSubscribed = false;
@SuppressWarnings("unchecked")
public T get(final Contextual component) {
assertActive();
if (!isJsfSubscribed) {
FacesContext.getCurrentInstance().getApplication()
.subscribeToEvent(PreDestroyViewMapEvent.class, this);
isJsfSubscribed = true;
}
T instance = (T) getComponentInstanceMap().get(component);
return instance;
}
@SuppressWarnings("unchecked")
public T get(final Contextual component,
final CreationalContext creationalContext) {
assertActive();
T instance = get(component);
if (instance == null) {
if (creationalContext != null) {
Map, Object> componentInstanceMap = getComponentInstanceMap();
Map, CreationalContext>> creationalContextMap = getCreationalInstanceMap();
synchronized (componentInstanceMap) {
instance = (T) componentInstanceMap.get(component);
if (instance == null) {
instance = component.create(creationalContext);
if (instance != null) {
componentInstanceMap.put(component, instance);
creationalContextMap.put(component,
creationalContext);
}
}
}
}
}
return instance;
}
public Class extends Annotation> getScope() {
//modified by epen dev to use epen's viewscoped
return ViewScoped.class;
}
public boolean isActive() {
return getViewRoot() != null;
}
private void assertActive() {
if (!isActive()) {
throw new ContextNotActiveException(
"epen Viewscoped context with scope annotation @ViewScoped is not active with respect to the current thread");
}
}
public boolean isListenerForSource(final Object source) {
if (source instanceof UIViewRoot) {
return true;
}
return false;
}
/**
* We get PreDestroyViewMapEvent events from the JSF servlet and destroy our
* contextual instances. This should (theoretically!) also get fired if the
* webapp closes, so there should be no need to manually track all view
* scopes and destroy them at a shutdown.
*
* @see javax.faces.event.SystemEventListener#processEvent(javax.faces.event.SystemEvent)
*/
@SuppressWarnings("unchecked")
public void processEvent(final SystemEvent event) {
if (event instanceof PreDestroyViewMapEvent) {
Map, Object> componentInstanceMap = getComponentInstanceMap();
Map, CreationalContext>> creationalContextMap = getCreationalInstanceMap();
if (componentInstanceMap != null) {
for (Entry, Object> componentEntry : componentInstanceMap
.entrySet()) {
/*
* No way to inform the compiler of type information, so
* it has to be abandoned here :(
*/
Contextual contextual = componentEntry.getKey();
Object instance = componentEntry.getValue();
CreationalContext creational = creationalContextMap
.get(contextual);
contextual.destroy(instance, creational);
}
}
}
}
protected UIViewRoot getViewRoot() {
FacesContext context = FacesContext.getCurrentInstance();
if (context != null) {
return context.getViewRoot();
}
return null;
}
protected Map getViewMap() {
UIViewRoot viewRoot = getViewRoot();
if (viewRoot != null) {
return viewRoot.getViewMap(true);
}
return null;
}
@SuppressWarnings("unchecked")
private Map, Object> getComponentInstanceMap() {
Map viewMap = getViewMap();
Map, Object> map = (ConcurrentHashMap, Object>) viewMap
.get(COMPONENT_MAP_NAME);
if (map == null) {
map = new ConcurrentHashMap, Object>();
viewMap.put(COMPONENT_MAP_NAME, map);
}
return map;
}
@SuppressWarnings("unchecked")
private Map, CreationalContext>> getCreationalInstanceMap() {
Map viewMap = getViewMap();
Map, CreationalContext>> map = (Map, CreationalContext>>) viewMap
.get(CREATIONAL_MAP_NAME);
if (map == null) {
map = new ConcurrentHashMap, CreationalContext>>();
viewMap.put(CREATIONAL_MAP_NAME, map);
}
return map;
}
}