1 45 46 47 package org.openejb.core.ivm; 48 49 import java.io.ObjectStreamException ; 50 import java.lang.reflect.Method ; 51 import java.rmi.RemoteException ; 52 53 import javax.ejb.EJBHome ; 54 import javax.ejb.EJBException ; 55 56 import org.openejb.ProxyInfo; 57 import org.openejb.RpcContainer; 58 import org.openejb.core.ThreadContext; 59 import org.openejb.util.proxy.ProxyManager; 60 61 77 public abstract class EjbHomeProxyHandler extends BaseEjbProxyHandler { 78 protected final static org.apache.log4j.Category logger = org.apache.log4j.Category.getInstance("OpenEJB"); 79 80 static final java.util.HashMap dispatchTable; 81 82 static { 84 dispatchTable = new java.util.HashMap (); 85 dispatchTable.put("create", new Integer (1)); 86 dispatchTable.put("getEJBMetaData", new Integer (2)); 87 dispatchTable.put("getHomeHandle", new Integer (3)); 88 dispatchTable.put("remove", new Integer (4)); 89 } 90 91 98 public EjbHomeProxyHandler(RpcContainer container, Object pk, Object depID) { 99 super(container, pk, depID); 100 } 101 102 public void invalidateReference(){ 103 throw new IllegalStateException ("A home reference must never be invalidated!"); 104 } 105 106 protected Object createProxy(ProxyInfo proxyInfo){ 107 108 if (proxyInfo instanceof SpecialProxyInfo) { 109 Object proxy = ((SpecialProxyInfo)proxyInfo).getProxy(); 110 if (proxy == null) throw new RuntimeException ("Could not create IVM proxy for "+proxyInfo.getInterface()+" interface"); 111 return proxy; 112 } 113 114 Object newProxy = null; 115 try { 116 EjbObjectProxyHandler handler = newEjbObjectHandler(proxyInfo.getBeanContainer(), proxyInfo.getPrimaryKey(), proxyInfo.getDeploymentInfo().getDeploymentID()); 117 handler.setLocal(isLocal()); 118 handler.doIntraVmCopy = this.doIntraVmCopy; 119 Class [] interfaces = new Class []{ proxyInfo.getInterface(), IntraVmProxy.class }; 120 newProxy = ProxyManager.newProxyInstance( interfaces , handler ); 121 } catch (IllegalAccessException iae) { 122 throw new RuntimeException ("Could not create IVM proxy for "+proxyInfo.getInterface()+" interface"); 123 } 124 if (newProxy == null) throw new RuntimeException ("Could not create IVM proxy for "+proxyInfo.getInterface()+" interface"); 125 126 return newProxy; 127 } 128 129 protected abstract EjbObjectProxyHandler newEjbObjectHandler(RpcContainer container, Object pk, Object depID); 130 131 protected Object _invoke(Object proxy, Method method, Object [] args) throws Throwable { 132 133 if (logger.isInfoEnabled()) { 134 logger.info("invoking method "+method.getName()+" on "+deploymentID); 135 } 136 137 String methodName = method.getName(); 138 139 try{ 140 java.lang.Object retValue; 141 Integer operation = (Integer )dispatchTable.get(methodName); 142 143 if(operation==null) { 144 if ( methodName.startsWith("find") ){ 145 retValue = findX(method, args, proxy); 146 } else { 147 throw new UnsupportedOperationException ("Unkown method: "+method); 149 } 150 }else { 151 switch(operation.intValue()) { 152 153 case 1: retValue = create(method, args, proxy); break; 154 155 case 2: retValue = getEJBMetaData(method, args, proxy); break; 156 157 case 3: retValue = getHomeHandle(method, args, proxy); break; 158 159 case 4: { 160 Class type = method.getParameterTypes()[0]; 161 162 163 if (javax.ejb.Handle .class.isAssignableFrom(type)) { 164 retValue = removeWithHandle(method, args, proxy); 165 } else { 166 167 retValue = removeByPrimaryKey(method, args, proxy); 168 } 169 break; 170 } 171 default: 172 throw new RuntimeException ("Inconsistent internal state: value "+operation.intValue()+" for operation "+methodName); 173 } 174 } 175 176 if(logger.isDebugEnabled()) { 177 logger.debug("finished invoking method "+method.getName()+". Return value:"+retValue); 178 } else if (logger.isInfoEnabled()) { 179 logger.info("finished invoking method "+method.getName()); 180 } 181 182 return retValue; 183 184 188 } catch (RemoteException re) { 189 if (isLocal()){ 190 throw new EJBException (re.getMessage(),(Exception )re.detail); 191 } else { 192 throw re; 193 } 194 195 } catch ( org.openejb.InvalidateReferenceException ire ) { 196 Throwable cause = ire.getRootCause(); 197 if (cause instanceof RemoteException && isLocal()){ 198 RemoteException re = (RemoteException )cause; 199 Throwable detail = (re.detail != null)? re.detail: re; 200 cause = new EJBException (re.getMessage(), (Exception ) detail); 201 } 202 throw cause; 203 207 } catch ( org.openejb.ApplicationException ae ) { 208 throw ae.getRootCause(); 209 213 } catch ( org.openejb.SystemException se ) { 214 if (isLocal()){ 215 throw new EJBException ("Container has suffered a SystemException", (Exception )se.getRootCause()); 216 } else { 217 throw new RemoteException ("Container has suffered a SystemException",se.getRootCause()); 218 } 219 } catch ( org.openejb.OpenEJBException oe ) { 220 if (isLocal()){ 221 throw new EJBException ("Unknown Container Exception", (Exception )oe.getRootCause()); 222 } else { 223 throw new RemoteException ("Unknown Container Exception",oe.getRootCause()); 224 } 225 } catch(Throwable t) { 226 logger.info("finished invoking method "+method.getName()+" with exception:"+t, t); 227 throw t; 228 } 229 } 230 231 232 233 234 235 257 protected Object create(Method method, Object [] args, Object proxy) throws Throwable { 258 ProxyInfo proxyInfo = (ProxyInfo) container.invoke(deploymentID,method,args,null, getThreadSpecificSecurityIdentity()); 259 return createProxy(proxyInfo); 260 } 261 262 284 protected abstract Object findX(Method method, Object [] args, Object proxy) throws Throwable ; 285 286 287 288 289 290 316 protected Object getEJBMetaData(Method method, Object [] args, Object proxy) throws Throwable { 317 checkAuthorization(method); 318 IntraVmMetaData metaData = new IntraVmMetaData(deploymentInfo.getHomeInterface(), deploymentInfo.getRemoteInterface(),deploymentInfo.getPrimaryKeyClass(), deploymentInfo.getComponentType()); 319 metaData.setEJBHome((EJBHome )proxy); 320 return metaData; 321 } 322 323 324 351 protected Object getHomeHandle(Method method, Object [] args, Object proxy) throws Throwable { 352 checkAuthorization(method); 353 return new IntraVmHandle(proxy); 354 } 355 public org.openejb.ProxyInfo getProxyInfo(){ 356 return new org.openejb.ProxyInfo(deploymentInfo, null, deploymentInfo.getHomeInterface(), container); 357 } 358 359 381 protected Object _writeReplace(Object proxy) throws ObjectStreamException { 382 386 if(IntraVmCopyMonitor.isIntraVmCopyOperation()){ 387 return new IntraVmArtifact( proxy ); 388 392 }else if(IntraVmCopyMonitor.isStatefulPassivationOperation()){ 393 return proxy; 394 398 } else{ 399 return org.openejb.OpenEJB.getApplicationServer().getEJBHome(this.getProxyInfo()); 400 } 401 } 402 403 433 protected Object removeWithHandle(Method method, Object [] args, Object proxy) throws Throwable { 434 435 IntraVmHandle handle = (IntraVmHandle)args[0]; 437 Object primKey = handle.getPrimaryKey(); 438 EjbObjectProxyHandler stub; 439 try{ 440 stub = (EjbObjectProxyHandler)ProxyManager.getInvocationHandler(handle.getEJBObject()); 441 }catch(IllegalArgumentException e) { 442 stub=null; 444 } 445 container.invoke(deploymentID, method, args, primKey, ThreadContext.getThreadContext().getSecurityIdentity()); 447 448 452 if(stub!=null) { 453 invalidateAllHandlers(stub.getRegistryId()); 454 } 455 return null; 456 } 457 458 484 protected abstract Object removeByPrimaryKey(Method method, Object [] args, Object proxy) throws Throwable ; 485 } 486 | Popular Tags |