Title: Message
I have
implemented a ContainerInterceptor that always executed the after method, though
it still allows the after processing to be conditional on an exception being
thrown.
Shall I add it to
xwork ?
01 /* 02 * Created on 15/07/2003 03 * 04 */ 05 package interceptor; 06 07 import com.opensymphony.xwork.ActionInvocation; 08 import com.opensymphony.xwork.interceptor.Interceptor; 09 10 /** 11 * 12 * An interceptor that executes an operation before and after the invocation, irrespective of whether or not 13 * an exception is thrown. 14 * 15 * @author CameronBraid 16 * 17 */ 18 public abstract class ContainerInterceptor implements Interceptor 19 { 20 21 /** 22 * 23 */ 24 public ContainerInterceptor() 25 { 26 super(); 27 } 28 29 /* (non-Javadoc) 30 * @see com.opensymphony.xwork.interceptor.Interceptor#destroy() 31 */ 32 public abstract void destroy(); 33 34 /* (non-Javadoc) 35 * @see com.opensymphony.xwork.interceptor.Interceptor#init() 36 */ 37 public abstract void init(); 38 39 public abstract void before(ActionInvocation invocation) throws Exception; 40 public abstract void after(ActionInvocation invocation, String result, Exception exception) throws Exception; 41 42 /* (non-Javadoc) 43 * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation) 44 */ 45 public String intercept(ActionInvocation invocation) throws Exception 46 { 47 before(invocation); 48 49 Exception exception = null; 50 String result = null; 51 52 try 53 { 54 result = invocation.invoke(); 55 } 56 catch (Exception e) 57 { 58 exception = e; 59 } 60 61 after(invocation, result, exception); 62 63 if (exception == null) 64 { 65 return result; 66 } 67 else 68 { 69 throw exception; 70 } 71 72 } 73 74 }
|
I
don't think this is good to do as a general rule. Most times you want to stop
processing when an exception occurs. You could implement this behavior with a
couple of carefully placed interceptors.
When extending the
BeforeAfterInterceptor, if the invocation throws an exception, the after
method isn't called.
Should the
BeforeAfterInterceptor catch the exception, run the after() and then
re-throw the exception ?
Any thoughts anyone
?