1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class MethodInterceptorFactory extends BaseLocatable implements ServiceInterceptorFactory |
39 |
| { |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
5
| public void createInterceptor(InterceptorStack stack, Module invokingModule, List parameters)
|
46 |
| { |
47 |
5
| final Class[] interfaces = new Class[]{stack.getServiceInterface()};
|
48 |
5
| final ClassLoader classLoader = invokingModule.getClassResolver().getClassLoader();
|
49 |
5
| final Object parameter = parameters.get( 0 );
|
50 |
5
| Defense.isAssignable( parameter, MethodInterceptor.class, "Implementation Object" );
|
51 |
4
| MethodInterceptor methodInterceptor = ( MethodInterceptor )parameter;
|
52 |
4
| final InvocationHandler invocationHandler = new MethodInterceptorInvocationHandler( methodInterceptor, stack );
|
53 |
4
| stack.push( Proxy.newProxyInstance( classLoader, interfaces, invocationHandler ) );
|
54 |
| } |
55 |
| |
56 |
| |
57 |
| |
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 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
4
| public MethodInterceptorInvocationHandler( MethodInterceptor methodInterceptor, InterceptorStack stack )
|
71 |
| { |
72 |
4
| this.stack = stack;
|
73 |
4
| this.target = stack.peek();
|
74 |
4
| this.methodInterceptor = methodInterceptor;
|
75 |
| } |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
4
| public Object invoke( Object proxy, Method method, Object[] args ) throws Throwable
|
86 |
| { |
87 |
4
| return methodInterceptor.invoke( new MethodInvocationImpl( target, method, args, stack.peek() ) );
|
88 |
| } |
89 |
| } |
90 |
| |
91 |
| |
92 |
| |
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 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
4
| public MethodInvocationImpl( Object next, Method method, Object[] arguments, Object proxy )
|
110 |
| { |
111 |
4
| this.next = next;
|
112 |
4
| this.method = method;
|
113 |
4
| this.arguments = arguments;
|
114 |
4
| this.proxy = proxy;
|
115 |
| } |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
4
| public final Object proceed() throws Throwable
|
124 |
| { |
125 |
4
| try
|
126 |
| { |
127 |
4
| 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 |
| } |