Coverage Report - org.kuali.rice.ksb.messaging.BusClientFailureProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
BusClientFailureProxy
0%
0/46
0%
0/12
4.5
 
 1  
 /*
 2  
  * Copyright 2007 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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.List;
 26  
 
 27  
 import org.apache.commons.httpclient.ConnectTimeoutException;
 28  
 import org.apache.commons.httpclient.ConnectionPoolTimeoutException;
 29  
 import org.apache.commons.httpclient.NoHttpResponseException;
 30  
 import org.apache.log4j.Logger;
 31  
 import org.kuali.rice.core.proxy.BaseTargetedInvocationHandler;
 32  
 import org.kuali.rice.core.resourceloader.ContextClassLoaderProxy;
 33  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 34  
 import org.kuali.rice.ksb.messaging.resourceloader.KSBResourceLoaderFactory;
 35  
 
 36  
 
 37  
 
 38  
 public class BusClientFailureProxy extends BaseTargetedInvocationHandler {
 39  
 
 40  0
         private static final Logger LOG = Logger.getLogger(BusClientFailureProxy.class);
 41  
 
 42  
         private ServiceInfo serviceInfo;
 43  
 
 44  
         // exceptions that will cause this Proxy to remove the service from the bus
 45  0
         private static List<Class> serviceRemovalExceptions = new ArrayList<Class>();
 46  0
         private static List<Integer> serviceRemovalResponseCodes = new ArrayList<Integer>();
 47  
 
 48  
         static {
 49  0
                 serviceRemovalExceptions.add(NoHttpResponseException.class);
 50  0
                 serviceRemovalExceptions.add(InterruptedIOException.class);
 51  0
                 serviceRemovalExceptions.add(UnknownHostException.class);
 52  0
                 serviceRemovalExceptions.add(NoRouteToHostException.class);
 53  0
                 serviceRemovalExceptions.add(ConnectTimeoutException.class);
 54  0
                 serviceRemovalExceptions.add(ConnectionPoolTimeoutException.class);
 55  0
                 serviceRemovalExceptions.add(ConnectException.class);
 56  
         }
 57  
         
 58  
         static {
 59  0
             serviceRemovalResponseCodes.add(new Integer(404));
 60  0
         serviceRemovalResponseCodes.add(new Integer(503));
 61  0
         }
 62  
 
 63  
         private BusClientFailureProxy(Object target, ServiceInfo serviceInfo) {
 64  0
                 super(target);
 65  0
                 this.serviceInfo = serviceInfo;
 66  0
         }
 67  
 
 68  
         public static Object wrap(Object target, ServiceInfo serviceInfo) {
 69  0
                 return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy.getInterfacesToProxyIncludeSpring(target), new BusClientFailureProxy(target, serviceInfo));
 70  
         }
 71  
 
 72  
         protected Object invokeInternal(Object proxyObject, Method method, Object[] params) throws Throwable {
 73  0
                 Object service = getTarget();
 74  
                 
 75  
                 do {
 76  
                         try {
 77  0
                                 return method.invoke(service, params);
 78  0
                         } catch (Throwable throwable) {                        
 79  0
                                 if (isServiceRemovalException(throwable)) {
 80  0
                                         LOG.error("Exception caught accessing remote service " + this.serviceInfo.getQname(), throwable);
 81  0
                                         RemoteResourceServiceLocator remoteResourceLocator = KSBResourceLoaderFactory.getRemoteResourceLocator();
 82  0
                                         remoteResourceLocator.removeService(this.serviceInfo);
 83  
         
 84  0
                                         service = remoteResourceLocator.getService(this.serviceInfo.getQname());
 85  
                                                                         
 86  0
                                         if (service != null) {
 87  0
                                                 LOG.info("Refetched replacement service for service " + this.serviceInfo.getQname());
 88  
                                                 
 89  
                                                 // as per KULRICE-4287, reassign target to the new service we just fetched, hopefully this one works better!
 90  0
                                                 setTarget(service);
 91  
                                                 
 92  
                                         } else {
 93  0
                                                 LOG.error("Didn't find replacement service throwing exception");
 94  0
                                                 throw throwable;                                        
 95  
                                         }
 96  0
                                 } else {
 97  0
                                         throw throwable;
 98  
                                 }
 99  
                         }
 100  0
                 } while (true);
 101  
         }
 102  
 
 103  
         private static boolean isServiceRemovalException(Throwable throwable) {
 104  0
                 LOG.info("Checking for Service Removal Exception: " + throwable.getClass().getName());
 105  0
                 if (serviceRemovalExceptions.contains(throwable.getClass())) {
 106  0
                         LOG.info("Found a Service Removal Exception: " + throwable.getClass().getName());
 107  0
                         return true;
 108  0
                 } else if (throwable instanceof HttpException) {
 109  0
                         HttpException httpException = (HttpException)throwable;
 110  0
                         if (serviceRemovalResponseCodes.contains(httpException.getResponseCode())) {
 111  0
                                 LOG.info("Found a Service Removal Exception because of a " + httpException.getResponseCode() + " " + throwable.getClass().getName());
 112  0
                                 return true;
 113  
                         }
 114  
                 }
 115  0
                 if (throwable.getCause() != null) {
 116  0
                         LOG.info("Unwrapping Throwable cause to check for service removal exception from: " + throwable.getClass().getName());
 117  0
                         return isServiceRemovalException(throwable.getCause());
 118  
                 }
 119  0
                 return false;
 120  
         }
 121  
 }