1 15 package org.apache.hivemind.impl; 16 17 import java.lang.reflect.InvocationHandler ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.lang.reflect.Method ; 20 import java.lang.reflect.Proxy ; 21 22 import org.apache.hivemind.InterceptorStack; 23 import org.apache.hivemind.ServiceInterceptorFactory; 24 import org.apache.hivemind.internal.Module; 25 26 32 public class CountFactory implements ServiceInterceptorFactory 33 { 34 private static int _count = 0; 35 36 public static int getCount() 37 { 38 return _count; 39 } 40 41 public static void reset() 42 { 43 _count = 0; 44 } 45 46 public static void incrementCount() 47 { 48 _count++; 49 } 50 51 private static class CountHandler implements InvocationHandler 52 { 53 private Object _inner; 54 55 CountHandler(Object inner) 56 { 57 _inner = inner; 58 } 59 60 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 61 { 62 try 63 { 64 incrementCount(); 65 66 return method.invoke(_inner, args); 67 } 68 catch (InvocationTargetException ex) 69 { 70 throw ex.getTargetException(); 71 } 72 } 73 74 } 75 76 public void createInterceptor(InterceptorStack stack, Module invokingModule, Object parameters) 77 { 78 InvocationHandler countHandler = new CountHandler(stack.peek()); 79 80 Object proxy = 81 Proxy.newProxyInstance( 82 invokingModule.getClassResolver().getClassLoader(), 83 new Class [] { stack.getServiceInterface()}, 84 countHandler); 85 86 stack.push(proxy); 87 } 88 } 89 | Popular Tags |