Skip to content

Instantly share code, notes, and snippets.

@jyeary
Last active August 7, 2017 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyeary/00fa0011b29bd64c1bd3254657772e93 to your computer and use it in GitHub Desktop.
Save jyeary/00fa0011b29bd64c1bd3254657772e93 to your computer and use it in GitHub Desktop.
package com.bluelotussoftware;
import com.sun.faces.application.view.ViewScopeManager;
import java.util.Collections;
import java.util.Map;
import javax.faces.component.TransientStateHelper;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PreDestroyViewMapEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.ViewMapListener;
/**
* This class is an implementation of {@link ViewMapListener} which listens for
* {@link SystemEvent} on the {@link UIViewRoot}. If the event is a
* {@link PreDestroyViewMapEvent}, the listener will clear the session map of
* the {@link ViewScopeManager#VIEW_MAP_ID} associated with the current view.
*
* @author John Yeary <jyeary@bluelotussoftware.com>
* @version 1.0
* @see ExternalContext#getSessionMap()
* @see ViewScopeManager#ACTIVE_VIEW_MAPS
* @see ViewScopeManager#VIEW_MAP_ID
* @see TransientStateHelper#getTransient(java.lang.Object)
*/
public class ViewMapListenerImpl implements ViewMapListener {
/**
* {@inheritDoc}
*/
@Override
public void processEvent(SystemEvent event) throws AbortProcessingException {
if (event instanceof PreDestroyViewMapEvent) {
PreDestroyViewMapEvent pdvme = (PreDestroyViewMapEvent) event;
UIViewRoot root = (UIViewRoot) pdvme.getComponent();
FacesContext fctx = FacesContext.getCurrentInstance();
String key = (String) root.getTransientStateHelper().getTransient(ViewScopeManager.VIEW_MAP_ID);
System.out.println("Found " + ViewScopeManager.VIEW_MAP_ID + ": " + key);
Map<String, Object> viewMaps = (Map<String, Object>) fctx.getExternalContext().getSessionMap().get(ViewScopeManager.ACTIVE_VIEW_MAPS);
Map<String, Object> map = Collections.synchronizedMap(viewMaps);
synchronized (map) {
System.out.println("Views in session map: " + map.keySet().toString());
map.remove(key);
System.out.println("View removed from " + ViewScopeManager.ACTIVE_VIEW_MAPS);
System.out.println("The remaining views are: " + map.keySet().toString());
}
}
}
/**
* {@inheritDoc}
*
*/
@Override
public boolean isListenerForSource(Object source) {
return source instanceof UIViewRoot;
}
}
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<application>
<system-event-listener>
<system-event-listener-class>com.bluelotussoftware.ViewMapListenerImpl</system-event-listener-class>
<system-event-class>javax.faces.event.PreDestroyViewMapEvent</system-event-class>
<source-class>javax.faces.component.UIViewRoot</source-class>
</system-event-listener>
</application>
</faces-config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment