package org.jfox.ejb.invoker.local;
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 LOCALContainerInvoker extends ContainerInvokerSupport{
public LOCALContainerInvoker(ObjectId objectId, ContainerRemote invoker) {
super(objectId, invoker);
if(!(invoker instanceof LOCALContainerService)) {
throw new RuntimeException(invoker + " is not a instance of " + LOCALContainerService.class);
}
}
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 LOCALContainerInvoker(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];
ObjectId _objectId = ((ContainerInvokerSupport)(Proxy.getInvocationHandler(ejbObject))).getObjectId();
invocation = new InvocationImpl(objectId,new Long(getMethodHash(method)).toString(),new ObjectId[]{_objectId});
return invoker.invoke(invocation);
}
else if(method.equals(EJBObjectMethod.Remove)){
return invoker.invoke(invocation);
}
else {
return invoker.invoke(invocation);
}
}
public static void main(String[] args) {
}
}