FacesContext.getCurrentInstance() returns null in RevisionListener

551 views Asked by At

I have a jsf based web application using hibernate for persistence. I also use Hibernate envers for auditing, therefore I want to implement a custom RevisionListener to save the current username in revision information. But FacesContext.getCurrentInstance() returns null.

What's the best approach to get the username in a org.hibernate.envers.RevisionListener implementation.

public class RevisionListener implements org.hibernate.envers.RevisionListener {

   @Override
   public void newRevision(Object object) {
      Revinfo revInfo = (Revinfo)object;

     revInfo.setUsername(getUserName());
   }

   private String getUserName() {
      //FacesContext.getCurrentInstance() throws Nullpointerexception
      Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
      if (request instanceof HttpServletRequest) {
         return ((HttpServletRequest)request).getRemoteUser();
      }

      return null;
   }

}
0

There are 0 answers