package org.jfox.ejb.invoker;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.ejb.EJBObject;
import org.jfox.ejb.EJBObjectMethod;
import org.jfox.ejb.MethodHasher;
import org.jfox.ejb.ObjectId;
import org.jfox.ejb.ObjectMethod;
public abstract class ContainerInvokerSupport implements ClientContainerInvoker {
protected ObjectId objectId;
protected ContainerRemote invoker;
public ContainerInvokerSupport(ObjectId objectId, ContainerRemote invoker) {
this.objectId = objectId;
this.invoker = invoker;
}
public ObjectId getObjectId() {
return objectId;
}
public ContainerRemote getInvoker() {
return invoker;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.equals(ObjectMethod.ToString)) {
return ((ClientContainerInvoker)Proxy.getInvocationHandler(proxy)).getObjectId().toString();
}
else if(method.equals(ObjectMethod.HashCode)){
return new Integer(objectId.hashCode());
}
else if(method.getName().equals(ObjectMethod.Equals)) {
Object obj = args[0];
if(objectId.isHome()){
return new Boolean(objectId.equals(((ContainerInvokerSupport)Proxy.getInvocationHandler(proxy)).getObjectId()));
}
else if(obj instanceof EJBObject){
return invokeBean(proxy, EJBObjectMethod.IsIdentical,args);
}
else {
return Boolean.FALSE;
}
}
if(objectId.isHome()) { return invokeHome(proxy, method, args);
}
else {
return invokeBean(proxy, method, args);
}
}
protected abstract Object invokeHome(Object proxy, Method method, Object[] args) throws Exception;
protected abstract Object invokeBean(Object proxy, Method method, Object[] args) throws Exception;
protected static long getMethodHash(Method method) {
return MethodHasher.getMethodHash(method);
}
}