package org.jfox.ejb.invoker.jrmp;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.ejb.EJBObject;
import org.jfox.ejb.EJBHomeMethod;
import org.jfox.ejb.EJBObjectMethod;
import org.jfox.ejb.HandleImpl;
import org.jfox.ejb.Invocation;
import org.jfox.ejb.InvocationImpl;
import org.jfox.ejb.ObjectId;
import org.jfox.ejb.invoker.ContainerRemote;
import org.jfox.ejb.invoker.ContainerInvokerSupport;
public class JRMPContainerInvoker extends ContainerInvokerSupport {
public JRMPContainerInvoker(ObjectId objectHash, ContainerRemote invoker) {
super(objectHash,invoker);
}
protected Object invokeHome(Object proxy, Method method, Object[] args) throws Exception {
Invocation invocation = new InvocationImpl(objectId,new Long(getMethodHash(method)).toString(),args);
if(method.equals(EJBHomeMethod.GetEJBMetaData)) {
return invoker.invoke(invocation);
}
else if(method.equals(EJBHomeMethod.GetHomeHandle)) {
return invoker.invoke(invocation);
}
else if(method.equals(EJBHomeMethod.RemoveByHandle)){
return invoker.invoke(invocation);
}
else if(method.equals(EJBHomeMethod.RemoveByPrimaryKey)){
return invoker.invoke(invocation);
}
else { Object obj = invoker.invoke(invocation); ObjectId objectId = (ObjectId)obj;
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
String remoteInterfaceName = objectId.getRemoteInterfaceName();
Class remoteInterface = ctxLoader.loadClass(remoteInterfaceName);
return Proxy.newProxyInstance(ctxLoader,
new Class[]{remoteInterface},
new JRMPContainerInvoker(objectId,invoker));
}
}
protected Object invokeBean(Object proxy, Method method, Object[] args) throws Exception {
Invocation invocation = new InvocationImpl(objectId,new Long(getMethodHash(method)).toString(),args);
if(method.equals(EJBObjectMethod.GetEJBHome)) {
return invoker.invoke(invocation);
}
else if(method.equals(EJBObjectMethod.GetHandle)){
ContainerInvokerSupport invocationHandler = (ContainerInvokerSupport)Proxy.getInvocationHandler(proxy);
return new HandleImpl(invocationHandler);
}
else if(method.equals(EJBObjectMethod.GetPrimaryKey)){
return invoker.invoke(invocation);
}
else if(method.equals(EJBObjectMethod.IsIdentical)){
EJBObject ejbObject = (EJBObject)args[0];
Object _objectId = ((ContainerInvokerSupport)(Proxy.getInvocationHandler(ejbObject))).getObjectId();
invocation = new InvocationImpl(objectId,new Long(getMethodHash(method)).toString(),new Object[]{_objectId});
return invoker.invoke(invocation);
}
else if(method.equals(EJBObjectMethod.Remove)){
return invoker.invoke(invocation);
}
else {
return invoker.invoke(invocation);
}
}
}