package org.jfox.ejb.invoker.soap;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
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 SOAPContainerInvoker extends ContainerInvokerSupport{
public SOAPContainerInvoker(ObjectId objectId, ContainerRemote invoker) {
super(objectId, 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 unmarshelledObject((byte[])invoker.invoke(invocation));
}
else if(method.equals(EJBHomeMethod.GetHomeHandle)) {
return unmarshelledObject((byte[])invoker.invoke(invocation));
}
else if(method.equals(EJBHomeMethod.RemoveByHandle)){
return unmarshelledObject((byte[])invoker.invoke(invocation));
}
else if(method.equals(EJBHomeMethod.RemoveByPrimaryKey)){
return unmarshelledObject((byte[])invoker.invoke(invocation));
}
else { byte[] bytes = (byte[])invoker.invoke(invocation); ObjectId objectId = (ObjectId)unmarshelledObject(bytes);
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
String remoteInterfaceName = objectId.getRemoteInterfaceName();
Class remoteInterface = ctxLoader.loadClass(remoteInterfaceName);
return Proxy.newProxyInstance(ctxLoader,
new Class[]{remoteInterface},
new SOAPContainerInvoker(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 unmarshelledObject((byte[])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 unmarshelledObject((byte[])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 unmarshelledObject((byte[])invoker.invoke(invocation));
}
else if(method.equals(EJBObjectMethod.Remove)){
return unmarshelledObject((byte[])invoker.invoke(invocation));
}
else {
return unmarshelledObject((byte[])invoker.invoke(invocation));
}
}
public static Object unmarshelledObject(byte[] bytes){
try {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = in.readObject();
in.close();
return obj;
}
catch(Exception e){
e.printStackTrace();
return bytes;
}
}
public static void main(String[] args) {
}
}