1 25 26 package org.objectweb.easybeans.rpc; 27 28 import java.lang.reflect.Method ; 29 import java.rmi.NoSuchObjectException ; 30 import java.util.HashMap ; 31 import java.util.Hashtable ; 32 33 import javax.ejb.EJBException ; 34 import javax.ejb.EJBTransactionRequiredException ; 35 import javax.ejb.NoSuchEJBException ; 36 import javax.transaction.TransactionRequiredException ; 37 38 import org.objectweb.easybeans.rpc.api.ClientRPC; 39 import org.objectweb.easybeans.rpc.api.EJBRequest; 40 import org.objectweb.easybeans.rpc.api.EJBResponse; 41 import org.objectweb.easybeans.rpc.api.RPCException; 42 import org.objectweb.easybeans.rpc.util.Hash; 43 44 45 46 51 public class ClientRPCInvocationHandler extends AbsInvocationHandler { 52 53 56 private static final long serialVersionUID = 1852625501781836250L; 57 58 59 63 private Hashtable <?, ?> rmiClientEnvironment = null; 64 65 73 public ClientRPCInvocationHandler(final String containerId, final String factoryName, final boolean useID) { 74 super(containerId, factoryName, useID); 75 } 76 77 99 public Object invoke(final Object proxy, final Method method, final Object [] args) throws Exception { 100 101 if (isRemoved()) { 103 handleThrowable(convertThrowable(new NoSuchEJBException ("The bean has been removed")), false, method); 104 } 105 106 if (method.getDeclaringClass().getName().equals("java.lang.Object")) { 108 return handleObjectMethods(method, args); 109 } 110 111 ClientRPC client = RPC.getClient(rmiClientEnvironment); 112 113 if (getHashedMethods() == null) { 114 setHashedMethods(new HashMap <Method , Long >()); 115 } 116 117 Long hashLong = getHashedMethods().get(method); 118 if (hashLong == null) { 119 hashLong = Long.valueOf(Hash.hashMethod(method)); 120 getHashedMethods().put(method, hashLong); 121 } 122 123 long hash = hashLong.longValue(); 124 125 EJBRequest request = new JEJBRequest(method.getName(), hash, args, getContainerId(), getFactoryName(), getBeanId()); 126 127 EJBResponse response; 129 try { 130 response = client.sendEJBRequest(request); 131 } catch (RuntimeException e) { 132 throw new EJBException ("Error while sending a request", e); 134 } 135 setBeanId(response.getBeanId()); 137 138 setRemoved(response.isRemoved()); 140 141 RPCException rpcException = response.getRPCException(); 143 if (rpcException != null) { 144 handleThrowable(convertThrowable(rpcException.getCause()), rpcException.isApplicationException(), method); 145 } 146 147 return response.getValue(); 148 } 149 150 155 private Throwable convertThrowable(final Throwable throwable) { 156 if (isExtendingRmiRemote() && throwable instanceof NoSuchEJBException ) { 158 NoSuchObjectException ne = new NoSuchObjectException (throwable.getMessage()); 159 ne.detail = throwable; 160 return ne; 161 } 162 163 if (throwable instanceof javax.ejb.TransactionRequiredLocalException ) { 165 if (isExtendingRmiRemote()) { 166 TransactionRequiredException tre = new TransactionRequiredException (throwable.getMessage()); 167 tre.detail = throwable; 168 return tre; 169 } 170 EJBTransactionRequiredException ejbTransRequiredException = new EJBTransactionRequiredException (throwable 172 .getMessage()); 173 ejbTransRequiredException.initCause(throwable); 174 return ejbTransRequiredException; 175 } 176 return throwable; 177 } 178 179 183 public void setRMIEnv(final Hashtable <?, ?> rmiClientEnvironment) { 184 this.rmiClientEnvironment = rmiClientEnvironment; 185 186 } 187 188 189 } 190 | Popular Tags |