1 16 17 package org.springframework.ejb.access; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import javax.ejb.CreateException ; 23 import javax.ejb.EJBLocalObject ; 24 import javax.naming.NamingException ; 25 26 import org.aopalliance.intercept.MethodInvocation; 27 28 49 public class LocalSlsbInvokerInterceptor extends AbstractSlsbInvokerInterceptor { 50 51 58 public Object invoke(MethodInvocation invocation) throws Throwable { 59 EJBLocalObject ejb = null; 60 try { 61 ejb = getSessionBeanInstance(); 62 Method method = invocation.getMethod(); 63 if (method.getDeclaringClass().isInstance(ejb)) { 64 return method.invoke(ejb, invocation.getArguments()); 66 } 67 else { 68 Method ejbMethod = ejb.getClass().getMethod(method.getName(), method.getParameterTypes()); 70 return ejbMethod.invoke(ejb, invocation.getArguments()); 71 } 72 } 73 catch (InvocationTargetException ex) { 74 Throwable targetEx = ex.getTargetException(); 75 if (logger.isDebugEnabled()) { 76 logger.debug("Method of local EJB [" + getJndiName() + "] threw exception", targetEx); 77 } 78 if (targetEx instanceof CreateException ) { 79 throw new EjbAccessException("Could not create local EJB [" + getJndiName() + "]", targetEx); 80 } 81 else { 82 throw targetEx; 83 } 84 } 85 catch (NamingException ex) { 86 throw new EjbAccessException("Failed to locate local EJB [" + getJndiName() + "]", ex); 87 } 88 catch (IllegalAccessException ex) { 89 throw new EjbAccessException("Could not access method [" + invocation.getMethod().getName() + 90 "] of local EJB [" + getJndiName() + "]", ex); 91 } 92 finally { 93 releaseSessionBeanInstance(ejb); 94 } 95 } 96 97 104 protected EJBLocalObject getSessionBeanInstance() throws NamingException , InvocationTargetException { 105 return newSessionBeanInstance(); 106 } 107 108 114 protected void releaseSessionBeanInstance(EJBLocalObject ejb) { 115 removeSessionBeanInstance(ejb); 116 } 117 118 125 protected EJBLocalObject newSessionBeanInstance() throws NamingException , InvocationTargetException { 126 if (logger.isDebugEnabled()) { 127 logger.debug("Trying to create reference to local EJB"); 128 } 129 130 Object ejbInstance = create(); 132 if (!(ejbInstance instanceof EJBLocalObject )) { 133 throw new EjbAccessException("EJB instance [" + ejbInstance + "] is not a local SLSB"); 134 } 135 136 if (logger.isDebugEnabled()) { 137 logger.debug("Obtained reference to local EJB: " + ejbInstance); 138 } 139 return (EJBLocalObject ) ejbInstance; 140 } 141 142 147 protected void removeSessionBeanInstance(EJBLocalObject ejb) { 148 if (ejb != null) { 149 try { 150 ejb.remove(); 151 } 152 catch (Throwable ex) { 153 logger.warn("Could not invoke 'remove' on local EJB proxy", ex); 154 } 155 } 156 } 157 158 } 159 | Popular Tags |