1 16 17 package org.springframework.remoting.rmi; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.rmi.ConnectException ; 22 import java.rmi.ConnectIOException ; 23 import java.rmi.MarshalException ; 24 import java.rmi.NoSuchObjectException ; 25 import java.rmi.Remote ; 26 import java.rmi.RemoteException ; 27 import java.rmi.StubNotFoundException ; 28 import java.rmi.UnknownHostException ; 29 import java.rmi.UnmarshalException ; 30 import java.util.Arrays ; 31 32 import org.aopalliance.intercept.MethodInvocation; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 import org.springframework.remoting.RemoteAccessException; 37 import org.springframework.remoting.RemoteConnectFailureException; 38 import org.springframework.remoting.RemoteProxyFailureException; 39 40 49 public abstract class RmiClientInterceptorUtils { 50 51 private static final String ORACLE_CONNECTION_EXCEPTION = "com.evermind.server.rmi.RMIConnectionException"; 52 53 private static final Log logger = LogFactory.getLog(RmiClientInterceptorUtils.class); 54 55 56 72 public static Object invoke(MethodInvocation invocation, Remote stub, String serviceName) throws Throwable { 73 try { 74 return doInvoke(invocation, stub); 75 } 76 catch (InvocationTargetException ex) { 77 Throwable targetEx = ex.getTargetException(); 78 if (targetEx instanceof RemoteException ) { 79 RemoteException rex = (RemoteException ) targetEx; 80 throw convertRmiAccessException(invocation.getMethod(), rex, serviceName); 81 } 82 else { 83 throw targetEx; 84 } 85 } 86 } 87 88 96 public static Object doInvoke(MethodInvocation invocation, Remote stub) throws InvocationTargetException { 97 Method method = invocation.getMethod(); 98 try { 99 if (method.getDeclaringClass().isInstance(stub)) { 100 return method.invoke(stub, invocation.getArguments()); 102 } 103 else { 104 Method stubMethod = stub.getClass().getMethod(method.getName(), method.getParameterTypes()); 106 return stubMethod.invoke(stub, invocation.getArguments()); 107 } 108 } 109 catch (InvocationTargetException ex) { 110 throw ex; 111 } 112 catch (NoSuchMethodException ex) { 113 throw new RemoteProxyFailureException("No matching RMI stub method found for: " + method, ex); 114 } 115 catch (Throwable ex) { 116 throw new RemoteProxyFailureException("Invocation of RMI stub method failed: " + method, ex); 117 } 118 } 119 120 133 public static Exception convertRmiAccessException(Method method, Throwable ex, String message) { 134 if (logger.isDebugEnabled()) { 135 logger.debug(message, ex); 136 } 137 if (!Arrays.asList(method.getExceptionTypes()).contains(RemoteException .class)) { 138 return new RemoteAccessException(message, ex); 139 } 140 else { 141 return new RemoteException (message, ex); 142 } 143 } 144 145 154 public static Exception convertRmiAccessException(Method method, RemoteException ex, String serviceName) { 155 return convertRmiAccessException(method, ex, isConnectFailure(ex), serviceName); 156 } 157 158 169 public static Exception convertRmiAccessException( 170 Method method, RemoteException ex, boolean isConnectFailure, String serviceName) { 171 172 if (logger.isDebugEnabled()) { 173 logger.debug("Remote service [" + serviceName + "] threw exception", ex); 174 } 175 if (!Arrays.asList(method.getExceptionTypes()).contains(RemoteException .class)) { 176 if (isConnectFailure) { 177 return new RemoteConnectFailureException("Cannot connect to remote service [" + serviceName + "]", ex); 178 } 179 else { 180 return new RemoteAccessException("Cannot access remote service [" + serviceName + "]", ex); 181 } 182 } 183 else { 184 return ex; 185 } 186 } 187 188 205 public static boolean isConnectFailure(RemoteException ex) { 206 return (ex instanceof ConnectException || ex instanceof ConnectIOException || 207 ex instanceof UnknownHostException || 208 ex instanceof NoSuchObjectException || ex instanceof StubNotFoundException || 209 ex instanceof MarshalException || ex instanceof UnmarshalException || 210 ORACLE_CONNECTION_EXCEPTION.equals(ex.getClass().getName())); 211 } 212 213 } 214 | Popular Tags |