1 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 38 public class MethodInterceptorFactory extends BaseLocatable implements ServiceInterceptorFactory 39 { 40 41 44 public void createInterceptor(InterceptorStack stack, Module invokingModule, Object parameters) 45 { 46 final Object parameter = ((List ) parameters).get( 0 ); 47 Defense.isAssignable( parameter, MethodInterceptor.class, "Implementation Object" ); 48 MethodInterceptor methodInterceptor = ( MethodInterceptor )parameter; 49 createInterceptor(stack, invokingModule, methodInterceptor); 50 } 51 52 55 public void createInterceptor(InterceptorStack stack, Module invokingModule, MethodInterceptor methodInterceptor) 56 { 57 final Class [] interfaces = new Class []{stack.getServiceInterface()}; 58 final ClassLoader classLoader = invokingModule.getClassResolver().getClassLoader(); 59 final InvocationHandler invocationHandler = new MethodInterceptorInvocationHandler( methodInterceptor, stack ); 60 stack.push( Proxy.newProxyInstance( classLoader, interfaces, invocationHandler ) ); 61 } 62 63 66 private final class MethodInterceptorInvocationHandler implements InvocationHandler 67 { 68 private final MethodInterceptor methodInterceptor; 69 private final InterceptorStack stack; 70 private final Object target; 71 72 77 public MethodInterceptorInvocationHandler( MethodInterceptor methodInterceptor, InterceptorStack stack ) 78 { 79 this.stack = stack; 80 this.target = stack.peek(); 81 this.methodInterceptor = methodInterceptor; 82 } 83 84 92 public Object invoke( Object proxy, Method method, Object [] args ) throws Throwable 93 { 94 return methodInterceptor.invoke( new MethodInvocationImpl( target, method, args, stack.peek() ) ); 95 } 96 } 97 98 101 private final class MethodInvocationImpl implements MethodInvocation 102 { 103 private final Object next; 104 private final Method method; 105 private final Object [] arguments; 106 private final Object proxy; 107 108 116 public MethodInvocationImpl( Object next, Method method, Object [] arguments, Object proxy ) 117 { 118 this.next = next; 119 this.method = method; 120 this.arguments = arguments; 121 this.proxy = proxy; 122 } 123 124 130 public final Object proceed() throws Throwable 131 { 132 try 133 { 134 return method.invoke( next, arguments ); 135 } 136 catch( InvocationTargetException e ) 137 { 138 throw e.getTargetException(); 139 } 140 } 141 142 public final Method getMethod() 143 { 144 return method; 145 } 146 147 public final AccessibleObject getStaticPart() 148 { 149 return method; 150 } 151 152 public final Object getThis() 153 { 154 return proxy; 155 } 156 157 public final Object [] getArguments() 158 { 159 return arguments; 160 } 161 } 162 } 163 | Popular Tags |