website/content/blog/adding-audit-information-to-entities-in-the-seam-framework.md
2023-10-19 01:04:39 +02:00

62 lines
2.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 'Adding audit information to entities in the Seam framework.'
date: 2008-11-11T13:15:51.000Z
draft: false
taxonomies:
tags: ["jvm"]
categories: ["programming"]
---
Since we write a lot of stuff for use in the production side of the business we need to comply with rules which allow effective control that the processes and procedures have been followed. In the automotive industry people take these measures seriously.
One of these requirements we often get is that all configuration data elements must contain information on who created this information and when, and similarly for the last update.
Since the advent of EJB3 and JPA we now have nice POJO entities which clearly show the structure of the data and the last thing we want is to add a bunch of boilerplate code which multiplies the number of code lines with redundant stuff.
This [blogpost of Daniel Pfeifer](http://www.mindbug.org/2008/04/automatic-tracing-of-entity-changes.html) shows how to do this using an entity listener. However, it does not work using Seam since we cannot get the user information through the EJBContext, at least not on JBoss 4.2.3.
After wasting the better part of an afternoon barking up the wrong tree, the answer came to me next morning during breakfast. I have the unhealthy habit of reading PDF-files between bites, in this case chapter 15 of the Seam manual. The security section. (Yes, I know, I hould have done this sooner, you know : "if all else fails, read the manual.")
Seam provides a nice utility class aptly named _org.jboss.seam.security.Identity_ which provides all we need to get the name of the person logged in the current session. So replacing the implementation of _getCallerIdentity()_ with the following, we now have nice audit trails.
>
> /\*\*
> \* Get the name of the currently logged in user.
> \*
> \* This part contains the framework specific code to get the active username.
> \* In this case we use the Seam provided Identity.instance() to get at this
> \* information.
> \*
> \* @param t
> \* @return
> \*/
> private String getCallerIdentity(Traceable t) {
>    try {
>      // get the identity principal to get information about the current active user
>      Principal principal = Identity.instance().getPrincipal();
>      if (log.isDebugEnabled())
>        log.debug("AuditLogger.getCallerIdentity returned " + principal.getName());
>
>      return principal.getName();
>
>    } catch (Exception e) {
>      log.error("Exception received : " + e.getClass().getName() + ":" + e.getMessage());
>      return "unknown";
>    }
> }
I provided both the interface and mapped superclass implementations for convenience. All what is now remaining is to extend our configuration POJO's from the TraceableEntity class (and create an update script, and modify the UI to get at the audit info, ...) to make our stuff auditable.