PhaseListener gets added twice. Is this a bug in myfaces or am I doing something wrong. Here is the detailed info:

Part I:
=====
I have made this entry only once in my faces-config.xml file:

<lifecycle>
<phase-listener>
com.ebay.channel.phaselisteners. AuthorizationPhaseListener
</phase-listener>
</lifecycle>

Part II:
=====
I notice that The AuthorizationPhaseListener gets added twice. I can confirm this in the LifeCycleImpl.java class when viewed under the debugger.
I looked at this method:
private void informPhaseListenersAfter(FacesContext facesContext, PhaseId phaseId)
  {
      PhaseListener[] phaseListeners = getPhaseListeners();
      for (int i = 0; i < phaseListeners.length; i++)
      {
          PhaseListener phaseListener = phaseListeners[i];
          int listenerPhaseId = phaseListener.getPhaseId().getOrdinal();
          if (listenerPhaseId == PhaseId.ANY_PHASE.getOrdinal() ||
              listenerPhaseId == phaseId.getOrdinal())
          {
phaseListener.afterPhase(new PhaseEvent(facesContext, phaseId, this));
          }
      }

  }


[[[[The phaseListeners Array has AuthorizationPhaseListener twice.
The second call to phaseListener.afterPhase method of the AuthorizationPhaseListener causes an exception. ]]]]



Part III:
=====
This is my afterPhase method in the AuthorizationPhaseListener:
I want to restrict access to all pages other than the UserLogin page. If the user is logged in then afterPhase returns without redirecting user to the UserLogin page.

public void afterPhase(PhaseEvent event) {

FacesContext fc =  event.getFacesContext();
if(fc.getViewRoot() == null ) {
return;
} else {//ViewRoot Not null
if(fc.getViewRoot().getViewId().equals("/demo/UserLogin.jsp")) {
//Do not redirect if you are trying to reach the Login Page.
return;
}
}

ApplicationFactory appFactory = (ApplicationFactory) FactoryFinder
.getFactory(FactoryFinder.APPLICATION_FACTORY);
Application app = appFactory.getApplication();
UserSessionBean usb = (UserSessionBean)app.createValueBinding(
"#{userSessionBean}").getValue(fc);
if(!usb.isUserLoggedIn()) {
          ExternalContext externalContext = fc.getExternalContext();
          ViewHandler viewHandler = fc.getApplication().getViewHandler();
String redirectPath = viewHandler.getActionURL(fc, "/Test/demo/UserLogin.faces");

          try
          {
externalContext.redirect(externalContext.encodeActionURL(redirectPath));
          }
          catch (IOException e)
          {
              throw new FacesException(e.getMessage(), e);
          }
          fc.responseComplete();

          /*
NavigationHandler nh = app.getNavigationHandler();
nh.handleNavigation(fc, null, "goto_login_page");
*/
}
}


Reply via email to