1 18 package org.apache.beehive.netui.pageflow.interceptor.action.internal; 19 20 import org.apache.beehive.netui.pageflow.interceptor.InterceptorChain; 21 import org.apache.beehive.netui.pageflow.interceptor.InterceptorContext; 22 import org.apache.beehive.netui.pageflow.interceptor.Interceptor; 23 import org.apache.beehive.netui.pageflow.interceptor.InterceptorException; 24 import org.apache.beehive.netui.pageflow.interceptor.action.ActionInterceptor; 25 import org.apache.beehive.netui.pageflow.interceptor.action.ActionInterceptorContext; 26 import org.apache.struts.action.ActionForward; 27 28 import javax.servlet.ServletException ; 29 import java.util.List ; 30 import java.io.IOException ; 31 32 public class ActionInterceptors 33 { 34 private static final class WrapActionInterceptorChain 35 extends InterceptorChain 36 { 37 private ActionExecutor _actionExecutor; 38 39 public WrapActionInterceptorChain( InterceptorContext context, List interceptors, 40 ActionExecutor actionExecutor ) 41 { 42 super( context, interceptors ); 43 _actionExecutor = actionExecutor; 44 } 45 46 protected Object invoke( Interceptor interceptor ) 47 throws InterceptorException 48 { 49 return ( ( ActionInterceptor ) interceptor ).wrapAction( ( ActionInterceptorContext ) getContext(), this ); 50 } 51 52 public Object continueChain() 53 throws InterceptorException 54 { 55 if ( ! isEmpty() ) 56 { 57 return invoke( removeFirst() ); 58 } 59 else 60 { 61 try 62 { 63 return _actionExecutor.execute(); 64 } 65 catch ( ServletException e ) 66 { 67 throw new InterceptorException( e ); 68 } 69 catch ( IOException e ) 70 { 71 throw new InterceptorException( e ); 72 } 73 } 74 } 75 } 76 77 public static ActionForward wrapAction( ActionInterceptorContext context, List interceptors, 78 ActionExecutor actionExecutor ) 79 throws InterceptorException, IOException , ServletException 80 { 81 try 82 { 83 if ( interceptors != null ) 84 { 85 WrapActionInterceptorChain chain = new WrapActionInterceptorChain( context, interceptors, actionExecutor ); 86 return ( ActionForward ) chain.continueChain(); 87 } 88 else 89 { 90 return actionExecutor.execute(); 91 } 92 } 93 catch ( InterceptorException e ) 94 { 95 Throwable cause = e.getCause(); 96 97 if ( cause instanceof ServletException ) 98 { 99 throw ( ServletException ) cause; 100 } 101 else if ( cause instanceof IOException ) 102 { 103 throw ( IOException ) cause; 104 } 105 106 throw e; 107 } 108 } 109 110 public interface ActionExecutor 111 { 112 public ActionForward execute() throws IOException , ServletException , InterceptorException; 113 } 114 } 115 | Popular Tags |