1 22 package org.jboss.ejb3.stateful; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Proxy ; 26 import org.jboss.aop.Dispatcher; 27 import org.jboss.aop.advice.Interceptor; 28 import org.jboss.aop.util.MethodHashing; 29 import org.jboss.aop.util.PayloadKey; 30 import org.jboss.aspects.asynch.AsynchMixin; 31 import org.jboss.aspects.asynch.AsynchProvider; 32 import org.jboss.aspects.remoting.ClusterConstants; 33 import org.jboss.aspects.remoting.FamilyWrapper; 34 import org.jboss.aspects.remoting.InvokeRemoteInterceptor; 35 import org.jboss.ejb3.JBossProxy; 36 import org.jboss.ejb3.ProxyUtils; 37 import org.jboss.ejb3.asynchronous.AsynchronousInterceptor; 38 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 39 import org.jboss.util.id.GUID; 40 41 47 public class StatefulClusteredProxy extends org.jboss.ejb3.remoting.BaseRemoteProxy 48 { 49 private Object id; 50 protected FamilyWrapper family; 51 protected LoadBalancePolicy lbPolicy; 52 AsynchProvider provider; 53 54 55 public StatefulClusteredProxy(Object containerId, Interceptor[] interceptors, FamilyWrapper family, LoadBalancePolicy lb) 56 { 57 super(containerId, interceptors); 58 this.family = family; 59 this.lbPolicy = lb; 60 } 61 62 public StatefulClusteredProxy(AsynchProvider provider, Object containerId, Interceptor[] interceptors, FamilyWrapper family, LoadBalancePolicy lb) 63 { 64 super(containerId, interceptors); 65 this.family = family; 66 this.lbPolicy = lb; 67 this.provider = provider; 68 } 69 70 protected StatefulClusteredProxy() 71 { 72 } 73 74 public Object invoke(Object proxy, Method method, Object [] args) 75 throws Throwable 76 { 77 if (method.getDeclaringClass() == AsynchProvider.class) 78 { 79 return provider.getFuture(); 80 } 81 82 long hash = MethodHashing.calculateHash(method); 83 Object ret = ProxyUtils.handleCallLocally(hash, (JBossProxy) proxy, this, method, args); 84 if (ret != null) 85 { 86 return ret; 87 } 88 89 StatefulRemoteInvocation sri = new StatefulRemoteInvocation(interceptors, hash, method, method, null, id); 90 sri.setArguments(args); 91 sri.setInstanceResolver(metadata); 92 sri.getMetaData().addMetaData(Dispatcher.DISPATCHER, Dispatcher.OID, containerId, PayloadKey.AS_IS); 93 sri.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.CLUSTER_FAMILY_WRAPPER, family, PayloadKey.AS_IS); 94 sri.getMetaData().addMetaData(ClusterConstants.CLUSTERED_REMOTING, ClusterConstants.LOADBALANCE_POLICY, lbPolicy, PayloadKey.AS_IS); 95 sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.SUBSYSTEM, "AOP", PayloadKey.AS_IS); 96 97 if (provider != null) 98 { 99 sri.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.INVOKE_ASYNCH, "YES", PayloadKey.AS_IS); 100 } 101 try 102 { 103 Object rtn = sri.invokeNext(); 104 if (id == null) 106 { 107 id = sri.getResponseAttachment(StatefulConstants.NEW_ID); 108 } 109 return rtn; 110 } 111 catch (ForwardId forward) 112 { 113 id = forward.getId(); 116 throw forward.getCause(); 117 } 118 } 119 120 public Object getAsynchronousProxy(Object proxy) 121 { 122 Class [] infs = proxy.getClass().getInterfaces(); 123 if (!ProxyUtils.isAsynchronous(infs)) 124 { 125 Class [] interfaces = ProxyUtils.addAsynchProviderInterface(infs); 126 AsynchMixin mixin = new AsynchMixin(); 127 Interceptor[] newInterceptors = ProxyUtils.addAsynchProxyInterceptor(mixin, interceptors); 128 StatefulClusteredProxy handler = new StatefulClusteredProxy(mixin, containerId, newInterceptors, family, lbPolicy); 129 return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, handler); 130 } 131 132 return proxy; 134 } 135 136 public String toString() 137 { 138 if (id != null) 139 { 140 return containerId.toString() + ":" + id.toString(); 141 } 142 else 143 { 144 GUID guid = new GUID(); 146 return containerId.toString() + ":" + guid.toString(); 147 } 148 } 149 150 } 151 | Popular Tags |