1 22 package org.jboss.ejb3.stateful; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Proxy ; 26 import javax.ejb.EJBMetaData ; 27 import javax.ejb.Handle ; 28 import javax.ejb.HomeHandle ; 29 import org.jboss.aop.Dispatcher; 30 import org.jboss.aop.advice.Interceptor; 31 import org.jboss.aop.util.MethodHashing; 32 import org.jboss.aop.util.PayloadKey; 33 import org.jboss.aspects.asynch.AsynchMixin; 34 import org.jboss.aspects.asynch.AsynchProvider; 35 import org.jboss.aspects.remoting.InvokeRemoteInterceptor; 36 import org.jboss.ejb3.ProxyUtils; 37 import org.jboss.ejb3.asynchronous.AsynchronousInterceptor; 38 import org.jboss.logging.Logger; 39 import org.jboss.remoting.InvokerLocator; 40 import org.jboss.util.id.GUID; 41 42 48 public class StatefulRemoteProxy extends org.jboss.ejb3.session.BaseSessionRemoteProxy 49 { 50 private static final Logger log = Logger.getLogger(StatefulRemoteProxy.class); 51 52 protected InvokerLocator uri; 53 AsynchProvider provider; 54 55 private Handle handle; 56 private HomeHandle homeHandle; 57 private EJBMetaData ejbMetaData; 58 59 public StatefulRemoteProxy(Object containerId, Interceptor[] interceptors, InvokerLocator uri) 60 { 61 super(containerId, interceptors); 62 this.uri = uri; 63 } 64 65 public StatefulRemoteProxy(Object containerId, Interceptor[] interceptors, InvokerLocator uri, Object id) 66 { 67 super(containerId, interceptors); 68 this.uri = uri; 69 this.id = id; 70 } 71 72 public StatefulRemoteProxy(AsynchProvider provider, Object containerId, Interceptor[] interceptors, InvokerLocator uri) 73 { 74 super(containerId, interceptors); 75 this.uri = uri; 76 this.provider = provider; 77 } 78 79 protected StatefulRemoteProxy() 80 { 81 } 82 83 public void setHandle(StatefulHandleImpl handle) 84 { 85 this.handle = handle; 86 handle.id = id; 87 } 88 89 public void setHomeHandle(HomeHandle homeHandle) 90 { 91 this.homeHandle = homeHandle; 92 } 93 94 public void setEjbMetaData(EJBMetaData ejbMetaData) 95 { 96 this.ejbMetaData = ejbMetaData; 97 } 98 99 public Object invoke(Object proxy, Method method, Object [] args) 100 throws Throwable 101 { 102 if (method.getDeclaringClass() == AsynchProvider.class) 103 { 104 return provider.getFuture(); 105 } 106 107 long hash = MethodHashing.calculateHash(method); 108 Object ret = ProxyUtils.handleCallLocally(hash, proxy, this, method, args); 109 if (ret != null) 110 { 111 return ret; 112 } 113 114 ret = handleEjb21CallLocally(method, args); 115 if (ret != null) 116 { 117 return ret; 118 } 119 120 StatefulRemoteInvocation sri = new StatefulRemoteInvocation(interceptors, hash, method, method, null, id); 121 sri.setArguments(args); 122 sri.setInstanceResolver(metadata); 123 sri.getMetaData().addMetaData(Dispatcher.DISPATCHER, Dispatcher.OID, containerId, PayloadKey.AS_IS); 124 sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.INVOKER_LOCATOR, uri, PayloadKey.AS_IS); 125 sri.getMetaData().addMetaData(InvokeRemoteInterceptor.REMOTING, InvokeRemoteInterceptor.SUBSYSTEM, "AOP", PayloadKey.AS_IS); 126 127 if (provider != null) 128 { 129 sri.getMetaData().addMetaData(AsynchronousInterceptor.ASYNCH, AsynchronousInterceptor.INVOKE_ASYNCH, "YES", PayloadKey.AS_IS); 130 } 131 try 132 { 133 Object rtn = sri.invokeNext(); 134 if (id == null) 136 { 137 id = sri.getResponseAttachment(StatefulConstants.NEW_ID); 138 } 139 return rtn; 140 } 141 catch (ForwardId forward) 142 { 143 id = forward.getId(); 146 throw forward.getCause(); 147 } 148 } 149 150 public Object getAsynchronousProxy(Object proxy) 151 { 152 Class [] infs = proxy.getClass().getInterfaces(); 153 if (!ProxyUtils.isAsynchronous(infs)) 154 { 155 Class [] interfaces = ProxyUtils.addAsynchProviderInterface(infs); 156 AsynchMixin mixin = new AsynchMixin(); 157 Interceptor[] newInterceptors = ProxyUtils.addAsynchProxyInterceptor(mixin, interceptors); 158 StatefulRemoteProxy handler = new StatefulRemoteProxy(mixin, containerId, newInterceptors, uri); 159 return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, handler); 160 } 161 162 return proxy; 164 } 165 166 public String toString() 167 { 168 if (id != null) 169 { 170 return containerId.toString() + ":" + id.toString(); 171 } 172 else 173 { 174 GUID guid = new GUID(); 176 return containerId.toString() + ":" + guid.toString(); 177 } 178 } 179 180 private Object handleEjb21CallLocally(Method method, Object [] args) 181 { 182 if (method.equals(ProxyUtils.GET_HOME_HANDLE)) 183 { 184 return homeHandle; 185 } else if (method.equals(ProxyUtils.GET_EJB_METADATA)) 186 { 187 return ejbMetaData; 188 } else if (method.equals(ProxyUtils.GET_HANDLE)) 189 { 190 return handle; 191 } 192 193 return null; 194 } 195 } 196 | Popular Tags |