How to access FacesContext in Hibernate Envers without Seam?

泄露秘密 提交于 2019-12-12 19:11:32

问题


We're implementing Envers in our project for database auditing, and have run into a snag. We don't know how to determine what user is making the change. All the examples I can find use Seam and their Component.getInstance technique.

  • Glassfish 3.1.2.2
  • Mojarra 2.1.13
  • Hibernate 4.1.6.Final

Here's our custom Revision Entity

@Entity
@RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity {
    @Id
    @GeneratedValue
    @RevisionNumber
    private int id;

    private String login;

    getters/setters...
}

And out custom Revision Listener

public class CustomRevisionListener implements RevisionListener {
    @Override
    public void newRevision(Object revisionEntity) {
        CustomRevisionEntity rev = (CustomRevisionEntity) revisionEntity;

        //how do we get the FacesContext/remote user?

        rev.setLogin("unknown");
    }
}

We've tried injection and scoping to no avail, we also attempted this ClassLoader solution I found for something that seemed to be a similar circumstance, but getCurrentInstance() was still null;

Thanks for your help!


回答1:


adamw was correct, FacesContext.getCurrentInstance() does work in a custom RevisionListener.

We had a javax.servlet.Filter implementation attempting to handle the transaction in the doFilter() method which was for whatever reason causing FacesContext.getCurrentInstance() to return null in the RevisionListener. We removed that from the web.xml yesterday for a different reason and when I tried getCurrentInstance() without it this morning, everything works fine.

Thanks for the replies everyone, maybe this will help someone else someday.



来源:https://stackoverflow.com/questions/14609394/how-to-access-facescontext-in-hibernate-envers-without-seam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!