001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.interceptor;
020:
021: import java.util.List;
022:
023: /**
024: * <p>
025: * Convenience utility used to execute an interceptor chain given an {@link InterceptorContext}. This
026: * class can be invoked to execute "pre" and "post" interceptor chains.
027: * </p>
028: */
029: public class Interceptors {
030: /**
031: * Execute a "pre" interceptor chain. This will execute the
032: * {@link Interceptor#preInvoke(InterceptorContext, InterceptorChain)}
033: * method to be invoked on each interceptor in a chain.
034: *
035: * @param context the context for a set of interceptors
036: * @param interceptors the list of interceptors
037: * @throws InterceptorException
038: */
039: public static void doPreIntercept(InterceptorContext context,
040: List/*< Interceptor >*/interceptors)
041: throws InterceptorException {
042: if (interceptors != null) {
043: PreInvokeInterceptorChain chain = new PreInvokeInterceptorChain(
044: context, interceptors);
045: chain.continueChain();
046: }
047: }
048:
049: /**
050: * Execute a "post" interceptor chain. This will execute the
051: * {@link Interceptor#postInvoke(InterceptorContext, InterceptorChain)}
052: * method to be invoked on each interceptor in a chain.
053: * @param context the context for a set of interceptors
054: * @param interceptors the list of interceptors
055: * @throws InterceptorException
056: */
057: public static void doPostIntercept(InterceptorContext context,
058: List/*< Interceptor >*/interceptors)
059: throws InterceptorException {
060: if (interceptors != null) {
061: PostInvokeInterceptorChain chain = new PostInvokeInterceptorChain(
062: context, interceptors);
063: chain.continueChain();
064: }
065: }
066:
067: /**
068: * Utility class used to configure an {@link InterceptorChain} and invoke an interceptor's
069: * {@link Interceptor#preInvoke(InterceptorContext, InterceptorChain)} method.
070: */
071: private static final class PreInvokeInterceptorChain extends
072: InterceptorChain {
073: public PreInvokeInterceptorChain(InterceptorContext context,
074: List/*< Interceptor >*/interceptors) {
075: super (context, interceptors);
076: }
077:
078: protected Object invoke(Interceptor interceptor)
079: throws InterceptorException {
080: interceptor.preInvoke(getContext(), this );
081: return null;
082: }
083: }
084:
085: /**
086: * Utility class used to configure an {@link InterceptorChain} and invoke an interceptor's
087: * {@link Interceptor#postInvoke(InterceptorContext, InterceptorChain)} method.
088: */
089: private static final class PostInvokeInterceptorChain extends
090: InterceptorChain {
091: public PostInvokeInterceptorChain(InterceptorContext context,
092: List/*< Interceptor >*/interceptors) {
093: super (context, interceptors);
094: }
095:
096: protected Object invoke(Interceptor interceptor)
097: throws InterceptorException {
098: interceptor.postInvoke(getContext(), this);
099: return null;
100: }
101: }
102: }
|