1 package org.sapia.soto.aop; 2 3 import net.sf.cglib.proxy.MethodInterceptor; 4 import net.sf.cglib.proxy.MethodProxy; 5 6 import gnu.trove.THashMap; 7 8 import java.lang.reflect.Method ; 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 16 27 public class AdviceInterceptor implements MethodInterceptor { 28 private Map _invokers = new THashMap(); 29 private Class _advised; 30 31 34 public AdviceInterceptor(Class advised) { 35 _advised = advised; 36 } 37 38 47 public void addInvoker(Method method, Invoker inv) { 48 List invokers = (List ) _invokers.get(method); 49 50 if (invokers == null) { 51 invokers = new ArrayList (1); 52 } 53 54 invokers.add(inv); 55 _invokers.put(method, invokers); 56 } 57 58 61 public void init() { 62 ((THashMap) _invokers).compact(); 63 } 64 65 public Class getAdvisedClass() { 66 return _advised; 67 } 68 69 72 public Object intercept(Object instance, Method toCall, Object [] args, 73 MethodProxy proxy) throws Throwable { 74 Object toReturn; 75 76 if (_invokers.size() > 0) { 77 List invokers = (List ) _invokers.get(toCall); 78 79 if ((invokers == null) || (invokers.size() == 0)) { 80 toReturn = proxy.invokeSuper(instance, args); 81 } else { 82 Iterator itr = invokers.iterator(); 83 Invocation invocation = new Invocation(instance, toCall, proxy, args); 84 toReturn = ((Invoker) itr.next()).invoke(invocation, itr); 85 } 86 } else { 87 toReturn = proxy.invokeSuper(instance, args); 88 } 89 90 return toReturn; 91 } 92 } 93 | Popular Tags |