1 25 26 package org.objectweb.easybeans.rpc; 27 28 import java.io.Externalizable ; 29 import java.io.IOException ; 30 import java.io.ObjectInput ; 31 import java.io.ObjectOutput ; 32 import java.lang.reflect.Method ; 33 import java.util.HashMap ; 34 35 import javax.ejb.NoSuchEJBException ; 36 37 import org.objectweb.easybeans.api.EZBContainer; 38 import org.objectweb.easybeans.api.EZBServer; 39 import org.objectweb.easybeans.api.Factory; 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 import org.objectweb.easybeans.server.EmbeddedManager; 44 45 49 public class LocalCallInvocationHandler extends AbsInvocationHandler implements Externalizable { 50 51 54 private static final long serialVersionUID = -4327634481654235615L; 55 56 59 private Integer embeddedID = null; 60 61 62 66 private transient Factory factory = null; 67 68 77 public LocalCallInvocationHandler(final Integer embeddedID, final String containerId, final String factoryName, 78 final boolean useID) { 79 super(containerId, factoryName, useID); 80 81 this.embeddedID = embeddedID; 83 84 initFactory(); 86 } 87 88 91 public LocalCallInvocationHandler() { 92 super(null, null, false); 93 } 94 95 98 private void initFactory() { 99 EZBServer ejb3Server = EmbeddedManager.getEmbedded(embeddedID); 101 if (ejb3Server == null) { 102 throw new IllegalStateException ("Cannot find the server with id '" + embeddedID + "'."); 103 } 104 105 EZBContainer container = ejb3Server.getContainer(getContainerId()); 107 if (container == null) { 108 throw new IllegalStateException ("Cannot find the container with id '" + getContainerId() + "'."); 109 } 110 111 factory = container.getFactory(getFactoryName()); 112 if (factory == null) { 113 throw new IllegalStateException ("Cannot find the factory with name '" + getFactoryName() + "'."); 114 } 115 } 116 117 139 public Object invoke(final Object proxy, final Method method, final Object [] args) throws Exception { 140 if (isRemoved()) { 142 throw new NoSuchEJBException ("The bean has been removed"); 143 } 144 145 if (method.getDeclaringClass().getName().equals("java.lang.Object")) { 147 return handleObjectMethods(method, args); 148 } 149 if (getHashedMethods() == null) { 150 setHashedMethods(new HashMap <Method , Long >()); 151 } 152 153 Long hashLong = getHashedMethods().get(method); 154 if (hashLong == null) { 155 hashLong = Long.valueOf(Hash.hashMethod(method)); 156 getHashedMethods().put(method, hashLong); 157 } 158 159 long hash = hashLong.longValue(); 160 161 EJBResponse response = null; 163 response = factory.localCall(hash, args, getBeanId()); 164 setBeanId(response.getBeanId()); 165 166 setRemoved(response.isRemoved()); 168 169 RPCException rpcException = response.getRPCException(); 170 if (rpcException != null) { 171 handleThrowable(rpcException.getCause(), rpcException.isApplicationException(), method); 172 } 173 174 175 176 177 return response.getValue(); 178 179 } 180 181 186 public void writeExternal(final ObjectOutput out) throws IOException { 187 out.writeObject(embeddedID); 189 out.writeObject(getContainerId()); 191 out.writeObject(getFactoryName()); 193 out.writeObject(getInterfaceClassName()); 195 out.writeBoolean(isExtendingRmiRemote()); 197 198 out.writeBoolean(isUsingID()); 200 201 out.writeBoolean(isRemoved()); 203 } 204 205 212 public void readExternal(final ObjectInput in) throws IOException , ClassNotFoundException { 213 this.embeddedID = (Integer ) in.readObject(); 215 setContainerId((String ) in.readObject()); 217 setFactoryName((String ) in.readObject()); 219 220 setInterfaceClassName((String ) in.readObject()); 222 223 setExtendingRmiRemote(in.readBoolean()); 225 226 setUseID(in.readBoolean()); 228 229 setRemoved(in.readBoolean()); 231 232 initFactory(); 234 235 } 236 237 } 238 | Popular Tags |