001 /** 002 * Copyright 2005-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.rice.ksb.messaging; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.apache.commons.lang.exception.ExceptionUtils; 020 import org.apache.log4j.Logger; 021 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 022 023 import javax.xml.namespace.QName; 024 import java.lang.reflect.InvocationHandler; 025 import java.lang.reflect.InvocationTargetException; 026 import java.lang.reflect.Method; 027 import java.lang.reflect.Proxy; 028 029 /** 030 * This class creates a proxy for services deployed on KSB. A 031 * reference to the service is obtained only upon the first method 032 * invocation. 033 * 034 * @author Kuali Rice Team (rice.collab@kuali.org) 035 * 036 */ 037 public class KSBClientProxy implements InvocationHandler { 038 039 private static final Logger LOG = Logger.getLogger(KSBClientProxy.class); 040 041 private QName serviceName; 042 private volatile Object service; 043 044 045 public static <T> T newInstance(String serviceQName, Class<T> interfaceClass) throws InstantiationException, IllegalAccessException { 046 if (StringUtils.isBlank(serviceQName)) { 047 throw new IllegalArgumentException("the qname was blank"); 048 } 049 050 if (interfaceClass == null) { 051 throw new IllegalArgumentException("the interfaceClass was null"); 052 } 053 054 @SuppressWarnings("unchecked") 055 final T t = (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] { interfaceClass }, new KSBClientProxy(serviceQName)); 056 return t; 057 } 058 059 public KSBClientProxy(String serviceQName){ 060 if (StringUtils.isBlank(serviceQName)) { 061 throw new IllegalArgumentException("the qname was blank"); 062 } 063 064 this.serviceName = QName.valueOf(serviceQName); 065 } 066 067 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 068 //using DCL idiom 069 //see effective java 2nd ed. pg. 71 070 Object s = service; 071 if (s == null) { 072 synchronized (this) { 073 s = service; 074 if (s == null) { 075 service = s = GlobalResourceLoader.getService(serviceName); 076 } 077 } 078 } 079 080 if (s != null) { 081 try { 082 return method.invoke(s, args); 083 } catch (InvocationTargetException e) { 084 throw ExceptionUtils.getRootCause(e); 085 } 086 } 087 088 LOG.warn("serviceName: " + serviceName + " was not found"); 089 return null; 090 } 091 }