1 22 package org.jboss.aop; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.lang.reflect.Method ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import org.jboss.aop.joinpoint.Invocation; 29 import org.jboss.aop.joinpoint.InvocationResponse; 30 import org.jboss.aop.joinpoint.MethodInvocation; 31 import org.jboss.aop.proxy.ClassProxy; 32 import org.jboss.aop.proxy.ClassProxyFactory; 33 import org.jboss.aop.proxy.Proxy; 34 import org.jboss.aop.util.reference.MethodPersistentReference; 35 36 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 37 38 42 43 public class Dispatcher 44 { 45 public static final String DISPATCHER = "DISPATCHER"; 46 public static final String OID = "OID"; 47 public static final Dispatcher singleton = new Dispatcher(); 48 49 Map targetMap = new ConcurrentReaderHashMap(); 50 51 52 public boolean isRegistered(Object oid) 53 { 54 return targetMap.containsKey(oid); 55 } 56 57 60 public void registerTarget(Object oid, Object target) 61 { 62 targetMap.put(oid, target); 63 } 64 65 public void unregisterTarget(Object oid) 66 { 67 targetMap.remove(oid); 68 } 69 70 public Object getRegistered(Object oid) 71 { 72 return targetMap.get(oid); 73 } 74 75 76 public InvocationResponse invoke(Invocation invocation) throws NotFoundInDispatcherException, Throwable 77 { 78 Object oid = invocation.getMetaData(DISPATCHER, OID); 79 80 Object target = null; 81 target = targetMap.get(oid); 82 83 if (target == null) 84 { 85 throw new NotFoundInDispatcherException(oid); 86 } 87 88 if (target instanceof ClassProxy) 89 { 90 ClassProxy proxy = (ClassProxy) target; 91 return proxy._dynamicInvoke(invocation); 92 } 93 else if (target instanceof Proxy) 94 { 95 ClassProxy proxy = (ClassProxy) target; 96 return proxy._dynamicInvoke(invocation); 97 } 98 else if (target instanceof Advised) 99 { 100 Advisor advisor = ((Advised) target)._getAdvisor(); 101 return advisor.dynamicInvoke(target, invocation); 102 } 103 else if (target instanceof Advisor) 104 { 105 Advisor advisor = (Advisor) target; 106 return advisor.dynamicInvoke(null, invocation); 107 } 108 else 109 { 110 if (invocation instanceof MethodInvocation) 111 { 112 MethodInvocation methodInvocation = (MethodInvocation) invocation; 113 long methodHash = methodInvocation.getMethodHash(); 115 HashMap methodMap = ClassProxyFactory.getMethodMap(target.getClass()); 116 MethodPersistentReference ref = (MethodPersistentReference)methodMap.get(new Long (methodHash)); 117 Method method = (Method )ref.get(); 118 Object [] args = methodInvocation.getArguments(); 119 try 120 { 121 return new InvocationResponse(method.invoke(target, args)); 122 } 123 catch (InvocationTargetException ex) 124 { 125 throw ex.getTargetException(); 126 } 127 } 128 else 129 { 130 throw new RuntimeException ("field invocations not implemented"); 131 } 132 } 133 } 134 } 135 | Popular Tags |