Centralized exception reporting with Java Server Faces

Here is described the solution of the student project TriplePlay in summer 2005.

The JavaServer Faces life cycle (see https://docs.oracle.com/javaee/7/tutorial/jsf-intro006.htm) is directed by an implementation of the interface Lifecycle (in the package javax.faces.lifecycle. The reference implementation of Sun is com.sun.faces.lifecycle.LifecycleImpl).

When the web application is started, a LifecycleFactory (also in package javax.faces.lifecycle, reference implementation of Sun is com.sun.faces.lifecycle.LifecycleFactoryImpl) is asked to return a Lifecycle object. Normally the standard implementation will be taken. But you have the possibility to change the LifecycleFactory, and to return your own Lifecycle implementation.

In detail: Here are the two classes, which we have written. You can subclass the reference implementation, or you can completely implement the interface. We take the first approach.

package myPackage; //MyLifecycleFactoryImpl.java
  
import javax.faces.FacesException;
import javax.faces.lifecycle.Lifecycle;

public class MyLifecycleFactoryImpl 
extends com.sun.faces.lifecycle.LifecycleFactoryImpl 
{

  public Lifecycle getLifecycle(final String i_lifecycleId) 
  throws FacesException 
  {
    return new MyLifecycleImpl();
  }

}

/////////////////////////////////////////////////////////////////////
package myPackage; //MyLifecycleImpl.java 

import javax.faces.FacesException;
import javax.faces.context.FacesContext;

public class MyLifecycleImpl extends com.sun.faces.lifecycle.LifecycleImpl {

  public void execute(final FacesContext io_facesContext) {
    try {
      super.execute(io_facesContext);
    } catch(final FacesException e) {
      // handleExceptions here
    }
  }

}      

Now we still have to instruct JavaServer Faces, that we want to use our own LifecycleFactory implementation instead of the default implementation. This we have to do in the file faces-config.xml (a configuration file similar to struts-config.xml).

<?xml version="1.0" encoding="UTF-8"?>

<!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>
       ...
  <factory>
    <lifecycle-factory>myPackage.MyLifecycleFactoryImpl</lifecycle-factory>
  </factory>
       ...
</faces-config>        

Having done this, our implementation of the LifecycleFactory will be used, which in turn will deliver in the method getLifecycle (see before) our own Lifecycle implementation.

If there remain questions, I will try to answer them. You can mention my addresses in this little howto, so that future students can ask me, if necessary.

Siamak Haschemi, www.haschemi.org E-Mail: info (at) haschemi.org,