/** @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent) */
 public void beforePhase(PhaseEvent event) {
   LOGGER.debug("Before phase {0}", event.getPhaseId());
   if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
     UIViewRoot viewRoot = event.getFacesContext().getViewRoot();
     if (viewRoot != null) {
       LOGGER.debug(
           "Subscribing to event {0} with listener {1}", PRERENDER_EVENT_CLASS, HOOKING_EVENT);
       viewRoot.subscribeToEvent(PRERENDER_EVENT_CLASS, HOOKING_EVENT);
     }
   }
 }
 /** @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent) */
 public void afterPhase(PhaseEvent event) {
   LOGGER.debug("After phase {0}", event.getPhaseId());
   if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
     UIViewRoot viewRoot = event.getFacesContext().getViewRoot();
     if (viewRoot != null) {
       ServiceRegisterJSComponent serviceRegister =
           InternalEventListener.findServiceRegister(FacesContext.getCurrentInstance());
       if (serviceRegister != null) {
         LOGGER.debug("Found service register with clientId {0}", serviceRegister.getClientId());
       } else {
         LOGGER.debug("Service register not found!");
       }
     }
   }
 }
 public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
   if (event.getClass() == PRERENDER_EVENT_CLASS) {
     LOGGER.debug("Prerender event {0} caught", event);
     // FIXME seriously, do it!
     FacesContext ctx = FacesContext.getCurrentInstance();
     UIViewRoot viewRoot = ctx.getViewRoot();
     if (viewRoot != null) {
       ServiceRegisterJSComponent comp;
       if ((comp = findServiceRegister(ctx)) == null) {
         LOGGER.debug("Cannot find a service register register component, adding one");
         viewRoot
             .getChildren()
             .add(
                 ctx.getApplication()
                     .createComponent(ServiceRegisterJSComponent.COMPONENT_TYPE));
         viewRoot.markInitialState();
       } else {
         LOGGER.debug("Found service register component with clientId {0}", comp.getClientId());
       }
     } else {
       LOGGER.error("Could not find a view root");
     }
   }
 }
/**
 * Phase listener for invoking logic
 *
 * @author Christian Heike
 */
public class CotentinPhaseListener implements PhaseListener {

  public static final String SERVLET_LIBRARY = "COTENTIN_SERVICE";
  public static final String WRAPPER_FORM_ID = "COTENTIN_WRAPPER_FORM";
  private static final ComponentSystemEventListener HOOKING_EVENT = new InternalEventListener();
  private static final Class<PreRenderViewEvent> PRERENDER_EVENT_CLASS = PreRenderViewEvent.class;
  private static final LoggerFacade LOGGER =
      LoggerFacade.getLogger(ServiceRegisterJSRenderer.class);
  private static final long serialVersionUID = -2533579777741458787L;

  /** @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent) */
  public void afterPhase(PhaseEvent event) {
    LOGGER.debug("After phase {0}", event.getPhaseId());
    if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
      UIViewRoot viewRoot = event.getFacesContext().getViewRoot();
      if (viewRoot != null) {
        ServiceRegisterJSComponent serviceRegister =
            InternalEventListener.findServiceRegister(FacesContext.getCurrentInstance());
        if (serviceRegister != null) {
          LOGGER.debug("Found service register with clientId {0}", serviceRegister.getClientId());
        } else {
          LOGGER.debug("Service register not found!");
        }
      }
    }
  }

  /** @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent) */
  public void beforePhase(PhaseEvent event) {
    LOGGER.debug("Before phase {0}", event.getPhaseId());
    if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
      UIViewRoot viewRoot = event.getFacesContext().getViewRoot();
      if (viewRoot != null) {
        LOGGER.debug(
            "Subscribing to event {0} with listener {1}", PRERENDER_EVENT_CLASS, HOOKING_EVENT);
        viewRoot.subscribeToEvent(PRERENDER_EVENT_CLASS, HOOKING_EVENT);
      }
    }
  }

  /** @see javax.faces.event.PhaseListener#getPhaseId() */
  public PhaseId getPhaseId() {
    return PhaseId.ANY_PHASE;
  }

  public static class InternalEventListener implements ComponentSystemEventListener {

    public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
      if (event.getClass() == PRERENDER_EVENT_CLASS) {
        LOGGER.debug("Prerender event {0} caught", event);
        // FIXME seriously, do it!
        FacesContext ctx = FacesContext.getCurrentInstance();
        UIViewRoot viewRoot = ctx.getViewRoot();
        if (viewRoot != null) {
          ServiceRegisterJSComponent comp;
          if ((comp = findServiceRegister(ctx)) == null) {
            LOGGER.debug("Cannot find a service register register component, adding one");
            viewRoot
                .getChildren()
                .add(
                    ctx.getApplication()
                        .createComponent(ServiceRegisterJSComponent.COMPONENT_TYPE));
            viewRoot.markInitialState();
          } else {
            LOGGER.debug("Found service register component with clientId {0}", comp.getClientId());
          }
        } else {
          LOGGER.error("Could not find a view root");
        }
      }
    }

    public static ServiceRegisterJSComponent findServiceRegister(FacesContext ctx) {
      for (UIComponent child : ctx.getViewRoot().getChildren()) {
        if (child instanceof ServiceRegisterJSComponent) {
          return (ServiceRegisterJSComponent) child;
        }
      }
      return null;
    }
  }
}