1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.messaging.serviceproxies; |
17 | |
|
18 | |
import org.apache.log4j.Logger; |
19 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
20 | |
import org.kuali.rice.core.api.proxy.TargetedInvocationHandler; |
21 | |
import org.kuali.rice.core.impl.proxy.BaseInvocationHandler; |
22 | |
import org.kuali.rice.core.impl.resourceloader.ContextClassLoaderProxy; |
23 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
24 | |
import org.kuali.rice.ksb.messaging.*; |
25 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
26 | |
import org.kuali.rice.ksb.util.KSBConstants; |
27 | |
|
28 | |
import java.io.Serializable; |
29 | |
import java.lang.reflect.Method; |
30 | |
import java.lang.reflect.Proxy; |
31 | |
import java.util.List; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class AsynchronousServiceCallProxy extends BaseInvocationHandler implements TargetedInvocationHandler { |
42 | |
|
43 | 0 | private static final Logger LOG = Logger.getLogger(AsynchronousServiceCallProxy.class); |
44 | |
|
45 | |
private AsynchronousCallback callback; |
46 | |
|
47 | |
private List<RemotedServiceHolder> serviceDefs; |
48 | |
|
49 | |
private Serializable context; |
50 | |
|
51 | |
private String value1; |
52 | |
|
53 | |
private String value2; |
54 | |
|
55 | |
protected AsynchronousServiceCallProxy(List<RemotedServiceHolder> serviceDefs, AsynchronousCallback callback, |
56 | 0 | Serializable context, String value1, String value2) { |
57 | 0 | this.serviceDefs = serviceDefs; |
58 | 0 | this.callback = callback; |
59 | 0 | this.context = context; |
60 | 0 | this.value1 = value1; |
61 | 0 | this.value2 = value2; |
62 | 0 | } |
63 | |
|
64 | |
public static Object createInstance(List<RemotedServiceHolder> serviceDefs, AsynchronousCallback callback, |
65 | |
Serializable context, String value1, String value2) { |
66 | 0 | if (serviceDefs == null || serviceDefs.isEmpty()) { |
67 | 0 | throw new RuntimeException("Cannot create service proxy, no service(s) passed in."); |
68 | |
} |
69 | |
try { |
70 | 0 | return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy |
71 | |
.getInterfacesToProxy(serviceDefs.get(0).getService()), new AsynchronousServiceCallProxy( |
72 | |
serviceDefs, callback, context, value1, value2)); |
73 | 0 | } catch (Exception e) { |
74 | 0 | throw new RiceRuntimeException(e); |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
protected Object invokeInternal(Object proxy, Method method, Object[] arguments) throws Throwable { |
80 | |
|
81 | 0 | if (LOG.isDebugEnabled()) { |
82 | 0 | LOG.debug("creating messages for method invocation: " + method.getName()); |
83 | |
} |
84 | |
|
85 | 0 | AsynchronousCall methodCall = null; |
86 | 0 | PersistedMessageBO message = null; |
87 | 0 | synchronized (this) { |
88 | |
|
89 | |
|
90 | 0 | for (ServiceHolder remotedServiceHolder : this.serviceDefs) { |
91 | 0 | ServiceInfo serviceInfo = remotedServiceHolder.getServiceInfo(); |
92 | 0 | methodCall = new AsynchronousCall(method.getParameterTypes(), arguments, serviceInfo, method.getName(), |
93 | |
this.callback, this.context); |
94 | 0 | message = KSBServiceLocator.getRouteQueueService().getMessage(serviceInfo, methodCall); |
95 | 0 | message.setValue1(this.value1); |
96 | 0 | message.setValue2(this.value2); |
97 | 0 | saveMessage(message); |
98 | 0 | executeMessage(message); |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | 0 | if (serviceInfo.getServiceDefinition().getQueue()) { |
104 | 0 | break; |
105 | |
} |
106 | 0 | } |
107 | 0 | } |
108 | 0 | if (LOG.isDebugEnabled()) { |
109 | 0 | LOG.debug("finished creating messages for method invocation: " + method.getName()); |
110 | |
} |
111 | 0 | return null; |
112 | |
} |
113 | |
|
114 | |
protected void saveMessage(PersistedMessageBO message) { |
115 | 0 | message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING); |
116 | 0 | KSBServiceLocator.getRouteQueueService().save(message); |
117 | 0 | } |
118 | |
|
119 | |
protected void executeMessage(PersistedMessageBO message) throws Exception { |
120 | 0 | MessageSender.sendMessage(message); |
121 | 0 | } |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public Object getTarget() { |
128 | 0 | return this.serviceDefs; |
129 | |
} |
130 | |
|
131 | |
public AsynchronousCallback getCallback() { |
132 | 0 | return this.callback; |
133 | |
} |
134 | |
|
135 | |
public void setCallback(AsynchronousCallback callback) { |
136 | 0 | this.callback = callback; |
137 | 0 | } |
138 | |
|
139 | |
public List<RemotedServiceHolder> getServiceDefs() { |
140 | 0 | return this.serviceDefs; |
141 | |
} |
142 | |
|
143 | |
public void setServiceDefs(List<RemotedServiceHolder> serviceDefs) { |
144 | 0 | this.serviceDefs = serviceDefs; |
145 | 0 | } |
146 | |
} |