Created
February 7, 2013 07:22
-
-
Save banterCZ/4729186 to your computer and use it in GitHub Desktop.
Classes needed for JSF ManagedBeans of ViewScope managed by Spring.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.faces.context.FacesContext; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.ObjectFactory; | |
import org.springframework.beans.factory.config.Scope; | |
import org.springframework.web.context.request.FacesRequestAttributes; | |
import java.util.Map; | |
/** | |
* Copy-cat from <a href="http://www.harezmi.com.tr/spring-view-scope-for-jsf-2-users/">http://www.harezmi.com.tr/spring-view-scope-for-jsf-2-users/</a> | |
*/ | |
public class ViewScope implements Scope { | |
private Logger logger = LoggerFactory.getLogger(getClass()); | |
public static final String VIEW_SCOPE_CALLBACKS = "viewScope.callbacks"; | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public Object get(String name, ObjectFactory<?> objectFactory) { | |
Map<String, Object> viewMap = getViewMap(); | |
Object instance = viewMap.get(name); | |
if (instance == null) { | |
instance = objectFactory.getObject(); | |
synchronized (viewMap) { | |
viewMap.put(name, instance); | |
logger.debug("Bean '{}' has been put to ViewScope.", instance); | |
} | |
} else { | |
logger.debug("Going to return an existing bean '{}'", instance); | |
} | |
return instance; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public Object remove(String name) { | |
Object instance = getViewMap().remove(name); | |
if (instance != null) { | |
Map<String, Runnable> callbacks = (Map<String, Runnable>) getViewMap().get(VIEW_SCOPE_CALLBACKS); | |
if (callbacks != null) { | |
callbacks.remove(name); | |
logger.debug("Bean '{}' has been removed.", instance); | |
} | |
} | |
return instance; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void registerDestructionCallback(String name, Runnable runnable) { | |
Map<String, Runnable> callbacks = (Map<String, Runnable>) getViewMap().get(VIEW_SCOPE_CALLBACKS); | |
if (callbacks != null) { | |
callbacks.put(name, runnable); | |
logger.debug("Registered callback for '{}'", name); | |
} | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public Object resolveContextualObject(String name) { | |
FacesContext facesContext = FacesContext.getCurrentInstance(); | |
FacesRequestAttributes facesRequestAttributes = new FacesRequestAttributes(facesContext); | |
return facesRequestAttributes.resolveReference(name); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public String getConversationId() { | |
FacesContext facesContext = FacesContext.getCurrentInstance(); | |
FacesRequestAttributes facesRequestAttributes = new FacesRequestAttributes(facesContext); | |
return facesRequestAttributes.getSessionId() + "-" + facesContext.getViewRoot().getViewId(); | |
} | |
private Map<String, Object> getViewMap() { | |
return FacesContext.getCurrentInstance().getViewRoot().getViewMap(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.faces.component.UIViewRoot; | |
import javax.faces.event.*; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Copy-cat from <a href="http://www.harezmi.com.tr/spring-view-scope-for-jsf-2-users/">http://www.harezmi.com.tr/spring-view-scope-for-jsf-2-users/</a> | |
*/ | |
public class ViewScopeCallbackRegistrar implements ViewMapListener { | |
private Logger logger = LoggerFactory.getLogger(getClass()); | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void processEvent(SystemEvent event) throws AbortProcessingException { | |
if (event instanceof PostConstructViewMapEvent) { | |
logger.debug("Registering a destruction callback for '{}'", event); | |
PostConstructViewMapEvent viewMapEvent = (PostConstructViewMapEvent) event; | |
UIViewRoot viewRoot = (UIViewRoot) viewMapEvent.getComponent(); | |
viewRoot.getViewMap().put(ViewScope.VIEW_SCOPE_CALLBACKS, new HashMap<String, Runnable>()); | |
} else if (event instanceof PreDestroyViewMapEvent) { | |
logger.debug("Executing a destruction callback for '{}'", event); | |
PreDestroyViewMapEvent viewMapEvent = (PreDestroyViewMapEvent) event; | |
UIViewRoot viewRoot = (UIViewRoot) viewMapEvent.getComponent(); | |
Map<String, Runnable> callbacks = (Map<String, Runnable>) viewRoot.getViewMap().get(ViewScope.VIEW_SCOPE_CALLBACKS); | |
if (callbacks != null) { | |
for (Runnable c : callbacks.values()) { | |
c.run(); | |
} | |
callbacks.clear(); | |
} | |
} | |
} | |
public boolean isListenerForSource(Object source) { | |
return source instanceof UIViewRoot; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment