Coverage Report - org.kuali.rice.ksb.messaging.KSBClientProxy
 
Classes in this File Line Coverage Branch Coverage Complexity
KSBClientProxy
0%
0/23
0%
0/12
4.333
 
 1  
 /**
 2  
  * Copyright 2005-2011 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 org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
 21  
 
 22  
 import javax.xml.namespace.QName;
 23  
 import java.lang.reflect.InvocationHandler;
 24  
 import java.lang.reflect.Method;
 25  
 import java.lang.reflect.Proxy;
 26  
 
 27  
 /**
 28  
  * This class creates a proxy for services deployed on KSB. A 
 29  
  * reference to the service is obtained only upon the first method
 30  
  * invocation.
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  *
 34  
  */
 35  
 public class KSBClientProxy implements InvocationHandler {
 36  
 
 37  0
 private static final Logger LOG = Logger.getLogger(KSBClientProxy.class);
 38  
     
 39  
     private QName serviceName;
 40  
     private volatile Object service;
 41  
 
 42  
 
 43  
     public static <T> T newInstance(String serviceQName, Class<T> interfaceClass) throws InstantiationException, IllegalAccessException {
 44  0
         if (StringUtils.isBlank(serviceQName)) {
 45  0
             throw new IllegalArgumentException("the qname was blank");
 46  
         }
 47  
 
 48  0
         if (interfaceClass == null) {
 49  0
             throw new IllegalArgumentException("the interfaceClass was null");
 50  
         }
 51  
 
 52  
         @SuppressWarnings("unchecked")
 53  0
         final T t = (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] { interfaceClass }, new KSBClientProxy(serviceQName));
 54  0
         return t;
 55  
     }
 56  
 
 57  0
     public KSBClientProxy(String serviceQName){
 58  0
         if (StringUtils.isBlank(serviceQName)) {
 59  0
             throw new IllegalArgumentException("the qname was blank");
 60  
         }
 61  
 
 62  0
         this.serviceName = QName.valueOf(serviceQName);
 63  0
     }
 64  
     
 65  
     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 66  
         //using DCL idiom
 67  
         //see effective java 2nd ed. pg. 71
 68  0
         Object s = service;
 69  0
         if (s == null) {
 70  0
             synchronized (this) {
 71  0
                 s = service;
 72  0
                 if (s == null) {
 73  0
                     service = s = GlobalResourceLoader.getService(serviceName);
 74  
                 }
 75  0
             }
 76  
         }
 77  
 
 78  0
         if (s != null) {
 79  0
             return method.invoke(s, args);
 80  
         }
 81  
 
 82  0
         LOG.warn("serviceName: " + serviceName + " was not found");
 83  0
         return null;
 84  
     }
 85  
 }