1 16 17 package org.springframework.ejb.access; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.rmi.RemoteException ; 21 22 import javax.ejb.EJBObject ; 23 import javax.naming.NamingException ; 24 import javax.rmi.PortableRemoteObject ; 25 26 import org.aopalliance.intercept.MethodInvocation; 27 28 import org.springframework.remoting.RemoteConnectFailureException; 29 import org.springframework.remoting.RemoteLookupFailureException; 30 import org.springframework.remoting.rmi.RmiClientInterceptorUtils; 31 32 41 public abstract class AbstractRemoteSlsbInvokerInterceptor extends AbstractSlsbInvokerInterceptor { 42 43 private Class homeInterface; 44 45 private boolean refreshHomeOnConnectFailure = false; 46 47 48 57 public void setHomeInterface(Class homeInterface) { 58 if (homeInterface != null && !homeInterface.isInterface()) { 59 throw new IllegalArgumentException ( 60 "Home interface class [" + homeInterface.getClass() + "] is not an interface"); 61 } 62 this.homeInterface = homeInterface; 63 } 64 65 76 public void setRefreshHomeOnConnectFailure(boolean refreshHomeOnConnectFailure) { 77 this.refreshHomeOnConnectFailure = refreshHomeOnConnectFailure; 78 } 79 80 protected boolean isHomeRefreshable() { 81 return this.refreshHomeOnConnectFailure; 82 } 83 84 85 91 protected Object lookup() throws NamingException { 92 Object homeObject = super.lookup(); 93 if (this.homeInterface != null) { 94 try { 95 homeObject = PortableRemoteObject.narrow(homeObject, this.homeInterface); 96 } 97 catch (ClassCastException ex) { 98 throw new RemoteLookupFailureException( 99 "Could not narrow EJB home stub to home interface [" + this.homeInterface.getName() + "]", ex); 100 } 101 } 102 return homeObject; 103 } 104 105 106 117 public Object invoke(MethodInvocation invocation) throws Throwable { 118 try { 119 return doInvoke(invocation); 120 } 121 catch (RemoteConnectFailureException ex) { 122 return handleRemoteConnectFailure(invocation, ex); 123 } 124 catch (RemoteException ex) { 125 if (isConnectFailure(ex)) { 126 return handleRemoteConnectFailure(invocation, ex); 127 } 128 else { 129 throw ex; 130 } 131 } 132 } 133 134 141 protected boolean isConnectFailure(RemoteException ex) { 142 return RmiClientInterceptorUtils.isConnectFailure(ex); 143 } 144 145 private Object handleRemoteConnectFailure(MethodInvocation invocation, Exception ex) throws Throwable { 146 if (this.refreshHomeOnConnectFailure) { 147 if (logger.isDebugEnabled()) { 148 logger.debug("Could not connect to remote EJB [" + getJndiName() + "] - retrying", ex); 149 } 150 else if (logger.isWarnEnabled()) { 151 logger.warn("Could not connect to remote EJB [" + getJndiName() + "] - retrying"); 152 } 153 return refreshAndRetry(invocation); 154 } 155 else { 156 throw ex; 157 } 158 } 159 160 168 protected Object refreshAndRetry(MethodInvocation invocation) throws Throwable { 169 try { 170 refreshHome(); 171 } 172 catch (NamingException ex) { 173 throw new RemoteLookupFailureException("Failed to locate remote EJB [" + getJndiName() + "]", ex); 174 } 175 return doInvoke(invocation); 176 } 177 178 179 188 protected abstract Object doInvoke(MethodInvocation invocation) throws Throwable ; 189 190 191 199 protected EJBObject newSessionBeanInstance() throws NamingException , InvocationTargetException { 200 if (logger.isDebugEnabled()) { 201 logger.debug("Trying to create reference to remote EJB"); 202 } 203 204 Object ejbInstance = create(); 206 if (!(ejbInstance instanceof EJBObject )) { 207 throw new RemoteLookupFailureException( 208 "EJB instance [" + ejbInstance + "] is not a Remote Stateless Session Bean"); 209 } 210 212 if (logger.isDebugEnabled()) { 213 logger.debug("Obtained reference to remote EJB: " + ejbInstance); 214 } 215 return (EJBObject ) ejbInstance; 216 } 217 218 224 protected void removeSessionBeanInstance(EJBObject ejb) { 225 if (ejb != null) { 226 try { 227 ejb.remove(); 228 } 229 catch (Throwable ex) { 230 logger.warn("Could not invoke 'remove' on remote EJB proxy", ex); 231 } 232 } 233 } 234 235 } 236 | Popular Tags |