Hiya Tomasz.

I eventually found some time to put together one of my ideas, here's an example using 
it (uses the annotation compiler).

The annotation interfaces.
package knrc.beforeAfter2;
  | 
  | /**
  |  * @author kevin
  |  */
  | public interface after
  | {
  | }

package knrc.beforeAfter2;
  | 
  | /**
  |  * @author kevin
  |  */
  | public interface afterThrowable
  | {
  | }

package knrc.beforeAfter2;
  | 
  | /**
  |  * @author kevin
  |  */
  | public interface before
  | {
  | }

The before/after/afterThrowable aspect.
package knrc.beforeAfter2;
  | 
  | import org.jboss.aop.joinpoint.Invocation;
  | import org.jboss.aop.joinpoint.MethodInvocation;
  | 
  | public class AspectBeforeAfter
  | {
  |    public final static String RESULT_KEY = AspectBeforeAfter.class.getName() + 
".result" ;
  |    public final static String THROWABLE_KEY = AspectBeforeAfter.class.getName() + 
".throwable" ;
  |    
  |    public Object invokeBefore(final MethodInvocation methodInvocation)
  |       throws Throwable
  |    {
  |       final Invocation invocation = getInvocation(methodInvocation) ;
  |       methodInvocation.invokeNext() ;
  |       return invocation.invokeNext() ;
  |    }
  |    
  |    public Object invokeAfter(final MethodInvocation methodInvocation)
  |       throws Throwable
  |    {
  |       final Invocation invocation = getInvocation(methodInvocation) ;
  |       final Object result = invocation.invokeNext() ;
  |       invocation.addResponseAttachment(RESULT_KEY, result) ;
  |       try
  |       {
  |          return methodInvocation.invokeNext() ;
  |       }
  |       finally
  |       {
  |          invocation.getResponseContextInfo().remove(RESULT_KEY) ;
  |       }
  |    }
  |    
  |    public Object invokeAfterThrowable(final MethodInvocation methodInvocation)
  |       throws Throwable
  |    {
  |       final Invocation invocation = getInvocation(methodInvocation) ;
  |       try
  |       {
  |          return invocation.invokeNext() ;
  |       }
  |       catch (final Throwable th)
  |       {
  |          invocation.addResponseAttachment(THROWABLE_KEY, th) ;
  |          try
  |          {
  |             return methodInvocation.invokeNext() ;
  |          }
  |          finally
  |          {
  |             invocation.getResponseContextInfo().remove(THROWABLE_KEY) ;
  |          }
  |       }
  |    }
  |    
  |    private static Invocation getInvocation(final MethodInvocation methodInvocation)
  |    {
  |       return (Invocation)methodInvocation.getArguments()[0] ;
  |    }
  | }

The POJO code
package knrc.beforeAfter2;
  | 
  | /**
  |  * @author kevin
  |  */
  | public class Pojo
  | {
  |    public void method1()
  |    {
  |       System.out.println("Pojo method1 here...") ;
  |    }
  |    
  |    public static void main(final String args[])
  |    {
  |       new Pojo().method1() ;
  |       new Aspect3D();
  |    }
  | }

The POJO aspects (3D and 4D)
package knrc.beforeAfter2;
  | 
  | import org.jboss.aop.joinpoint.Invocation;
  | 
  | public class Aspect3D
  | {
  |    /**
  |     * @@knrc.beforeAfter2.before 
  |     */
  |    public Object before(final Invocation invocation)
  |    {
  |       System.out.println("2-executing 3D operations") ;
  |       databaseOperations() ;
  |       return null ;
  |    }
  |    
  |    private void databaseOperations()
  |    {
  |       throw new RuntimeException("X") ;
  |    }
  | }

package knrc.beforeAfter2;
  | 
  | import org.jboss.aop.joinpoint.MethodInvocation;
  | 
  | public class AspectExceptionHandler4D
  | {
  |    /**
  |     * @@knrc.beforeAfter2.before 
  |     */
  |    public Object before(final MethodInvocation methodInvocation)
  |    {
  |       System.out.println("1-Begin of 4D-Exception handler concern") ;
  |       return null ;
  |    }
  |    
  |    /**
  |     * @@knrc.beforeAfter2.after
  |     */
  |    public Object after(final MethodInvocation methodInvocation)
  |    {
  |       System.out.println("After thrown exception - You shouldn't see this!!!") ;
  |       return methodInvocation.getResponseAttachment(AspectBeforeAfter.RESULT_KEY) ;
  |    }
  |    
  |    /**
  |     * @@knrc.beforeAfter2.afterThrowable
  |     */
  |    public Object afterThrowable(final MethodInvocation methodInvocation)
  |       throws Throwable
  |    {
  |       System.out.println("Handle 3D exceptions here") ;
  |       return null ;
  |       //throw 
(Throwable)methodInvocation.getResponseAttachment(AspectBeforeAfter.THROWABLE_KEY) ;
  |    }
  | }

The aop.xml
<?xml version="1.0" encoding="UTF-8"?>
  | <aop>
  |    <aspect name="aspect3D" class="knrc.beforeAfter2.Aspect3D"/>
  |    <aspect name="aspectExceptionHandler4D" 
class="knrc.beforeAfter2.AspectExceptionHandler4D"/>
  |    <aspect name="aspectBeforeAfter" class="knrc.beforeAfter2.AspectBeforeAfter"/>
  |    
  |    <bind pointcut="execution(* *->@knrc.beforeAfter2.before(..))">
  |      <advice name="invokeBefore" aspect="aspectBeforeAfter"/>
  |    </bind>
  |    <bind pointcut="execution(* *->@knrc.beforeAfter2.after(..))">
  |      <advice name="invokeAfter" aspect="aspectBeforeAfter"/>
  |    </bind>
  |    <bind pointcut="execution(* *->@knrc.beforeAfter2.afterThrowable(..))">
  |      <advice name="invokeAfterThrowable" aspect="aspectBeforeAfter"/>
  |    </bind>
  |    
  |    <bind pointcut="execution(* knrc.beforeAfter2.Pojo->method1())">
  |      <advice name="before" aspect="aspect3D"/>
  |    </bind>
  |    
  |    <bind pointcut="execution(* knrc.beforeAfter2.Aspect3D->before(..))">
  |      <advice name="afterThrowable" aspect="aspectExceptionHandler4D"/>
  |      <advice name="after" aspect="aspectExceptionHandler4D"/>
  |      <advice name="before" aspect="aspectExceptionHandler4D"/>
  |    </bind>
  | </aop>

This is still not as neat as the solution you were after but it is close.  The only 
part to watch out for is the ordering of the 4D advices, the afterThrowable advice 
should be the first one so that the thrown throwable falls throught the after/before 
advices.

I'm sure there are other improvements that can be made ;-).

Kev


View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3850623#3850623

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3850623


-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
JBoss-Development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to