[Java] Spring AOP question

grant@bluetube.com grant at bluetube.com
Tue Jan 31 12:48:58 EST 2006


Hey guys, just joined the list on advice from Mr Allen.

I have something that seems like it would be a good AOP fit but breaks part way through...

I have some classes that need auditing and I CAN'T change the method signature on these classes.

Lets show a very simple example class that uses our auditing class without spring :

class SecurityManager
{

 public login(id, password)
 {

   Audit = new Audit(id, this.getClass().getName(), "login");

	audit.markReceipt();  // sets the timestamp of when we started our work



   // log the user in and write to the audit log

    if (someothersystem.login(id, password) == true)
     	audit.write("user " + id "logged in at " + new Date().toString());
    else
        audit.write("user " + id " failed to login");

   audit.markEnd();

 }


}

Every method needs an audit object that knows the method that is being audited and the user that has called this object 

Audit = new Audit(id, this.getClass().getName(), "login");

so the method is id, the name of the class and the method that is called

then we mark receipt of the call to say our business logic has started by :

audit.markReceipt();

now, this class needs to write audits based on different things, sometimes it wil be purely business so I don't want to tie the audit message to sucesss/failure as there may be many audits, for example if a user has failed to login but they have also tried 3 times before I may want to audit that...

so 

audit.write("some free form message");  is very method specific


at the end, all methods need

audit.markServiceEnd();


So I jumped into AOP with both feet, I created a methodBeforeAdvice that creates the audit object and calls the markReceipt

I then got stuck...

The login method needs access to the audit object which has "state", so it can call the "write" method.  But i can't pass it as an argument as the method signature can't be changed and giving it an audit object does seem to polute a very business oriented method.  In discussions with chris I thought of creating an audit manager but each audit object is unique to a method call.  The 3 parts of an audit, markReceipt, write, and MarkEnd are combined into 1 log entry in the audit log.  so I don't want an atomic call like log4j as the data would be written 3 times and our audits are big enough with just 1 line...

Any ideas, I can re-architect the audit, but I can't change the method signatures of the objects that need auditing, I can change the code inside those methods, just not what they are passed or what they return...


Thanks guys.

Grant.










More information about the Java mailing list