1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.spring;
17
18 import java.io.Externalizable;
19 import java.io.IOException;
20 import java.io.ObjectInput;
21 import java.io.ObjectOutput;
22 import java.io.Serializable;
23 import java.lang.reflect.InvocationHandler;
24 import java.lang.reflect.Method;
25
26 import javax.xml.namespace.QName;
27
28 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
29 import org.springframework.util.ReflectionUtils;
30
31
32
33
34
35
36
37
38 public class SerializableProxyInvokationHandler implements InvocationHandler, Externalizable {
39
40
41
42
43 private static final long serialVersionUID = 1L;
44
45 private transient Object serviceDelegate;
46
47 private QName serviceName;
48
49
50
51
52 public SerializableProxyInvokationHandler() {
53 }
54
55
56
57
58
59 public void setServiceName(QName serviceName) {
60 this.serviceName = serviceName;
61 }
62
63
64
65
66
67
68
69
70 @Override
71 public Object invoke(Object proxy, Method method, Object[] args)
72 throws Throwable {
73
74 if (serviceDelegate == null) {
75
76 try {
77 serviceDelegate = GlobalResourceLoader.getService(serviceName);
78 } catch (Exception e) {
79
80 if (method.getName().equals("toString")) {
81
82
83
84 return getClass().getName() + " (serviceName=" + serviceName + ")";
85 }
86 else
87 throw e;
88 }
89 }
90
91
92 return ReflectionUtils.invokeMethod(method, serviceDelegate, args);
93 }
94
95
96
97
98 @Override
99 public void writeExternal(ObjectOutput out) throws IOException {
100
101 QName name = new QName(serviceName.getNamespaceURI(), serviceName.getLocalPart());
102
103 out.writeObject(name);
104
105 }
106
107
108
109
110 @Override
111 public void readExternal(ObjectInput in) throws IOException,
112 ClassNotFoundException {
113
114 QName name = (QName) in.readObject();
115
116 this.serviceName = new QName (name.getNamespaceURI(), name.getLocalPart());
117
118 }
119
120
121
122 }