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 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 if (StringUtils.isBlank(serviceQName)) {
45 throw new IllegalArgumentException("the qname was blank");
46 }
47
48 if (interfaceClass == null) {
49 throw new IllegalArgumentException("the interfaceClass was null");
50 }
51
52 @SuppressWarnings("unchecked")
53 final T t = (T)Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class[] { interfaceClass }, new KSBClientProxy(serviceQName));
54 return t;
55 }
56
57 public KSBClientProxy(String serviceQName){
58 if (StringUtils.isBlank(serviceQName)) {
59 throw new IllegalArgumentException("the qname was blank");
60 }
61
62 this.serviceName = QName.valueOf(serviceQName);
63 }
64
65 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
66
67
68 Object s = service;
69 if (s == null) {
70 synchronized (this) {
71 s = service;
72 if (s == null) {
73 service = s = GlobalResourceLoader.getService(serviceName);
74 }
75 }
76 }
77
78 if (s != null) {
79 return method.invoke(s, args);
80 }
81
82 LOG.warn("serviceName: " + serviceName + " was not found");
83 return null;
84 }
85 }