Spring¿ò¼Üѧϰ[Spring HTTPµ÷ÓÃÆ÷ʵÏÖÔ¶³Ìµ÷ÓÃ](ËÄ)
eptor 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(
"Cou