1 23 package com.sun.ejb.containers; 24 25 import java.lang.reflect.InvocationHandler ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Proxy ; 28 import java.lang.reflect.Method ; 29 30 import java.util.logging.Logger ; 31 import java.util.logging.Level ; 32 33 import java.util.Map ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 37 import java.rmi.RemoteException ; 38 import javax.rmi.CORBA.Tie ; 39 import javax.ejb.EJBException ; 40 import javax.ejb.EJBHome ; 41 import com.sun.ejb.Invocation; 42 import com.sun.ejb.InvocationInfo; 43 import com.sun.ejb.Container; 44 import com.sun.ejb.containers.util.MethodMap; 45 import com.sun.enterprise.deployment.EjbDescriptor; 46 import com.sun.enterprise.deployment.EjbSessionDescriptor; 47 import com.sun.enterprise.util.LocalStringManagerImpl; 48 import com.sun.enterprise.util.Utility; 49 50 import com.sun.logging.LogDomains; 51 52 57 58 final class EJBHomeInvocationHandler 59 extends ReadOnlyEJBHomeImpl implements InvocationHandler { 60 61 private static Logger logger = LogDomains.getLogger(LogDomains.EJB_LOGGER); 62 63 private static LocalStringManagerImpl localStrings = 64 new LocalStringManagerImpl(EJBHomeInvocationHandler.class); 65 66 private boolean isStatelessSession_; 67 private boolean isEntity_; 68 69 private EJBHome proxy_; 72 73 private Class homeIntfClass_; 74 75 private MethodMap invocationInfoMap_; 81 82 EJBHomeInvocationHandler(EjbDescriptor ejbDescriptor, 83 Class homeIntfClass, 84 MethodMap invocationInfoMap) 85 throws Exception { 86 87 if( ejbDescriptor instanceof EjbSessionDescriptor ) { 88 isEntity_ = false; 89 isStatelessSession_ = 90 ((EjbSessionDescriptor)ejbDescriptor).isStateless(); 91 } else { 92 isStatelessSession_ = false; 93 isEntity_ = true; 94 } 95 96 invocationInfoMap_ = invocationInfoMap; 97 98 homeIntfClass_ = homeIntfClass; 99 100 } 103 104 public void setProxy(EJBHome proxy) { 105 proxy_ = proxy; 106 } 107 108 protected EJBHome getEJBHome() { 109 return proxy_; 110 } 111 112 115 public Object invoke(Object proxy, Method method, Object [] args) 116 throws Throwable { 117 118 ClassLoader originalClassLoader = null; 119 120 try { 123 ((BaseContainer) getContainer()).onEnteringContainer(); 124 125 134 if( Thread.currentThread().getContextClassLoader() != 135 getContainer().getClassLoader() ) { 136 originalClassLoader = Utility.setContextClassLoader 137 (getContainer().getClassLoader()); 138 } 139 140 Class methodClass = method.getDeclaringClass(); 141 142 if( methodClass == java.lang.Object .class ) { 143 return InvocationHandlerUtil.invokeJavaObjectMethod 144 (this, method, args); 145 } else if( methodClass == ReadOnlyEJBHome.class ) { 146 if( method.getName().equals("_refresh_All") ) { 147 super._refresh_All(); 148 } else { 149 super._refresh_com_sun_ejb_containers_read_only_bean_ 150 (args[0]); 151 } 152 return null; 153 } 154 155 InvocationInfo invInfo = (InvocationInfo) 158 invocationInfoMap_.get(method, 159 ((args != null) ? args.length : 0) ); 160 161 if( invInfo == null ) { 162 163 throw new RemoteException ("Unknown Home interface method :" 164 + method); 165 166 } else if( (methodClass == javax.ejb.EJBHome .class) || 167 invInfo.ejbIntfOverride ) { 168 169 return invokeEJBHomeMethod(method.getName(), args); 170 171 } else if( GenericEJBHome.class.isAssignableFrom(methodClass) ) { 172 173 EJBObjectImpl busObjectImpl = createRemoteBusinessObjectImpl(); 176 return busObjectImpl.getStub((String ) args[0]); 177 178 } 179 180 EJBObjectImpl ejbObjectImpl = null; 182 Object returnValue = null; 183 184 if( !isEntity_ && invInfo.startsWithCreate ) { 185 ejbObjectImpl = createEJBObjectImpl(); 186 returnValue = ejbObjectImpl.getStub(); 187 } 188 189 if( !isStatelessSession_ ) { 190 191 if( invInfo.targetMethod1 == null ) { 192 193 String errorMsg = localStrings.getLocalString 194 ("ejb.bean_class_method_not_found", "", new Object [] 195 { invInfo.ejbName, "Home", 196 invInfo.method.toString() }); 197 logger.log(Level.SEVERE, errorMsg); 198 throw new RemoteException (errorMsg); 199 200 } 201 202 Invocation inv = new Invocation(); 203 204 inv.isLocal = false; 205 inv.method = method; 206 inv.isHome = true; 207 208 inv.clientInterface = homeIntfClass_; 209 210 213 inv.transactionAttribute = invInfo.txAttr; 214 inv.securityPermissions = invInfo.securityPermissions; 215 inv.invocationInfo = invInfo; 216 217 if( !isEntity_ && invInfo.startsWithCreate ) { 218 inv.ejbObject = (EJBLocalRemoteObject) ejbObjectImpl; 219 } 220 221 BaseContainer container = (BaseContainer) getContainer(); 222 223 try { 224 225 container.preInvoke(inv); 226 227 if( invInfo.startsWithCreate ) { 228 229 Object ejbCreateReturnValue = container. 230 invokeTargetBeanMethod(invInfo.targetMethod1, 231 inv, inv.ejb, args, null); 232 if( isEntity_ ) { 233 container.postCreate(inv, ejbCreateReturnValue); 234 container.invokeTargetBeanMethod 235 (invInfo.targetMethod2, 236 inv, inv.ejb, args, null); 237 } 238 if( inv.ejbObject != null ) { 239 returnValue = ((EJBObjectImpl)inv.ejbObject). 240 getStub(); 241 } 242 243 } else if (invInfo.startsWithFindByPrimaryKey) { 244 EntityContainer entityContainer = (EntityContainer) 245 container; 246 returnValue = entityContainer.invokeFindByPrimaryKey 247 (invInfo.targetMethod1, inv, args); 248 249 } else if ( invInfo.startsWithFind ) { 250 251 Object pKeys = container.invokeTargetBeanMethod 252 (invInfo.targetMethod1, inv, inv.ejb, args, null); 253 254 returnValue = container.postFind(inv, pKeys, null); 255 256 } else { 257 258 returnValue = container.invokeTargetBeanMethod 259 (invInfo.targetMethod1, inv, inv.ejb, args, null); 260 } 261 } catch(InvocationTargetException ite) { 262 inv.exception = ite.getCause(); 263 } catch(Throwable c) { 264 inv.exception = c; 265 } finally { 266 container.postInvoke(inv); 267 } 268 269 if (inv.exception != null) { 270 InvocationHandlerUtil.throwRemoteException 271 (inv.exception, method.getExceptionTypes()); 272 } 273 } 274 275 return returnValue; 276 } finally { 277 278 if( originalClassLoader != null ) { 279 Utility.setContextClassLoader(originalClassLoader); 280 } 281 282 ((BaseContainer) getContainer()).onLeavingContainer(); 283 } 284 } 285 286 private Object invokeEJBHomeMethod(String methodName, Object [] args) 287 throws Exception 288 { 289 Object returnValue = null; 291 292 297 if( methodName.equals("getEJBMetaData") ) { 298 299 returnValue = super.getEJBMetaData(); 300 301 } else if( methodName.equals("getHomeHandle") ) { 302 303 returnValue = super.getHomeHandle(); 304 305 } else if( methodName.equals("remove") ) { 306 307 if( args[0] instanceof javax.ejb.Handle ) { 308 super.remove((javax.ejb.Handle )args[0]); 309 } else { 310 super.remove(args[0]); 311 } 312 313 } else { 314 315 throw new RemoteException ("unknown EJBHome method = " + methodName); 316 317 } 318 319 return returnValue; 320 } 321 } 322 | Popular Tags |