KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > component > proxy > AbstractInterceptor


1 package org.sape.carbon.core.component.proxy;
2
3 import java.lang.reflect.InvocationHandler JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Proxy JavaDoc;
6
7 /**
8  * This is the abstract interceptor that can provide basic tracking
9  * and next interceptor calling for subclassed interceptors. This
10  * implementation will also shortcut calls to DynamicProxy classes
11  * acting as interceptors by skipping their proxy classes and directly
12  * calling the
13  *
14  * Copyright 2003 Sapient
15  * @since carbon 2.1
16  * @author Greg Hinkle, October 2003
17  * @version $Revision: 1.1 $($Author: ghinkl $ / $Date: 2003/10/15 01:03:26 $)
18  */

19 public abstract class AbstractInterceptor implements Interceptor {
20
21     /**
22      * The next interceptor in the chain after this one.
23      */

24     protected Interceptor nextInterceptor;
25
26     /**
27      * True if the next interceptor is a dynamic proxy.
28      */

29     protected boolean nextInterceptorIsProxy;
30
31     /**
32      * If the next interceptor is a dynamic proxy, this will
33      * hold a reference to its invocation handler.
34      */

35     protected InvocationHandler JavaDoc nextInvocationHandler;
36
37     /**
38      * The interceptor method object for the "invoke" method.
39      */

40     private Method JavaDoc interceptorMethod;
41
42
43
44     /**
45      * Prepares the interceptor chain by setting the next interceptor
46      * that this one will forward to. It will also prepare for calls
47      * to DynamicProxies.
48      *
49      * @param interceptor the next interceptor in the chain
50      */

51     public void setNextInterceptor(Interceptor interceptor) {
52         this.nextInterceptor = interceptor;
53         this.nextInterceptorIsProxy = Proxy.isProxyClass(interceptor.getClass());
54
55         // If the next interceptor is a Proxy itself,
56
if (this.nextInterceptorIsProxy) {
57             this.nextInvocationHandler = Proxy.getInvocationHandler(interceptor);
58             try {
59                 this.interceptorMethod =
60                    Interceptor.class.getMethod("invoke", new Class JavaDoc[] { Invocation.class });
61             } catch (NoSuchMethodException JavaDoc nsme) {
62                 // This should never happen
63
}
64         }
65     }
66
67     /**
68      * Will forward on an interceptor call to the next interceptor
69      * in the chain.
70      * @param invocation the invocation in progress
71      * @return the return value of the component invocation
72      * @throws Throwable the exception caused by the invocation
73      */

74     protected Object JavaDoc callNextInterceptor(Invocation invocation) throws Throwable JavaDoc {
75         if (this.nextInterceptorIsProxy) {
76             return this.nextInvocationHandler.invoke(
77                 this.nextInterceptor,
78                 this.interceptorMethod,
79                 new Object JavaDoc[] { invocation });
80         } else {
81             return this.nextInterceptor.invoke(invocation);
82
83         }
84     }
85
86 }
87
Popular Tags