3

I have this PhaseListner:

public class CheckAccessInterceptor implements PhaseListener {
    private static final long serialVersionUID = -311454347719850720L;
    private static Logger logger = Logger.getLogger(CheckAccessInterceptor.class);

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.ANY_PHASE;
    }

    public void beforePhase(PhaseEvent event) {
        System.out.println("START PHASE " + event.getPhaseId());
    }

    public void afterPhase(PhaseEvent event) {
        System.out.println("END PHASE " + event.getPhaseId());
    }

}

Is it possible to get ManagedBean's name and invoked method's name from PhaseEvent object? How?

UPDATED:

I find solution here but looks like it doesnt work with Ajax. So I dont know what to do.

Community
  • 1
  • 1
Divers
  • 9,531
  • 7
  • 45
  • 88
  • possible duplicate of [Logging the invoked managed bean action in a PhaseListener](http://stackoverflow.com/questions/4788454/logging-the-invoked-managed-bean-action-in-a-phaselistener) – maple_shaft Sep 07 '12 at 11:03
  • 2
    I updated the answer which you found to make it ajax compatible. – BalusC Sep 07 '12 at 11:48

2 Answers2

2

Anything is possible, and I am sure there is a far easier way to do this, but I gave up and went straight to the source.

The UIViewRoot has a private field events that during the INVOKE APPLICATION phase will have a list of lists that have all of the events to broadcast at each respective Phase. The only way I could think of doing this is to use reflection to obtain that list.

Field field = UIViewRoot.class.getDeclaredField("events");
field.setAccessible(true);
ArrayList<ArrayList<SystemEvent>> events =
  (ArrayList<ArrayList<SystemEvent>>)field.get(
     FacesContext.getCurrentInstance().getViewRoot());

for (ArrayList<SystemEvent> phaseEvents : events) {
   for (SystemEvent event : phaseEvents) {
      // is this the event you are looking for?
   }
}

I think somewhere I hear Gosling and the original authors of the JSF spec crying right now.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • @Divers Yes. I just ran this code in the Before PhaseEvent for INVOKE APPLICATION and I found my `AjaxBehaviorEvent`. If you don't see the event then your Ajax postback is not executing the event. It sounds like you have something else wrong. – maple_shaft Sep 07 '12 at 11:02
  • 1
    They not only, I was also crying :) – BalusC Sep 07 '12 at 11:48
  • @BalusC Are you crying because we are breaking encapsulation in JSF or because you wish we didn't have to? :) – maple_shaft Sep 07 '12 at 12:16
1

So right answer is here

But I must warn you, it work with actions only, not actionlisters.

maple_shaft's solution works too, but in my opinion BalusC's solution better.

Community
  • 1
  • 1
Divers
  • 9,531
  • 7
  • 45
  • 88