[Java] Spring AOP question

Nicolas Désy liguorien at hotmail.com
Tue Jan 31 16:19:42 EST 2006


I don't know exactly how you want to manage the audit for each method calls. 
But you can store the audit of the current stack in an List.

**** CODE ****

import java.util.*;

public class AuditRepository {

    private static ThreadLocal<List<Audit>> _context = new 
ThreadLocal<List<Audit>>();

    public static void addAudit(Audit audit){
        List<Audit> audits = null;
        if(_context.get() == null){
            audits = new ArrayList<Audit>();
            _context.set(audits);
        }else{
            audits = _context.get();
        }
        audits.add(audit);
    }

    public static Audit getCurrentAudit(){
        final List<Audit> audits = _context.get();
        if(audits.size() == 0) return null;
        return audits.get(audits.size()-1);
    }

    public static void removeCurrentAudit(){
        final List<String> audits = _context.get();
        if(audits.size() > 0) {
            audits.remove(audits.size()-1);
        }
    }
}

**** CODE ****

I don't think this is exactly what you want, but the idea is here :)

-Nicolas


----- Original Message ----- 
From: <grant at bluetube.com>
To: <liguorien at hotmail.com>
Sent: Tuesday, January 31, 2006 3:07 PM
Subject: Re: [Java] Spring AOP question


>I was quite excited to try this but then thought..
>
> lets asssume the login method that is audited needs to call another method 
> on the same or different object that also is audited, the original audit 
> values would get overwritten as it would still be the same thread when you 
> went to the auditManager to get the audit object instance.
>
> hmmm
>
> Grant.
>
> ----- Original Message -----
> From: Nicolas_Dsy liguorien at hotmail.com
> To: Java at osflash.org
> Sent: 1/31/06 2:02 PM
> Subject: Re: [Java] Spring AOP question
>
>> Hi Grant,
>>
>> Have you tried using a ThreadLocal to store the audit object ?
>>
>> /***** code *********/
>>
>> public class AuditRepository {
>>
>>     private static ThreadLocal _context = new ThreadLocal();
>>
>>     public static void setAudit(Audit audit){
>>         _context.set(audit);
>>     }
>>
>>     public static Audit getAudit(){
>>         return (Audit) _context.get();
>>     }
>>
>>     public static void clear(){
>>         _context.set(null);
>>     }
>> }
>>
>>
>>
>> private void someMethod(){
>>     AuditRepository.getAudit().write("foo");
>> }
>>
>>
>> Audit = new Audit(id, this.getClass().getName(), "login");
>> audit.markReceipt();
>>
>> AuditRepository.setAudit(audit);
>>
>> someMethod();
>>
>> audit.markEnd();
>>
>> AuditRepository.clear();
>>
>> /***** code *********/
>>
>>
>> Nicolas
>>
>> ----- Original Message ----- 
>> From:
>> To:
>> Sent: Tuesday, January 31, 2006 12:48 PM
>> Subject: [Java] Spring AOP question
>>
>>
>> >
>> > 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.
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>> >
>>
>>
>> --------------------------------------------------------------------------------
>>
>>
>> > _______________________________________________
>> > Java mailing list
>> > Java at osflash.org
>> > http://osflash.org/mailman/listinfo/java_osflash.org
>> >
>>
>> _______________________________________________
>> Java mailing list
>> Java at osflash.org
>> http://osflash.org/mailman/listinfo/java_osflash.org
>
> 



More information about the Java mailing list