1 22 package org.jboss.ejb3; 23 24 import java.lang.reflect.Method ; 25 import java.util.ArrayList ; 26 import javax.ejb.EJBHome ; 27 import javax.ejb.EJBObject ; 28 import org.jboss.aop.advice.Interceptor; 29 import org.jboss.aop.joinpoint.MethodInvocation; 30 import org.jboss.aop.util.MethodHashing; 31 import org.jboss.aop.util.PayloadKey; 32 import org.jboss.aspects.asynch.AsynchMixin; 33 import org.jboss.aspects.asynch.AsynchProvider; 34 import org.jboss.aspects.asynch.AsynchProxyInterceptor; 35 import org.jboss.aspects.asynch.FutureHolder; 36 import org.jboss.ejb3.asynchronous.AsynchronousInterceptor; 37 import org.jboss.ejb3.remoting.Proxy; 38 39 43 public class ProxyUtils 44 { 45 public final static Class ASYNCH_PROVIDER_CLASS = AsynchProvider.class; 46 public final static long GET_ASYNCHRONOUS; 47 public static final long TO_STRING; 48 public static final long EQUALS; 49 public static final long HASHCODE; 50 public static final Method GET_PRIMARY_KEY; 51 public static final Method GET_HANDLE; 52 public static final Method GET_EJB_HOME; 53 public static final Method IS_IDENTICAL; 54 public static final Method GET_HOME_HANDLE; 55 public static final Method GET_EJB_METADATA; 56 public static final Method REMOVE; 57 58 static 59 { 60 try 61 { 62 Class [] empty = {}; 63 64 Method method = JBossProxy.class.getMethod("getAsynchronousProxy", empty); 65 GET_ASYNCHRONOUS = MethodHashing.calculateHash(method); 66 TO_STRING = MethodHashing.calculateHash(Object .class.getDeclaredMethod("toString", empty)); 67 EQUALS = MethodHashing.calculateHash(Object .class.getDeclaredMethod("equals", new Class []{Object .class})); 68 HASHCODE = MethodHashing.calculateHash(Object .class.getDeclaredMethod("hashCode", empty)); 69 70 GET_PRIMARY_KEY = EJBObject .class.getMethod("getPrimaryKey", empty); 71 GET_HANDLE = EJBObject .class.getMethod("getHandle", empty); 72 GET_EJB_HOME = EJBObject .class.getMethod("getEJBHome", empty); 73 IS_IDENTICAL = EJBObject .class.getMethod("isIdentical", new Class [] { EJBObject .class }); 74 REMOVE = EJBObject .class.getMethod("remove", empty); 75 76 GET_HOME_HANDLE = EJBHome .class.getMethod("getHomeHandle", empty); 77 GET_EJB_METADATA = EJBHome .class.getMethod("getEJBMetaData", empty); 78 } 79 catch (NoSuchMethodException e) 80 { 81 throw new RuntimeException (e); 82 } 83 } 84 85 public static boolean isAsynchronous(Class [] infs) 86 { 87 for (int i = 0; i < infs.length; i++) 88 { 89 if (infs[i] == ASYNCH_PROVIDER_CLASS) 90 { 91 return true; 92 } 93 } 94 return false; 95 } 96 97 public static Class [] addAsynchProviderInterface(Class [] infs) 98 { 99 ArrayList interfaces = new ArrayList (); 100 101 for (int i = 0; i < infs.length; i++) 102 { 103 if (infs[i] == ASYNCH_PROVIDER_CLASS) 104 { 105 continue; 107 } 108 interfaces.add(infs[i]); 109 } 110 111 interfaces.add(ASYNCH_PROVIDER_CLASS); 112 return (Class []) interfaces.toArray(new Class [interfaces.size()]); 113 } 114 115 public static Interceptor[] addAsynchProxyInterceptor(AsynchMixin mixin, Interceptor[] interceptors) 116 { 117 AsynchProxyInterceptor interceptor = new AsynchProxyInterceptor(mixin); 118 Interceptor[] newInterceptors = null; 119 newInterceptors = new Interceptor[interceptors.length + 1]; 120 newInterceptors[0] = interceptor; 121 System.arraycopy(interceptors, 0, newInterceptors, 1, interceptors.length); 122 return newInterceptors; 123 } 124 125 public static void addLocalAsynchronousInfo(MethodInvocation invocation, FutureHolder provider) 126 { 127 if (provider != null) 128 { 129 invocation.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.INVOKE_ASYNCH, "YES", PayloadKey.AS_IS); 130 invocation.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.FUTURE_HOLDER, provider, PayloadKey.AS_IS); 131 } 132 } 133 134 public static Object handleCallLocally(Object jbproxy, Proxy ih, Method m, Object [] args) 135 { 136 long hash = MethodHashing.calculateHash(m); 137 return handleCallLocally(hash, jbproxy, ih, m, args); 138 } 139 140 public static Object handleCallLocally(long hash, Object jbproxy, Proxy ih, Method m, Object [] args) 141 { 142 if (hash == ProxyUtils.GET_ASYNCHRONOUS) 143 { 144 return ih.getAsynchronousProxy((JBossProxy)jbproxy); 145 } 146 else if (hash == TO_STRING) 147 { 148 return ih.toString(); 149 } 150 else if (hash == HASHCODE) 151 { 152 return new Integer (ih.toString().hashCode()); 153 } 154 else if (hash == EQUALS) 155 { 156 return new Boolean (ih.toString().equals(args[0].toString())); 157 } 158 return null; 159 } 160 } 161 | Popular Tags |