1 16 17 package org.springframework.remoting.httpinvoker; 18 19 import java.io.IOException ; 20 import java.net.ConnectException ; 21 22 import org.aopalliance.intercept.MethodInterceptor; 23 import org.aopalliance.intercept.MethodInvocation; 24 25 import org.springframework.aop.support.AopUtils; 26 import org.springframework.beans.factory.BeanClassLoaderAware; 27 import org.springframework.remoting.RemoteAccessException; 28 import org.springframework.remoting.RemoteConnectFailureException; 29 import org.springframework.remoting.support.RemoteInvocation; 30 import org.springframework.remoting.support.RemoteInvocationBasedAccessor; 31 import org.springframework.remoting.support.RemoteInvocationResult; 32 import org.springframework.util.ClassUtils; 33 34 66 public class HttpInvokerClientInterceptor extends RemoteInvocationBasedAccessor 67 implements MethodInterceptor, HttpInvokerClientConfiguration, BeanClassLoaderAware { 68 69 private String codebaseUrl; 70 71 private HttpInvokerRequestExecutor httpInvokerRequestExecutor; 72 73 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 74 75 76 88 public void setCodebaseUrl(String codebaseUrl) { 89 this.codebaseUrl = codebaseUrl; 90 } 91 92 95 public String getCodebaseUrl() { 96 return this.codebaseUrl; 97 } 98 99 108 public void setHttpInvokerRequestExecutor(HttpInvokerRequestExecutor httpInvokerRequestExecutor) { 109 this.httpInvokerRequestExecutor = httpInvokerRequestExecutor; 110 } 111 112 117 public HttpInvokerRequestExecutor getHttpInvokerRequestExecutor() { 118 if (this.httpInvokerRequestExecutor == null) { 119 SimpleHttpInvokerRequestExecutor executor = new SimpleHttpInvokerRequestExecutor(); 120 executor.setBeanClassLoader(this.beanClassLoader); 121 this.httpInvokerRequestExecutor = executor; 122 } 123 return this.httpInvokerRequestExecutor; 124 } 125 126 public void setBeanClassLoader(ClassLoader classLoader) { 127 this.beanClassLoader = classLoader; 128 } 129 130 134 protected final ClassLoader getBeanClassLoader() { 135 return this.beanClassLoader; 136 } 137 138 public void afterPropertiesSet() { 139 super.afterPropertiesSet(); 140 141 getHttpInvokerRequestExecutor(); 143 } 144 145 146 public Object invoke(MethodInvocation methodInvocation) throws Throwable { 147 if (AopUtils.isToStringMethod(methodInvocation.getMethod())) { 148 return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]"; 149 } 150 151 RemoteInvocation invocation = createRemoteInvocation(methodInvocation); 152 RemoteInvocationResult result = null; 153 try { 154 result = executeRequest(invocation); 155 } 156 catch (Throwable ex) { 157 throw convertHttpInvokerAccessException(ex); 158 } 159 return recreateRemoteInvocationResult(result); 160 } 161 162 176 protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws Exception { 177 return getHttpInvokerRequestExecutor().executeRequest(this, invocation); 178 } 179 180 186 protected RemoteAccessException convertHttpInvokerAccessException(Throwable ex) { 187 if (ex instanceof ConnectException ) { 188 throw new RemoteConnectFailureException( 189 "Cannot connect to HTTP invoker remote service at [" + getServiceUrl() + "]", ex); 190 } 191 else if (ex instanceof ClassNotFoundException ) { 192 throw new RemoteAccessException( 193 "Cannot deserialize result from HTTP invoker remote service [" + getServiceUrl() + "]", ex); 194 } 195 else { 196 throw new RemoteAccessException( 197 "Cannot access HTTP invoker remote service at [" + getServiceUrl() + "]", ex); 198 } 199 } 200 201 } 202 | Popular Tags |