001 /** 002 * Copyright 2010 The Kuali Foundation Licensed under the 003 * Educational Community License, Version 2.0 (the "License"); you may 004 * not use this file except in compliance with the License. You may 005 * obtain a copy of the License at 006 * 007 * http://www.osedu.org/licenses/ECL-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, 010 * software distributed under the License is distributed on an "AS IS" 011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 012 * or implied. See the License for the specific language governing 013 * permissions and limitations under the License. 014 */ 015 016 package org.kuali.rice.ksb.messaging; 017 018 import java.lang.reflect.InvocationHandler; 019 import java.lang.reflect.InvocationTargetException; 020 import java.lang.reflect.Method; 021 import java.lang.reflect.Proxy; 022 023 import javax.xml.namespace.QName; 024 025 import org.apache.log4j.Logger; 026 import org.kuali.rice.core.resourceloader.GlobalResourceLoader; 027 import org.kuali.rice.core.util.ExceptionUtils; 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 QName serviceName; 042 Object service; 043 044 @SuppressWarnings("unchecked") 045 public static <T> T newInstance(String serviceQName, Class<T> interfaceClass) throws InstantiationException, IllegalAccessException { 046 return (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] { interfaceClass }, new KSBClientProxy(serviceQName)); 047 } 048 049 public KSBClientProxy(String serviceQName){ 050 this.serviceName = QName.valueOf(serviceQName); 051 } 052 053 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 054 if (this.service == null){ 055 LOG.info("Getting service using GRL for: " + serviceName); 056 service = GlobalResourceLoader.getService(serviceName); 057 LOG.info("Obtained service using GRL for: " + serviceName); 058 } 059 060 try { 061 return method.invoke(service, args); 062 } catch (InvocationTargetException e) { 063 throw ExceptionUtils.unwrapActualCause(e); 064 } 065 066 } 067 068 }