1 45 package org.openejb.client; 46 47 import java.lang.reflect.Method ; 48 import java.rmi.RemoteException ; 49 50 import javax.ejb.EJBObject ; 51 52 import org.openejb.client.proxy.ProxyManager; 53 54 59 public abstract class EJBObjectHandler extends EJBInvocationHandler { 60 61 62 protected static final Method GETEJBHOME = getMethod(EJBObject .class, "getEJBHome", null); 63 protected static final Method GETHANDLE = getMethod(EJBObject .class, "getHandle", null); 64 protected static final Method GETPRIMARYKEY = getMethod(EJBObject .class, "getPrimaryKey", null); 65 protected static final Method ISIDENTICAL = getMethod(EJBObject .class, "isIdentical", new Class []{EJBObject .class}); 66 protected static final Method REMOVE = getMethod(EJBObject .class, "remove", null); 67 68 protected static final Method GETHANDLER = getMethod(EJBObjectProxy.class, "getEJBObjectHandler", null); 69 70 79 public Object registryId; 80 81 82 EJBHomeProxy ejbHome = null; 83 84 86 public EJBObjectHandler(){ 87 } 88 89 public EJBObjectHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client){ 90 super(ejb, server, client); 91 } 92 93 public EJBObjectHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client, Object primaryKey){ 94 super(ejb, server, client, primaryKey); 95 } 96 97 protected void setEJBHomeProxy(EJBHomeProxy ejbHome){ 98 this.ejbHome = ejbHome; 99 } 100 101 public static EJBObjectHandler createEJBObjectHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client, Object primaryKey){ 102 103 switch (ejb.type) { 104 case EJBMetaDataImpl.BMP_ENTITY: 105 case EJBMetaDataImpl.CMP_ENTITY: 106 107 return new EntityEJBObjectHandler(ejb, server, client, primaryKey); 108 109 case EJBMetaDataImpl.STATEFUL: 110 111 return new StatefulEJBObjectHandler(ejb, server, client, primaryKey); 112 113 case EJBMetaDataImpl.STATELESS: 114 115 return new StatelessEJBObjectHandler(ejb, server, client, primaryKey); 116 } 117 return null; 118 } 119 132 public abstract Object getRegistryId(); 133 134 public EJBObjectProxy createEJBObjectProxy(){ 135 136 EJBObjectProxy ejbObject = null; 137 138 try{ 139 140 Class [] interfaces = new Class []{ EJBObjectProxy.class, ejb.remoteClass }; 141 ejbObject = (EJBObjectProxy) ProxyManager.newProxyInstance(interfaces, this); 142 143 } catch (IllegalAccessException e){ 144 e.printStackTrace(); 146 } 147 return ejbObject; 148 } 149 150 151 public synchronized Object _invoke(Object p, Method m, Object [] a) throws Throwable { 158 159 Object retValue = null; 160 164 165 try{ 166 167 String methodName = m.getName(); 168 if (m.getDeclaringClass() == Object .class ) { 169 if ( m.equals( TOSTRING ) ){ 170 return "proxy="+this; 171 } else if ( m.equals( EQUALS ) ) { 172 return Boolean.FALSE; 174 } else if ( m.equals( HASHCODE ) ) { 176 return new Integer ( this.hashCode() ); 177 } else { 178 throw new UnsupportedOperationException ("Unkown method: "+m); 179 } 180 } else if (m.getDeclaringClass() == EJBObjectProxy.class ) { 181 if ( m.equals( GETHANDLER ) ){ 182 return this; 183 } else if (methodName.equals("writeReplace")) { 184 return new EJBObjectProxyHandle(this); 185 } else if (methodName.equals("readResolve")) { 186 } else { 189 throw new UnsupportedOperationException ("Unkown method: "+m); 190 } 191 } else if ( m.getDeclaringClass() == javax.ejb.EJBObject .class) { 192 if( m.equals( GETHANDLE )) retValue = getHandle(m,a,p); 193 else if(m.equals(GETPRIMARYKEY)) retValue = getPrimaryKey(m,a,p); 194 else if(m.equals(ISIDENTICAL)) retValue = isIdentical(m,a,p); 195 else if(m.equals(GETEJBHOME)) retValue = getEJBHome(m,a,p); 196 else if(m.equals(REMOVE)) retValue = remove(m,a,p); 197 else throw new UnsupportedOperationException ("Unkown method: "+m); 198 } else if ( m.getDeclaringClass() == ejb.remoteClass ) { 199 retValue = businessMethod(m,a,p); 200 } else { 201 throw new UnsupportedOperationException ("Unkown method: "+m); 202 } 203 204 205 209 }catch ( org.openejb.InvalidateReferenceException ire ) { 210 invalidateAllHandlers(getRegistryId()); 211 return ire.getRootCause(); 212 216 } catch ( org.openejb.ApplicationException ae ) { 217 throw ae.getRootCause(); 218 222 } catch ( org.openejb.SystemException se ) { 223 invalidateReference(); 224 throw new RemoteException ("Container has suffered a SystemException",se.getRootCause()); 225 } catch ( org.openejb.OpenEJBException oe ) { 226 throw new RemoteException ("Unknown Container Exception",oe.getRootCause()); 227 } 228 return retValue; 229 } 230 231 232 protected Object getEJBHome(Method method, Object [] args, Object proxy) throws Throwable { 233 if ( ejbHome == null ) { 234 ejbHome = EJBHomeHandler.createEJBHomeHandler(ejb, server, client).createEJBHomeProxy(); 235 } 236 return ejbHome; 237 } 238 239 protected Object getHandle(Method method, Object [] args, Object proxy) throws Throwable { 240 return new EJBObjectHandle((EJBObjectProxy)proxy); 241 } 242 243 244 protected abstract Object getPrimaryKey(Method method, Object [] args, Object proxy) throws Throwable ; 245 246 protected abstract Object isIdentical(Method method, Object [] args, Object proxy) throws Throwable ; 247 248 protected abstract Object remove(Method method, Object [] args, Object proxy) throws Throwable ; 249 250 protected Object businessMethod(Method method, Object [] args, Object proxy) throws Throwable { 251 254 EJBRequest req = new EJBRequest( EJB_OBJECT_BUSINESS_METHOD ); 255 256 req.setMethodParameters( args ); 257 req.setMethodInstance( method ); 258 req.setClientIdentity( client.getClientIdentity() ); 259 req.setDeploymentCode( ejb.deploymentCode ); 260 req.setDeploymentId( ejb.deploymentID ); 261 req.setPrimaryKey( primaryKey ); 262 263 EJBResponse res = request( req ); 264 265 switch (res.getResponseCode()) { 278 case EJB_ERROR: 279 throw (Throwable )res.getResult(); 281 case EJB_SYS_EXCEPTION: 282 throw (Throwable )res.getResult(); 284 case EJB_APP_EXCEPTION: 285 throw (Throwable )res.getResult(); 287 case EJB_OK: 288 return res.getResult(); 289 default: 290 throw new RemoteException ("Received invalid response code from server: "+res.getResponseCode()); 291 } 292 } 293 } 294 | Popular Tags |