logo
Home Help Desk

JSF Phase Tracker


Download URL: http://www.jsfTutorials.net/download/phaseTracker/phaseTracker.jar

Life Cycle is a fundamental concept of JSF. Pay attention of it. This tiny library prints out the phases application passes.

Just download the phaseTracker.jar file and put it into the WEB-INF/lib folder of the application you want to track. No more steps are required. After that, the application will print something like that:

10.01.2005 17:09:41 org.exadel.jsf.PhaseTracker afterPhase
INFO: AFTER RESTORE_VIEW 1
10.01.2005 17:09:41 org.exadel.jsf.PhaseTracker beforePhase
INFO: BEFORE APPLY_REQUEST_VALUES 2
10.01.2005 17:09:41 org.exadel.jsf.PhaseTracker afterPhase
INFO: AFTER APPLY_REQUEST_VALUES 2
10.01.2005 17:09:41 org.exadel.jsf.PhaseTracker beforePhase
INFO: BEFORE PROCESS_VALIDATIONS 3
10.01.2005 17:09:41 org.exadel.jsf.PhaseTracker afterPhase
INFO: AFTER PROCESS_VALIDATIONS 3
10.01.2005 17:09:41 org.exadel.jsf.PhaseTracker beforePhase
INFO: BEFORE RENDER_RESPONSE 6
10.01.2005 17:09:41 org.exadel.jsf.PhaseTracker afterPhase
INFO: AFTER RENDER_RESPONSE 6

Here is a content of the /META-INF/faces-config.xml:

<?xml version="1.0"?>

<!DOCTYPE faces-config PUBLIC
   "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
   "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">

<faces-config>
   <lifecycle>
      <phase-listener>org.exadel.jsf.PhaseTracker</phase-listener>
   </lifecycle>
</faces-config>

The content of the Phase Tracker class:

package org.exadel.jsf;

import java.util.logging.Logger;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseListener;
import javax.faces.context.FacesContext;


public class PhaseTracker implements PhaseListener {

	private static final String PHASE_PARAM ="org.exadel.helper.phaseTracker.cphase";
    private static final Logger logger = Logger.getLogger("org.exadel.helper");
    private static String cphase = null;

   public void setPhase(String newValue) { cphase = newValue; }
   
   public PhaseId getPhaseId() {

    PhaseId phaseId = PhaseId.ANY_PHASE;
   	
   	if(cphase == null) {
         FacesContext context = FacesContext.getCurrentInstance();
         if (context == null)
         	return phaseId;
         cphase = (String)context.getExternalContext()
		 	.getInitParameter(PHASE_PARAM);
      }

      if(cphase != null) {
         if("RESTORE_VIEW".equals(cphase)) 
            phaseId = PhaseId.RESTORE_VIEW;
         else if("APPLY_REQUEST_VALUES".equals(cphase)) 
            phaseId = PhaseId.APPLY_REQUEST_VALUES;
         else if("PROCESS_VALIDATIONS".equals(cphase)) 
            phaseId = PhaseId.PROCESS_VALIDATIONS;
         else if("UPDATE_MODEL_VALUES".equals(cphase)) 
            phaseId = PhaseId.UPDATE_MODEL_VALUES;
         else if("INVOKE_APPLICATION".equals(cphase)) 
            phaseId = PhaseId.INVOKE_APPLICATION;
         else if("RENDER_RESPONSE".equals(cphase)) 
            phaseId = PhaseId.RENDER_RESPONSE;
         else if("ANY_PHASE".equals(cphase)) 
            phaseId = PhaseId.ANY_PHASE;
      }
      return phaseId;
   }
   public void beforePhase(PhaseEvent e) {
      logger.info("BEFORE " + e.getPhaseId());
   }
   public void afterPhase(PhaseEvent e) {
      logger.info("AFTER " + e.getPhaseId());
   }
}


Credit: The library has been created based on the example set of the "Core JavaServer Faces" book (http://horstmann.com/corejsf/)

See Also:

JavaServer Faces, Web Applications Made Easier


The short introduction to the JavaServer Faces