KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > interceptor > action > internal > ActionInterceptors


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

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 JavaDoc;
29 import java.util.List JavaDoc;
30 import java.io.IOException JavaDoc;
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 JavaDoc/*< Interceptor >*/ interceptors,
40                                            ActionExecutor actionExecutor )
41         {
42             super( context, interceptors );
43             _actionExecutor = actionExecutor;
44         }
45         
46         protected Object JavaDoc invoke( Interceptor interceptor )
47                 throws InterceptorException
48         {
49             return ( ( ActionInterceptor ) interceptor ).wrapAction( ( ActionInterceptorContext ) getContext(), this );
50         }
51
52         public Object JavaDoc 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 JavaDoc e )
66                 {
67                     throw new InterceptorException( e );
68                 }
69                 catch ( IOException JavaDoc e )
70                 {
71                     throw new InterceptorException( e );
72                 }
73             }
74         }
75     }
76     
77     public static ActionForward wrapAction( ActionInterceptorContext context, List JavaDoc/*< Interceptor >*/ interceptors,
78                                             ActionExecutor actionExecutor )
79         throws InterceptorException, IOException JavaDoc, ServletException JavaDoc
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 JavaDoc cause = e.getCause();
96             
97             if ( cause instanceof ServletException JavaDoc )
98             {
99                 throw ( ServletException JavaDoc ) cause;
100             }
101             else if ( cause instanceof IOException JavaDoc )
102             {
103                 throw ( IOException JavaDoc ) cause;
104             }
105             
106             throw e;
107         }
108     }
109     
110     public interface ActionExecutor
111     {
112         public ActionForward execute() throws IOException JavaDoc, ServletException JavaDoc, InterceptorException;
113     }
114 }
115
Popular Tags