| 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 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 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 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 | |
|
| 67 | |
|
| 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 | |
} |