1 45 package org.openejb.client; 46 47 import java.io.Externalizable ; 48 import java.io.IOException ; 49 import java.io.ObjectInput ; 50 import java.io.ObjectOutput ; 51 import java.lang.reflect.Method ; 52 import java.rmi.RemoteException ; 53 54 import javax.ejb.EJBHome ; 55 import javax.ejb.Handle ; 56 57 import org.openejb.client.proxy.ProxyManager; 58 59 64 public abstract class EJBHomeHandler extends EJBInvocationHandler implements Externalizable { 65 66 protected static final Method GETEJBMETADATA= getMethod(EJBHome .class, "getEJBMetaData", null); 67 protected static final Method GETHOMEHANDLE = getMethod(EJBHome .class, "getHomeHandle", null); 68 protected static final Method REMOVE_W_KEY = getMethod(EJBHome .class, "remove", new Class []{Object .class}); 69 protected static final Method REMOVE_W_HAND = getMethod(EJBHome .class, "remove", new Class []{Handle .class}); 70 protected static final Method GETHANDLER = getMethod(EJBHomeProxy.class, "getEJBHomeHandler", null); 71 72 75 public EJBHomeHandler() { 76 } 77 78 public EJBHomeHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client){ 79 super(ejb, server, client); 80 } 81 82 public static EJBHomeHandler createEJBHomeHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client) { 83 84 switch (ejb.type) { 85 case EJBMetaDataImpl.BMP_ENTITY: 86 case EJBMetaDataImpl.CMP_ENTITY: 87 88 return new EntityEJBHomeHandler(ejb, server, client); 89 90 case EJBMetaDataImpl.STATEFUL: 91 92 return new StatefulEJBHomeHandler(ejb, server, client); 93 94 case EJBMetaDataImpl.STATELESS: 95 96 return new StatelessEJBHomeHandler(ejb, server, client); 97 } 98 return null; 99 100 } 101 102 104 public EJBHomeProxy createEJBHomeProxy(){ 105 try{ 106 Class [] interfaces = new Class []{ EJBHomeProxy.class, ejb.homeClass }; 107 return (EJBHomeProxy) ProxyManager.newProxyInstance(interfaces, this); 108 } catch (IllegalAccessException e){ 109 e.printStackTrace(); 111 } 112 return null; 113 } 114 115 protected Object _invoke(Object proxy, Method method, Object [] args) throws Throwable { 116 117 String methodName = method.getName(); 118 119 try{ 120 121 if (method.getDeclaringClass() == Object .class ) { 122 if ( method.equals( TOSTRING ) ){ 123 return "proxy="+this; 124 } else if ( method.equals( EQUALS ) ) { 125 return Boolean.FALSE; 127 } else if ( method.equals( HASHCODE ) ) { 129 return new Integer ( this.hashCode() ); 130 } else { 133 throw new UnsupportedOperationException ("Unkown method: "+method); 134 } 135 } else if (method.getDeclaringClass() == EJBHomeProxy.class ) { 136 if ( method.equals( GETHANDLER ) ){ 137 return this; 138 } else if (methodName.equals("writeReplace")) { 139 return new EJBHomeProxyHandle(this); 140 } else if (methodName.equals("readResolve")) { 141 throw new UnsupportedOperationException ("Unkown method: "+method); 143 } else { 145 throw new UnsupportedOperationException ("Unkown method: "+method); 146 } 147 } 148 149 151 152 153 if ( methodName.equals("create") ) { 154 return create(method, args, proxy); 155 156 157 } else if ( methodName.startsWith("find") ){ 158 return findX(method, args, proxy); 159 160 161 162 163 } else if ( method.equals( GETEJBMETADATA ) ) { 164 return getEJBMetaData(method, args, proxy); 165 166 167 168 169 } else if ( method.equals( GETHOMEHANDLE ) ) { 170 return getHomeHandle(method, args, proxy); 171 172 173 174 175 } else if ( method.equals( REMOVE_W_HAND ) ) { 176 return removeWithHandle(method, args, proxy); 177 178 } else if ( method.equals( REMOVE_W_KEY ) ) { 179 return removeByPrimaryKey(method, args, proxy); 180 181 182 183 } else { 184 185 throw new UnsupportedOperationException ("Unkown method: "+method); 186 187 } 188 } catch ( org.openejb.SystemException se ) { 191 invalidateReference(); 192 throw new RemoteException ("Container has suffered a SystemException",se.getRootCause()); 193 } 194 195 196 } 197 198 199 200 201 202 224 protected Object create(Method method, Object [] args, Object proxy) throws Throwable { 225 EJBRequest req = new EJBRequest( EJB_HOME_CREATE ); 226 227 req.setClientIdentity( client.getClientIdentity() ); 228 req.setDeploymentCode( ejb.deploymentCode ); 229 req.setDeploymentId( ejb.deploymentID ); 230 req.setMethodInstance( method ); 231 req.setMethodParameters( args ); 232 233 EJBResponse res = request( req ); 234 235 switch (res.getResponseCode()) { 236 case EJB_SYS_EXCEPTION: 237 throw (Throwable )res.getResult(); 238 case EJB_APP_EXCEPTION: 239 throw (Throwable )res.getResult(); 240 case EJB_ERROR: 241 throw (Throwable )res.getResult(); 242 case EJB_OK: 243 Object primKey = res.getResult(); 245 EJBObjectHandler handler = EJBObjectHandler.createEJBObjectHandler(ejb,server,client,primKey); 246 handler.setEJBHomeProxy((EJBHomeProxy)proxy); 247 return handler.createEJBObjectProxy(); 249 default: 250 throw new RemoteException ("Received invalid response code from server: "+res.getResponseCode()); 251 } 252 } 253 254 276 protected abstract Object findX(Method method, Object [] args, Object proxy) throws Throwable ; 277 278 279 280 281 282 307 protected Object getEJBMetaData(Method method, Object [] args, Object proxy) throws Throwable { 308 return ejb; 309 } 310 311 337 protected Object getHomeHandle(Method method, Object [] args, Object proxy) throws Throwable { 338 return new EJBHomeHandle((EJBHomeProxy)proxy); 340 } 341 342 368 protected abstract Object removeWithHandle(Method method, Object [] args, Object proxy) throws Throwable ; 369 370 396 protected abstract Object removeByPrimaryKey(Method method, Object [] args, Object proxy) throws Throwable ; 397 398 399 411 public void readExternal(ObjectInput in) throws IOException ,ClassNotFoundException { 412 } 413 428 public void writeExternal(ObjectOutput out) throws IOException { 429 } 430 431 } 432 433 434 | Popular Tags |