01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.netui.pageflow.interceptor.action.internal;
20:
21: import org.apache.beehive.netui.pageflow.interceptor.InterceptorChain;
22: import org.apache.beehive.netui.pageflow.interceptor.InterceptorContext;
23: import org.apache.beehive.netui.pageflow.interceptor.Interceptor;
24: import org.apache.beehive.netui.pageflow.interceptor.InterceptorException;
25: import org.apache.beehive.netui.pageflow.interceptor.action.ActionInterceptor;
26: import org.apache.beehive.netui.pageflow.interceptor.action.ActionInterceptorContext;
27: import org.apache.struts.action.ActionForward;
28:
29: import javax.servlet.ServletException;
30: import java.util.List;
31: import java.io.IOException;
32:
33: public class ActionInterceptors {
34: private static final class WrapActionInterceptorChain extends
35: InterceptorChain {
36: private ActionExecutor _actionExecutor;
37:
38: public WrapActionInterceptorChain(InterceptorContext context,
39: List/*< Interceptor >*/interceptors,
40: ActionExecutor actionExecutor) {
41: super (context, interceptors);
42: _actionExecutor = actionExecutor;
43: }
44:
45: protected Object invoke(Interceptor interceptor)
46: throws InterceptorException {
47: return ((ActionInterceptor) interceptor).wrapAction(
48: (ActionInterceptorContext) getContext(), this );
49: }
50:
51: public Object continueChain() throws InterceptorException {
52: if (!isEmpty()) {
53: return invoke(removeFirst());
54: } else {
55: try {
56: return _actionExecutor.execute();
57: } catch (ServletException e) {
58: throw new InterceptorException(e);
59: } catch (IOException e) {
60: throw new InterceptorException(e);
61: }
62: }
63: }
64: }
65:
66: public static ActionForward wrapAction(
67: ActionInterceptorContext context,
68: List/*< Interceptor >*/interceptors,
69: ActionExecutor actionExecutor) throws InterceptorException,
70: IOException, ServletException {
71: try {
72: if (interceptors != null) {
73: WrapActionInterceptorChain chain = new WrapActionInterceptorChain(
74: context, interceptors, actionExecutor);
75: return (ActionForward) chain.continueChain();
76: } else {
77: return actionExecutor.execute();
78: }
79: } catch (InterceptorException e) {
80: Throwable cause = e.getCause();
81:
82: if (cause instanceof ServletException) {
83: throw (ServletException) cause;
84: } else if (cause instanceof IOException) {
85: throw (IOException) cause;
86: }
87:
88: throw e;
89: }
90: }
91:
92: public interface ActionExecutor {
93: public ActionForward execute() throws IOException,
94: ServletException, InterceptorException;
95: }
96: }
|