[java] view plaincopyprint
- public class HttpInvokerClientInterceptor extends RemoteInvocationBasedAccessor implements MethodInterceptor, HttpInvokerClientConfiguration {
- private String codebaseUrl; //HTTP调用请求执行器
- private HttpInvokerRequestExecutor httpInvokerRequestExecutor; public void setCodebaseUrl(String codebaseUrl) {
- this.codebaseUrl = codebaseUrl; }
- public String getCodebaseUrl() { return this.codebaseUrl;
- } public void setHttpInvokerRequestExecutor(HttpInvokerRequestExecutor httpInvokerRequestExecutor) {
- this.httpInvokerRequestExecutor = httpInvokerRequestExecutor; }
- //获取HTTP调用请求执行器,如果HTTP调用请求执行器没有设置,则使用 //SimpleHttpInvokerRequestExecutor作为HTTP调用请求执行器
- public HttpInvokerRequestExecutor getHttpInvokerRequestExecutor() { if (this.httpInvokerRequestExecutor == null) {
- SimpleHttpInvokerRequestExecutor executor = new SimpleHttpInvokerRequestExecutor(); executor.setBeanClassLoader(getBeanClassLoader());
- this.httpInvokerRequestExecutor = executor; }
- return this.httpInvokerRequestExecutor; }
- //IoC容器初始化完成回调方法 public void afterPropertiesSet() {
- //调用父类的初始化回调方法 super.afterPropertiesSet();
- //获取HTTP调用请求执行器 getHttpInvokerRequestExecutor();
- } //拦截器代理对象方法调用入口,拦截器将客户端对远程调用代理的调用封装为
- //MethodInvocation对象。 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
- if (AopUtils.isToStringMethod(methodInvocation.getMethod())) { return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]";
- } //创建远程调用对象,封装了远程调用
- RemoteInvocation invocation = createRemoteInvocation(methodInvocation); //远程调用结果
- RemoteInvocationResult result = null; try {
- //远程调用入口 result = executeRequest(invocation, methodInvocation);
- } catch (Throwable ex) {
- throw convertHttpInvokerAccessException(ex); }
- try { //返回远程调用结果
- return recreateRemoteInvocationResult(result); }
- catch (Throwable ex) { if (result.hasInvocationTargetException()) {
- throw ex; }
- else { throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() +
- "] failed in HTTP invoker remote service at [" + getServiceUrl() + "]", ex); }
- } }
- //执行远程调用入口 protected RemoteInvocationResult executeRequest(
- RemoteInvocation invocation, MethodInvocation originalInvocation) throws Exception { return executeRequest(invocation);
- } //通过HTTP调用请求执行器执行远程调用
- protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws Exception { return getHttpInvokerRequestExecutor().executeRequest(this, invocation);
- } //将远程调用异常转换成Spring异常
- protected RemoteAccessException convertHttpInvokerAccessException(Throwable ex) { if (ex instanceof ConnectException) {
- throw new RemoteConnectFailureException( "Could not connect to HTTP invoker remote service at [" + getServiceUrl() + "]", ex);
- } else if (ex instanceof ClassNotFoundException || ex instanceof NoClassDefFoundError ||
- ex instanceof InvalidClassException) { throw new RemoteAccessException(
- "Could not deserialize result from HTTP invoker remote service [" + getServiceUrl() + "]", ex); }
- else { throw new RemoteAccessException(
- "Could not access HTTP invoker remote service at [" + getServiceUrl() + "]", ex); }
- } }