|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MethodInterceptorFactory.java | - | 77.3% | 55.6% | 71% |
|
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;
|
|
18 |
import java.lang.reflect.InvocationHandler;
|
|
19 |
import java.lang.reflect.InvocationTargetException;
|
|
20 |
import java.lang.reflect.Method;
|
|
21 |
import java.lang.reflect.Proxy;
|
|
22 |
import java.util.List;
|
|
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 |
*
|
|
43 |
* @see org.apache.hivemind.ServiceInterceptorFactory#createInterceptor(org.apache.hivemind.InterceptorStack, org.apache.hivemind.internal.Module, java.util.List)
|
|
44 |
*/
|
|
45 | 3 |
public void createInterceptor(InterceptorStack stack, Module invokingModule, List parameters) |
46 |
{ |
|
47 | 3 |
final Class[] interfaces = new Class[]{stack.getServiceInterface()};
|
48 | 3 |
final ClassLoader classLoader = invokingModule.getClassResolver().getClassLoader(); |
49 | 3 |
final Object parameter = parameters.get( 0 ); |
50 | 3 |
Defense.isAssignable( parameter, MethodInterceptor.class, "Implementation Object" ); |
51 | 2 |
MethodInterceptor methodInterceptor = ( MethodInterceptor )parameter; |
52 | 2 |
final InvocationHandler invocationHandler = new MethodInterceptorInvocationHandler( methodInterceptor, stack );
|
53 | 2 |
stack.push( Proxy.newProxyInstance( classLoader, interfaces, invocationHandler ) ); |
54 |
} |
|
55 |
|
|
56 |
/**
|
|
57 |
* A java proxy InvocationHandler implementation which allows a MethodInterceptor to intercept the method invocation.
|
|
58 |
*/
|
|
59 |
private final class MethodInterceptorInvocationHandler implements InvocationHandler |
|
60 |
{ |
|
61 |
private final MethodInterceptor methodInterceptor;
|
|
62 |
private final InterceptorStack stack;
|
|
63 |
private final Object target;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Constructs a MethodInterceptorInvocationHandler
|
|
67 |
*
|
|
68 |
* @param stack the interceptor stack
|
|
69 |
*/
|
|
70 | 2 |
public MethodInterceptorInvocationHandler( MethodInterceptor methodInterceptor, InterceptorStack stack )
|
71 |
{ |
|
72 | 2 |
this.stack = stack;
|
73 | 2 |
this.target = stack.peek();
|
74 | 2 |
this.methodInterceptor = methodInterceptor;
|
75 |
} |
|
76 |
|
|
77 |
/**
|
|
78 |
* Calls the MethodInterceptor's invoke method.
|
|
79 |
* @param proxy a reference to the proxy instance
|
|
80 |
* @param method the method being invoked
|
|
81 |
* @param args the arguments to the method
|
|
82 |
* @return the value returned by the MethodInterceptor
|
|
83 |
* @throws Throwable
|
|
84 |
*/
|
|
85 | 2 |
public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable |
86 |
{ |
|
87 | 2 |
return methodInterceptor.invoke( new MethodInvocationImpl( target, method, args, stack.peek() ) ); |
88 |
} |
|
89 |
} |
|
90 |
|
|
91 |
/**
|
|
92 |
* A java reflection-based implementation of a MethodInvocation
|
|
93 |
*/
|
|
94 |
private final class MethodInvocationImpl implements MethodInvocation |
|
95 |
{ |
|
96 |
private final Object next;
|
|
97 |
private final Method method;
|
|
98 |
private final Object[] arguments;
|
|
99 |
private final Object proxy;
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Constructs a MethodInvocationImpl object.
|
|
103 |
*
|
|
104 |
* @param next the next object
|
|
105 |
* @param method the method
|
|
106 |
* @param arguments the arguments
|
|
107 |
* @param proxy the outermost proxy object (allows calling another method instead).
|
|
108 |
*/
|
|
109 | 2 |
public MethodInvocationImpl( Object next, Method method, Object[] arguments, Object proxy )
|
110 |
{ |
|
111 | 2 |
this.next = next;
|
112 | 2 |
this.method = method;
|
113 | 2 |
this.arguments = arguments;
|
114 | 2 |
this.proxy = proxy;
|
115 |
} |
|
116 |
|
|
117 |
/**
|
|
118 |
* Invokes the method on the next object.
|
|
119 |
*
|
|
120 |
* @return value returned by invoking the method on the next object
|
|
121 |
* @throws Throwable throwable thrown by invoking method on the next object
|
|
122 |
*/
|
|
123 | 2 |
public final Object proceed() throws Throwable |
124 |
{ |
|
125 | 2 |
try
|
126 |
{ |
|
127 | 2 |
return method.invoke( next, arguments );
|
128 |
} |
|
129 |
catch( InvocationTargetException e )
|
|
130 |
{ |
|
131 | 0 |
throw e.getTargetException();
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 | 0 |
public final Method getMethod()
|
136 |
{ |
|
137 | 0 |
return method;
|
138 |
} |
|
139 |
|
|
140 | 0 |
public final AccessibleObject getStaticPart()
|
141 |
{ |
|
142 | 0 |
return method;
|
143 |
} |
|
144 |
|
|
145 | 0 |
public final Object getThis()
|
146 |
{ |
|
147 | 0 |
return proxy;
|
148 |
} |
|
149 |
|
|
150 | 0 |
public final Object[] getArguments()
|
151 |
{ |
|
152 | 0 |
return arguments;
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
|
|