
package de.ivu.elect.jee.faces.context;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;

// ELECT-11030, cf. /META-INF/services/javax.faces.context.FacesContextFactory and https://docs.oracle.com/javaee/7/api/javax/faces/FactoryFinder.html
public class ElectFacesContextFactory extends FacesContextFactory {

  private FacesContextFactory wrapped;

  public ElectFacesContextFactory() {

  }

  public ElectFacesContextFactory(FacesContextFactory wrapped) {
    this.wrapped = wrapped;
  }

  @Override
  public FacesContext getFacesContext(Object context, Object request, Object response, Lifecycle lifecycle) throws FacesException {
    FacesContext wrappedContext = wrapped.getFacesContext(context, request, response, lifecycle);

    if (wrappedContext instanceof ElectFacesContext) {
      return wrappedContext;
    }

    return new ElectFacesContext(wrappedContext);
  }

  @Override
  public FacesContextFactory getWrapped() {
    return wrapped;
  }
}
