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.Iterator ; 31 import java.util.HashMap ; 32 import java.util.Map ; 33 import java.util.logging.Logger ; 34 import java.util.logging.Level ; 35 36 import java.rmi.RemoteException ; 37 38 import javax.ejb.EJBException ; 39 import javax.ejb.EJBObject ; 40 import com.sun.ejb.Invocation; 41 import com.sun.ejb.InvocationInfo; 42 import com.sun.ejb.Container; 43 import com.sun.ejb.containers.util.MethodMap; 44 import com.sun.enterprise.EJBObjectToTie; 45 import com.sun.enterprise.util.LocalStringManagerImpl; 46 import com.sun.enterprise.util.Utility; 47 import com.sun.logging.LogDomains; 48 49 55 56 public final class EJBObjectInvocationHandler 57 extends EJBObjectImpl implements InvocationHandler { 58 59 private static Logger logger = LogDomains.getLogger(LogDomains.EJB_LOGGER); 60 61 private static LocalStringManagerImpl localStrings = 62 new LocalStringManagerImpl(EJBObjectInvocationHandler.class); 63 64 private MethodMap invocationInfoMap_; 68 69 private Class remoteIntf_; 70 71 74 public EJBObjectInvocationHandler(MethodMap invocationInfoMap, 75 Class remoteIntf) 76 throws Exception { 77 78 invocationInfoMap_ = invocationInfoMap; 79 80 remoteIntf_ = remoteIntf; 81 setIsRemoteHomeView(true); 82 83 } 86 87 90 public EJBObjectInvocationHandler(MethodMap invocationInfoMap) 91 throws Exception { 92 93 invocationInfoMap_ = invocationInfoMap; 94 95 setIsRemoteHomeView(false); 96 97 } 100 101 104 public Object invoke(Object proxy, Method method, Object [] args) 105 throws Throwable { 106 107 return invoke(remoteIntf_, method, args); 108 } 109 110 Object invoke(Class clientInterface, Method method, Object [] args) 111 throws Throwable { 112 113 ClassLoader originalClassLoader = null; 114 115 try { 118 container.onEnteringContainer(); 119 120 129 if( Thread.currentThread().getContextClassLoader() != 130 getContainer().getClassLoader() ) { 131 originalClassLoader = Utility.setContextClassLoader 132 (getContainer().getClassLoader()); 133 } 134 135 Class methodClass = method.getDeclaringClass(); 136 if( methodClass == java.lang.Object .class ) { 137 return InvocationHandlerUtil.invokeJavaObjectMethod 138 (this, method, args); 139 } 140 141 InvocationInfo invInfo = (InvocationInfo) 144 invocationInfoMap_.get(method, 145 ((args != null) ? args.length : 0) ); 146 147 if( invInfo == null ) { 148 throw new RemoteException ("Unknown Remote interface method :" 149 + method); 150 } 151 152 if( (methodClass == javax.ejb.EJBObject .class) || 153 invInfo.ejbIntfOverride ) { 154 return invokeEJBObjectMethod(method.getName(), args); 155 } else if( invInfo.targetMethod1 == null ) { 156 Object [] params = new Object [] 157 { invInfo.ejbName, "Remote", invInfo.method.toString() }; 158 String errorMsg = localStrings.getLocalString 159 ("ejb.bean_class_method_not_found", "", params); 160 161 logger.log(Level.SEVERE, "ejb.bean_class_method_not_found", 162 params); 163 throw new RemoteException (errorMsg); 164 } 165 166 168 Object returnValue = null; 169 170 Invocation inv = new Invocation(); 171 172 inv.isLocal = false; 173 inv.isHome = false; 174 inv.isBusinessInterface = !isRemoteHomeView(); 175 inv.ejbObject = this; 176 inv.method = method; 177 178 inv.clientInterface = clientInterface; 179 180 inv.transactionAttribute = invInfo.txAttr; 183 inv.securityPermissions = invInfo.securityPermissions; 184 inv.invocationInfo = invInfo; 185 inv.beanMethod = invInfo.targetMethod1; 186 inv.methodParams = args; 187 188 try { 189 container.preInvoke(inv); 190 returnValue = container.intercept(inv); 191 } catch(InvocationTargetException ite) { 192 inv.exception = ite.getCause(); 193 inv.exceptionFromBeanMethod = inv.exception; 194 } catch(Throwable t) { 195 inv.exception = t; 196 } finally { 197 container.postInvoke(inv); 198 } 199 200 if (inv.exception != null) { 201 InvocationHandlerUtil.throwRemoteException 202 (inv.exception, method.getExceptionTypes()); 203 } 204 205 return returnValue; 206 } finally { 207 208 if( originalClassLoader != null ) { 209 Utility.setContextClassLoader(originalClassLoader); 210 } 211 212 container.onLeavingContainer(); 213 } 214 } 215 216 217 private Object invokeEJBObjectMethod(String methodName, Object [] args) 218 throws Exception 219 { 220 Object returnValue = null; 222 223 224 229 if( methodName.equals("getEJBHome") ) { 230 231 returnValue = super.getEJBHome(); 232 233 } else if( methodName.equals("getHandle") ) { 234 235 returnValue = super.getHandle(); 236 237 } else if( methodName.equals("getPrimaryKey") ) { 238 239 returnValue = super.getPrimaryKey(); 240 241 } else if( methodName.equals("isIdentical") ) { 242 243 EJBObject other = (EJBObject ) args[0]; 246 247 returnValue = new Boolean (super.isIdentical(other)); 248 249 } else if( methodName.equals("remove") ) { 250 251 super.remove(); 252 253 } else { 254 255 throw new RemoteException ("unknown EJBObject method = " 256 + methodName); 257 } 258 259 return returnValue; 260 } 261 262 } 263 | Popular Tags |