KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > impl > MethodInterceptorFactory


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

15 package org.apache.hivemind.lib.impl;
16
17 import java.lang.reflect.AccessibleObject JavaDoc;
18 import java.lang.reflect.InvocationHandler JavaDoc;
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.aopalliance.intercept.MethodInterceptor;
25 import org.aopalliance.intercept.MethodInvocation;
26 import org.apache.hivemind.InterceptorStack;
27 import org.apache.hivemind.ServiceInterceptorFactory;
28 import org.apache.hivemind.impl.BaseLocatable;
29 import org.apache.hivemind.internal.Module;
30 import org.apache.hivemind.util.Defense;
31
32 /**
33  * A service interceptor factory supporting the AOP Alliance MethodInterceptor interface.
34  * <b>Note:</b>The current implementation uses JDK proxies as opposed to Javassist!
35  * @author James Carman
36  * @since 1.1
37  */

38 public class MethodInterceptorFactory extends BaseLocatable implements ServiceInterceptorFactory
39 {
40
41     /**
42      * @see org.apache.hivemind.ServiceInterceptorFactory#createInterceptor(org.apache.hivemind.InterceptorStack, org.apache.hivemind.internal.Module, java.util.List)
43      */

44     public void createInterceptor(InterceptorStack stack, Module invokingModule, Object JavaDoc parameters)
45     {
46         final Object JavaDoc parameter = ((List JavaDoc) parameters).get( 0 );
47         Defense.isAssignable( parameter, MethodInterceptor.class, "Implementation Object" );
48         MethodInterceptor methodInterceptor = ( MethodInterceptor )parameter;
49         createInterceptor(stack, invokingModule, methodInterceptor);
50     }
51  
52     /**
53      * @see org.apache.hivemind.ServiceInterceptorFactory#createInterceptor(org.apache.hivemind.InterceptorStack, org.apache.hivemind.internal.Module, java.util.List)
54      */

55     public void createInterceptor(InterceptorStack stack, Module invokingModule, MethodInterceptor methodInterceptor)
56     {
57         final Class JavaDoc[] interfaces = new Class JavaDoc[]{stack.getServiceInterface()};
58         final ClassLoader JavaDoc classLoader = invokingModule.getClassResolver().getClassLoader();
59         final InvocationHandler JavaDoc invocationHandler = new MethodInterceptorInvocationHandler( methodInterceptor, stack );
60         stack.push( Proxy.newProxyInstance( classLoader, interfaces, invocationHandler ) );
61     }
62     
63     /**
64      * A java proxy InvocationHandler implementation which allows a MethodInterceptor to intercept the method invocation.
65      */

66     private final class MethodInterceptorInvocationHandler implements InvocationHandler JavaDoc
67     {
68         private final MethodInterceptor methodInterceptor;
69         private final InterceptorStack stack;
70         private final Object JavaDoc target;
71
72         /**
73          * Constructs a MethodInterceptorInvocationHandler
74          *
75          * @param stack the interceptor stack
76          */

77         public MethodInterceptorInvocationHandler( MethodInterceptor methodInterceptor, InterceptorStack stack )
78         {
79             this.stack = stack;
80             this.target = stack.peek();
81             this.methodInterceptor = methodInterceptor;
82         }
83
84         /**
85          * Calls the MethodInterceptor's invoke method.
86          * @param proxy a reference to the proxy instance
87          * @param method the method being invoked
88          * @param args the arguments to the method
89          * @return the value returned by the MethodInterceptor
90          * @throws Throwable
91          */

92         public Object JavaDoc invoke( Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args ) throws Throwable JavaDoc
93         {
94             return methodInterceptor.invoke( new MethodInvocationImpl( target, method, args, stack.peek() ) );
95         }
96     }
97
98     /**
99      * A java reflection-based implementation of a MethodInvocation
100      */

101     private final class MethodInvocationImpl implements MethodInvocation
102     {
103         private final Object JavaDoc next;
104         private final Method JavaDoc method;
105         private final Object JavaDoc[] arguments;
106         private final Object JavaDoc proxy;
107
108         /**
109          * Constructs a MethodInvocationImpl object.
110          *
111          * @param next the next object
112          * @param method the method
113          * @param arguments the arguments
114          * @param proxy the outermost proxy object (allows calling another method instead).
115          */

116         public MethodInvocationImpl( Object JavaDoc next, Method JavaDoc method, Object JavaDoc[] arguments, Object JavaDoc proxy )
117         {
118             this.next = next;
119             this.method = method;
120             this.arguments = arguments;
121             this.proxy = proxy;
122         }
123
124         /**
125          * Invokes the method on the next object.
126          *
127          * @return value returned by invoking the method on the next object
128          * @throws Throwable throwable thrown by invoking method on the next object
129          */

130         public final Object JavaDoc proceed() throws Throwable JavaDoc
131         {
132             try
133             {
134                 return method.invoke( next, arguments );
135             }
136             catch( InvocationTargetException JavaDoc e )
137             {
138                 throw e.getTargetException();
139             }
140         }
141
142         public final Method JavaDoc getMethod()
143         {
144             return method;
145         }
146
147         public final AccessibleObject JavaDoc getStaticPart()
148         {
149             return method;
150         }
151
152         public final Object JavaDoc getThis()
153         {
154             return proxy;
155         }
156
157         public final Object JavaDoc[] getArguments()
158         {
159             return arguments;
160         }
161     }
162 }
163
Popular Tags