1 16 17 package org.springframework.remoting.caucho; 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.UndeclaredThrowableException ; 21 import java.net.ConnectException ; 22 import java.net.MalformedURLException ; 23 24 import com.caucho.hessian.client.HessianProxyFactory; 25 import com.caucho.hessian.client.HessianRuntimeException; 26 import com.caucho.hessian.io.SerializerFactory; 27 import org.aopalliance.intercept.MethodInterceptor; 28 import org.aopalliance.intercept.MethodInvocation; 29 30 import org.springframework.remoting.RemoteAccessException; 31 import org.springframework.remoting.RemoteConnectFailureException; 32 import org.springframework.remoting.RemoteLookupFailureException; 33 import org.springframework.remoting.RemoteProxyFailureException; 34 import org.springframework.remoting.support.UrlBasedRemoteAccessor; 35 import org.springframework.util.Assert; 36 37 62 public class HessianClientInterceptor extends UrlBasedRemoteAccessor implements MethodInterceptor { 63 64 private HessianProxyFactory proxyFactory = new HessianProxyFactory(); 65 66 private Object hessianProxy; 67 68 69 75 public void setProxyFactory(HessianProxyFactory proxyFactory) { 76 this.proxyFactory = (proxyFactory != null ? proxyFactory : new HessianProxyFactory()); 77 } 78 79 85 public void setSerializerFactory(SerializerFactory serializerFactory) { 86 this.proxyFactory.setSerializerFactory(serializerFactory); 87 } 88 89 93 public void setSendCollectionType(boolean sendCollectionType) { 94 this.proxyFactory.getSerializerFactory().setSendCollectionType(sendCollectionType); 95 } 96 102 public void setUsername(String username) { 103 this.proxyFactory.setUser(username); 104 } 105 106 112 public void setPassword(String password) { 113 this.proxyFactory.setPassword(password); 114 } 115 116 121 public void setOverloadEnabled(boolean overloadEnabled) { 122 this.proxyFactory.setOverloadEnabled(overloadEnabled); 123 } 124 125 126 public void afterPropertiesSet() { 127 super.afterPropertiesSet(); 128 prepare(); 129 } 130 131 135 public void prepare() throws RemoteLookupFailureException { 136 try { 137 this.hessianProxy = createHessianProxy(this.proxyFactory); 138 } 139 catch (MalformedURLException ex) { 140 throw new RemoteLookupFailureException("Service URL [" + getServiceUrl() + "] is invalid", ex); 141 } 142 } 143 144 151 protected Object createHessianProxy(HessianProxyFactory proxyFactory) throws MalformedURLException { 152 Assert.notNull(getServiceInterface(), "'serviceInterface' is required"); 153 return proxyFactory.create(getServiceInterface(), getServiceUrl()); 154 } 155 156 157 public Object invoke(MethodInvocation invocation) throws Throwable { 158 if (this.hessianProxy == null) { 159 throw new IllegalStateException ("HessianClientInterceptor is not properly initialized - " + 160 "invoke 'prepare' before attempting any operations"); 161 } 162 163 try { 164 return invocation.getMethod().invoke(this.hessianProxy, invocation.getArguments()); 165 } 166 catch (InvocationTargetException ex) { 167 if (ex.getTargetException() instanceof HessianRuntimeException) { 168 HessianRuntimeException hre = (HessianRuntimeException) ex.getTargetException(); 169 Throwable rootCause = (hre.getRootCause() != null ? hre.getRootCause() : hre); 170 throw convertHessianAccessException(rootCause); 171 } 172 else if (ex.getTargetException() instanceof UndeclaredThrowableException ) { 173 UndeclaredThrowableException utex = (UndeclaredThrowableException ) ex.getTargetException(); 174 throw convertHessianAccessException(utex.getUndeclaredThrowable()); 175 } 176 throw ex.getTargetException(); 177 } 178 catch (Throwable ex) { 179 throw new RemoteProxyFailureException( 180 "Failed to invoke Hessian proxy for remote service [" + getServiceUrl() + "]", ex); 181 } 182 } 183 184 190 protected RemoteAccessException convertHessianAccessException(Throwable ex) { 191 if (ex instanceof ConnectException ) { 192 throw new RemoteConnectFailureException( 193 "Cannot connect to Hessian remote service at [" + getServiceUrl() + "]", ex); 194 } 195 else { 196 throw new RemoteAccessException( 197 "Cannot access Hessian remote service at [" + getServiceUrl() + "]", ex); 198 } 199 } 200 201 } 202 | Popular Tags |