1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging; |
17 | |
|
18 | |
import java.io.InterruptedIOException; |
19 | |
import java.lang.reflect.Method; |
20 | |
import java.lang.reflect.Proxy; |
21 | |
import java.net.ConnectException; |
22 | |
import java.net.NoRouteToHostException; |
23 | |
import java.net.UnknownHostException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.List; |
27 | |
import java.util.Set; |
28 | |
|
29 | |
import org.apache.commons.httpclient.ConnectTimeoutException; |
30 | |
import org.apache.commons.httpclient.ConnectionPoolTimeoutException; |
31 | |
import org.apache.commons.httpclient.NoHttpResponseException; |
32 | |
import org.apache.log4j.Logger; |
33 | |
import org.kuali.rice.core.impl.resourceloader.ContextClassLoaderProxy; |
34 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
35 | |
import org.kuali.rice.core.util.reflect.BaseTargetedInvocationHandler; |
36 | |
import org.kuali.rice.ksb.api.KsbApiServiceLocator; |
37 | |
import org.kuali.rice.ksb.api.bus.Endpoint; |
38 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
39 | |
|
40 | |
|
41 | |
|
42 | |
public class BusClientFailureProxy extends BaseTargetedInvocationHandler { |
43 | |
|
44 | 0 | private static final Logger LOG = Logger.getLogger(BusClientFailureProxy.class); |
45 | |
|
46 | 0 | private final Object failoverLock = new Object(); |
47 | |
|
48 | |
private ServiceConfiguration serviceConfiguration; |
49 | |
|
50 | |
|
51 | 0 | private static List<Class<?>> serviceRemovalExceptions = new ArrayList<Class<?>>(); |
52 | 0 | private static List<Integer> serviceRemovalResponseCodes = new ArrayList<Integer>(); |
53 | |
|
54 | |
static { |
55 | 0 | serviceRemovalExceptions.add(NoHttpResponseException.class); |
56 | 0 | serviceRemovalExceptions.add(InterruptedIOException.class); |
57 | 0 | serviceRemovalExceptions.add(UnknownHostException.class); |
58 | 0 | serviceRemovalExceptions.add(NoRouteToHostException.class); |
59 | 0 | serviceRemovalExceptions.add(ConnectTimeoutException.class); |
60 | 0 | serviceRemovalExceptions.add(ConnectionPoolTimeoutException.class); |
61 | 0 | serviceRemovalExceptions.add(ConnectException.class); |
62 | |
} |
63 | |
|
64 | |
static { |
65 | 0 | serviceRemovalResponseCodes.add(new Integer(404)); |
66 | 0 | serviceRemovalResponseCodes.add(new Integer(503)); |
67 | 0 | } |
68 | |
|
69 | |
private BusClientFailureProxy(Object target, ServiceConfiguration serviceConfiguration) { |
70 | 0 | super(target); |
71 | 0 | this.serviceConfiguration = serviceConfiguration; |
72 | 0 | } |
73 | |
|
74 | |
public static Object wrap(Object target, ServiceConfiguration serviceConfiguration) { |
75 | 0 | return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy.getInterfacesToProxy(target), new BusClientFailureProxy(target, serviceConfiguration)); |
76 | |
} |
77 | |
|
78 | |
protected Object invokeInternal(Object proxyObject, Method method, Object[] params) throws Throwable { |
79 | 0 | Set<ServiceConfiguration> servicesTried = null; |
80 | |
|
81 | |
do { |
82 | |
try { |
83 | 0 | return method.invoke(getTarget(), params); |
84 | 0 | } catch (Throwable throwable) { |
85 | 0 | if (isServiceRemovalException(throwable)) { |
86 | 0 | synchronized (failoverLock) { |
87 | 0 | LOG.error("Exception caught accessing remote service " + this.serviceConfiguration.getServiceName(), throwable); |
88 | 0 | if (servicesTried == null) { |
89 | 0 | servicesTried = new HashSet<ServiceConfiguration>(); |
90 | 0 | servicesTried.add(serviceConfiguration); |
91 | |
} |
92 | 0 | Object failoverService = null; |
93 | 0 | List<Endpoint> endpoints = KsbApiServiceLocator.getServiceBus().getEndpoints(serviceConfiguration.getServiceName()); |
94 | 0 | for (Endpoint endpoint : endpoints) { |
95 | 0 | if (!servicesTried.contains(endpoint.getServiceConfiguration())) { |
96 | 0 | failoverService = endpoint.getService(); |
97 | 0 | servicesTried.add(endpoint.getServiceConfiguration()); |
98 | |
} |
99 | |
} |
100 | 0 | if (failoverService != null) { |
101 | 0 | LOG.info("Refetched replacement service for service " + this.serviceConfiguration.getServiceName()); |
102 | |
|
103 | 0 | setTarget(failoverService); |
104 | |
} else { |
105 | 0 | LOG.error("Didn't find replacement service throwing exception"); |
106 | 0 | throw throwable; |
107 | |
} |
108 | 0 | } |
109 | |
} else { |
110 | 0 | throw throwable; |
111 | |
} |
112 | |
} |
113 | 0 | } while (true); |
114 | |
} |
115 | |
|
116 | |
private static boolean isServiceRemovalException(Throwable throwable) { |
117 | 0 | LOG.info("Checking for Service Removal Exception: " + throwable.getClass().getName()); |
118 | 0 | if (serviceRemovalExceptions.contains(throwable.getClass())) { |
119 | 0 | LOG.info("Found a Service Removal Exception: " + throwable.getClass().getName()); |
120 | 0 | return true; |
121 | 0 | } else if (throwable instanceof HttpException) { |
122 | 0 | HttpException httpException = (HttpException)throwable; |
123 | 0 | if (serviceRemovalResponseCodes.contains(httpException.getResponseCode())) { |
124 | 0 | LOG.info("Found a Service Removal Exception because of a " + httpException.getResponseCode() + " " + throwable.getClass().getName()); |
125 | 0 | return true; |
126 | |
} |
127 | |
} |
128 | 0 | if (throwable.getCause() != null) { |
129 | 0 | LOG.info("Unwrapping Throwable cause to check for service removal exception from: " + throwable.getClass().getName()); |
130 | 0 | return isServiceRemovalException(throwable.getCause()); |
131 | |
} |
132 | 0 | return false; |
133 | |
} |
134 | |
|
135 | |
} |