com.mycompany.jsf.event
Class CaptureStatePhaseListener

java.lang.Object
  extended bycom.mycompany.jsf.event.CaptureStatePhaseListener
All Implemented Interfaces:
java.util.EventListener, javax.faces.event.PhaseListener, java.io.Serializable

public class CaptureStatePhaseListener
extends java.lang.Object
implements javax.faces.event.PhaseListener

This class is a JSF PhaseListener that captures view state before and after each request processing lifecycle phase.

It saves the state as a tree composed of TreeNode instances in a session scope Map named "com.mycompany.debug", with an entry per view keyed by the view ID.

Version:
1.0
Author:
Hans Bergsten, Gefion Software
See Also:
Serialized Form

Field Summary
private  java.util.Map pdCache
           
 
Constructor Summary
CaptureStatePhaseListener()
           
 
Method Summary
private  void addComponentNodes(javax.faces.context.FacesContext context, TreeNode parent, javax.faces.component.UIComponent comp)
          Creates nodes for properties and attributes of the provided component and adds them as branches under the provided node, and then recursively calls itself for each child and facet of the provided component.
private  void addLeafNodes(TreeNode parent, java.util.Map map)
          Add a TreeNode instance for each entry in the Map, with the entry key (converted to a String) as the node name, the entry value (converted to a String) as the node value, and with the "leafNode" property set to "true".
 void afterPhase(javax.faces.event.PhaseEvent event)
          Saves the view state after the regular processing of the view in the current phase by calling capturePhaseData().
 void beforePhase(javax.faces.event.PhaseEvent event)
          Saves the view state before the regular processing of the view in the current phase by calling capturePhaseData(), unless the current phase is Restore View (there's no view available in this case).
private  void capturePhaseData(java.lang.String phaseName, javax.faces.context.FacesContext context)
          Creates nodes for the view.
private  void captureRequestData(javax.faces.context.FacesContext context)
          Creates nodes for the request data.
private  java.lang.String format(java.util.Iterator i)
          Returns a String with comma-separated list of the values represented by the Iterator.
private  java.lang.String format(java.util.Map map)
          Returns a String with comma-separated list of the Map entries, with each entry as "key = value".
 javax.faces.event.PhaseId getPhaseId()
          Returns PhaseId.ANY_PHASE to announce that this listener must be invoked in all phases.
private  TreeNode getRoot(javax.faces.context.FacesContext context)
          Returns the TreeNode for the tree root, from the Map saved as a session scope variable named "com.mycompany.debug" or a new instance if it's not found or the found instance is for a previous request.
private  java.lang.String getViewId(javax.faces.context.FacesContext context)
          Returns the view ID for the view held by the provided FacesContext, adjusted if needed.
private  java.lang.Object[] toObjects(boolean[] arr)
          Returns an Object array with the values of the array.
private  java.lang.Object[] toObjects(byte[] arr)
          Returns an Object array with the values of the array.
private  java.lang.Object[] toObjects(char[] arr)
          Returns an Object array with the values of the array.
private  java.lang.Object[] toObjects(double[] arr)
          Returns an Object array with the values of the array.
private  java.lang.Object[] toObjects(float[] arr)
          Returns an Object array with the values of the array.
private  java.lang.Object[] toObjects(int[] arr)
          Returns an Object array with the values of the array.
private  java.lang.Object[] toObjects(long[] arr)
          Returns an Object array with the values of the array.
private  java.lang.Object[] toObjects(short[] arr)
          Returns an Object array with the values of the array.
private  java.lang.String toString(java.lang.Object value)
          Returns the value as a String in an appropriate format depending on the data type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pdCache

private java.util.Map pdCache
Constructor Detail

CaptureStatePhaseListener

public CaptureStatePhaseListener()
Method Detail

getPhaseId

public javax.faces.event.PhaseId getPhaseId()
Returns PhaseId.ANY_PHASE to announce that this listener must be invoked in all phases.

Specified by:
getPhaseId in interface javax.faces.event.PhaseListener

beforePhase

public void beforePhase(javax.faces.event.PhaseEvent event)
Saves the view state before the regular processing of the view in the current phase by calling capturePhaseData(), unless the current phase is Restore View (there's no view available in this case).

Specified by:
beforePhase in interface javax.faces.event.PhaseListener

afterPhase

public void afterPhase(javax.faces.event.PhaseEvent event)
Saves the view state after the regular processing of the view in the current phase by calling capturePhaseData(). If the current phase is Render Response, also saves general request data by calling captureRequestData().

Specified by:
afterPhase in interface javax.faces.event.PhaseListener

getRoot

private TreeNode getRoot(javax.faces.context.FacesContext context)
Returns the TreeNode for the tree root, from the Map saved as a session scope variable named "com.mycompany.debug" or a new instance if it's not found or the found instance is for a previous request.


getViewId

private java.lang.String getViewId(javax.faces.context.FacesContext context)
Returns the view ID for the view held by the provided FacesContext, adjusted if needed. For the first phases on a new view, the viewId may have the wrong extension. Just replace it with ".jsp" (should really look at mapping).


captureRequestData

private void captureRequestData(javax.faces.context.FacesContext context)
Creates nodes for the request data. Nodes for request headers, request parameters, and request locales are created and added as a branch under the root of the tree.


capturePhaseData

private void capturePhaseData(java.lang.String phaseName,
                              javax.faces.context.FacesContext context)
Creates nodes for the view. Nodes for each component in the view's component tree and all scoped variables are created and added as a branch under the root of the tree.


addComponentNodes

private void addComponentNodes(javax.faces.context.FacesContext context,
                               TreeNode parent,
                               javax.faces.component.UIComponent comp)
Creates nodes for properties and attributes of the provided component and adds them as branches under the provided node, and then recursively calls itself for each child and facet of the provided component.


addLeafNodes

private void addLeafNodes(TreeNode parent,
                          java.util.Map map)
Add a TreeNode instance for each entry in the Map, with the entry key (converted to a String) as the node name, the entry value (converted to a String) as the node value, and with the "leafNode" property set to "true".


toString

private java.lang.String toString(java.lang.Object value)
Returns the value as a String in an appropriate format depending on the data type. A null value is returned as "null", an Object or primitive type array or a Collection is returned as a comma-separated list of values, a Map is returned as a comma-separated list of "key=value" entries.


toObjects

private java.lang.Object[] toObjects(boolean[] arr)
Returns an Object array with the values of the array.


toObjects

private java.lang.Object[] toObjects(byte[] arr)
Returns an Object array with the values of the array.


toObjects

private java.lang.Object[] toObjects(char[] arr)
Returns an Object array with the values of the array.


toObjects

private java.lang.Object[] toObjects(double[] arr)
Returns an Object array with the values of the array.


toObjects

private java.lang.Object[] toObjects(float[] arr)
Returns an Object array with the values of the array.


toObjects

private java.lang.Object[] toObjects(int[] arr)
Returns an Object array with the values of the array.


toObjects

private java.lang.Object[] toObjects(short[] arr)
Returns an Object array with the values of the array.


toObjects

private java.lang.Object[] toObjects(long[] arr)
Returns an Object array with the values of the array.


format

private java.lang.String format(java.util.Iterator i)
Returns a String with comma-separated list of the values represented by the Iterator.


format

private java.lang.String format(java.util.Map map)
Returns a String with comma-separated list of the Map entries, with each entry as "key = value".