1 6 7 package org.jfox.ejb.connector; 8 9 import java.lang.reflect.Method ; 10 import java.lang.reflect.Proxy ; 11 import javax.ejb.EJBObject ; 12 13 import org.jfox.ejb.EJBHomeMethod; 14 import org.jfox.ejb.EJBObjectId; 15 import org.jfox.ejb.EJBObjectMethod; 16 import org.jfox.ejb.HandleImpl; 17 import org.jfox.ioc.connector.AbstractConnectorInvoker; 18 import org.jfox.ioc.connector.ConnectorHelper; 19 import org.jfox.ioc.connector.ConnectorInvoker; 20 import org.jfox.ioc.connector.ConnectorRemote; 21 import org.jfox.ioc.connector.ObjectId; 22 import org.jfox.ioc.connector.local.LOCALConnectorRemote; 23 import org.jfox.ioc.util.Methods; 24 import org.jfox.jndi.JNDIProperties; 25 26 29 30 public class EJBConnectorInvoker extends AbstractConnectorInvoker { 31 32 public EJBConnectorInvoker(ObjectId clientId, ConnectorRemote remote) { 33 super(clientId, remote); 34 } 35 36 protected Object doInvoke(Object proxy, Method method, Object [] args) throws Throwable { 37 EJBObjectId objectId = (EJBObjectId) getClientId(); 38 if(method.getName().equals(Methods.Equals)) { 39 Object obj = args[0]; 40 if(objectId.isHome()) { 41 return new Boolean (objectId.equals(((ConnectorInvoker) Proxy.getInvocationHandler(proxy)).getClientId())); 42 } 43 else if(obj instanceof EJBObject ) { 44 return invokeBean(proxy, EJBObjectMethod.IsIdentical, args); 45 } 46 else { 47 return Boolean.FALSE; 48 } 49 } 50 51 if(remote == null) { 52 synchronized(this) { 53 if(objectId.isCurrentVM()) { 55 remote = LOCALConnectorRemote.getInstance(); 56 } 57 else { 58 remote = ConnectorHelper.lookupConnector(JNDIProperties.getProviderURL()); 60 } 61 } 62 } 63 64 65 if(objectId.isHome()) { return invokeHome(proxy, method, args); 67 } 68 else { 69 return invokeBean(proxy, method, args); 70 } 71 72 } 73 74 protected Object invokeHome(Object proxy, Method method, Object [] args) throws Exception { 75 EJBObjectId objectId = (EJBObjectId) getClientId(); 76 EJBInvocation invocation = new EJBInvocation(objectId, getMethodHash(method), args); 77 78 if(method.equals(EJBHomeMethod.GetEJBMetaData)) { 79 return remote.invoke(invocation); 80 } 81 else if(method.equals(EJBHomeMethod.GetHomeHandle)) { 82 return remote.invoke(invocation); 85 } 86 else if(method.equals(EJBHomeMethod.RemoveByHandle)) { 87 return remote.invoke(invocation); 88 } 89 else if(method.equals(EJBHomeMethod.RemoveByPrimaryKey)) { 90 return remote.invoke(invocation); 91 } 92 else { Object obj = remote.invoke(invocation); EJBObjectId _objectId = (EJBObjectId) obj; 95 ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader(); 96 String remoteInterfaceName = _objectId.getRemoteInterfaceName(); 98 Class remoteInterface = ctxLoader.loadClass(remoteInterfaceName); 99 return Proxy.newProxyInstance(ctxLoader, 101 new Class []{remoteInterface}, 102 new EJBConnectorInvoker(_objectId, remote)); 103 } 104 } 105 106 protected Object invokeBean(Object proxy, Method method, Object [] args) throws Exception { 107 EJBObjectId objectId = (EJBObjectId) getClientId(); 108 EJBInvocation invocation = new EJBInvocation(objectId, getMethodHash(method), args); 109 if(method.equals(EJBObjectMethod.GetEJBHome)) { 110 return remote.invoke(invocation); 111 } 112 else if(method.equals(EJBObjectMethod.GetHandle)) { 113 ConnectorInvoker invocationHandler = (ConnectorInvoker) Proxy.getInvocationHandler(proxy); 114 return new HandleImpl(invocationHandler); 115 } 116 else if(method.equals(EJBObjectMethod.GetPrimaryKey)) { 117 return remote.invoke(invocation); 118 } 119 else if(method.equals(EJBObjectMethod.IsIdentical)) { 120 EJBObject ejbObject = (EJBObject ) args[0]; 122 Object _objectId = ((ConnectorInvoker) (Proxy.getInvocationHandler(ejbObject))).getClientId(); 123 invocation = new EJBInvocation(objectId, getMethodHash(method), new Object []{_objectId}); 124 return remote.invoke(invocation); 125 } 126 else if(method.equals(EJBObjectMethod.Remove)) { 127 return remote.invoke(invocation); 128 } 129 else { 130 return remote.invoke(invocation); 131 } 132 } 133 134 135 public static void main(String [] args) { 136 137 } 138 } 139 | Popular Tags |