开发者

Envers custom revision listener

开发者 https://www.devze.com 2023-04-06 08:15 出处:网络
I am using Hibernate Envers in a Tomcat environment. It works fine. I do however need to be able to add the username of the user that changed the data that is to be audited. This can be done by implem

I am using Hibernate Envers in a Tomcat environment. It works fine. I do however need to be able to add the username of the user that changed the data that is to be audited. This can be done by implementing your own version of RevisionEntity. Then you also have to implement a custom RevisionListener that will populate the extra information you want to audit. I need to audit the username that has changed the data that are to be audited. In the documentation it is a example on how to do this with Seam. In the RevisionListener they call:

Identity identity = (Identity) Component.getInstance("org.jboss.seam.security.identity");

To obtain the username. In my project we have split the project into seperate modules for database and web. I need to implement my custum revision listener in my database module to be able to get the curren开发者_运维技巧tly logged in username. I can not move the RevisionListener to the web package and introduce a dependency on the web module from the database module. How can I obtain the current username in a way that handles multiple users logged in at the same time in my custom RevisionListener`?

The best solution would be one that worked in most containers.


I can understand you wanting to limit coupling between modules. I have done this in the past using Spring and JSF. I had Data Access code in their own projects and my web projects were dependant on the data access jars.

I wrote the listener in another project that knew about the Hibernate classes and JSF API. I was able to use the JSF API to find the logged in user.

That way the web app is dependent on the data and the listener project. The data access project doesn't need to know where it is used and even if there is auditing happening. And the listener is configured in the web project.


This sounds like a job for Context and Dependency Injection (CDI). You can define an interface such as ClientInfo which has getUsername and getIPAddress abstract methods. Then in your presentation layer create a concrete implementation of that interface, say JSFClientInfo, that uses FacesContext to grab the username and IP Address. The business layer component can simply use @Inject ClientInfo.

Sadly, I think you'll have to actually use a service locator instead though because the org.hibernate.envers.RevisionListener lifecycle is not maintained by CDI.

0

精彩评论

暂无评论...
验证码 换一张
取 消