1
22
23 package com.liferay.portal.messaging.proxy;
24
25 import com.liferay.portal.kernel.messaging.proxy.BaseProxyBean;
26 import com.liferay.portal.kernel.messaging.proxy.ProxyRequest;
27 import com.liferay.portal.kernel.messaging.proxy.ProxyResponse;
28 import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
29 import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
30
31 import org.aspectj.lang.ProceedingJoinPoint;
32 import org.aspectj.lang.reflect.MethodSignature;
33
34
41 public class MessagingProxyAdvice {
42
43 public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
44 throws Throwable {
45
46 ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint);
47
48 BaseProxyBean baseProxyBean =
49 (BaseProxyBean)proceedingJoinPoint.getTarget();
50
51 if (proxyRequest.isSynchronous()) {
52 return doInvokeSynchronous(proxyRequest, baseProxyBean);
53 }
54 else {
55 doInvokeAsynchronous(proxyRequest, baseProxyBean);
56
57 return null;
58 }
59 }
60
61 protected ProxyRequest createProxyRequest(
62 ProceedingJoinPoint proceedingJoinPoint) throws Exception {
63
64 MethodSignature methodSignature =
65 (MethodSignature)proceedingJoinPoint.getSignature();
66 return new ProxyRequest(
67 methodSignature.getMethod(), proceedingJoinPoint.getArgs());
68 }
69
70 protected void doInvokeAsynchronous(
71 ProxyRequest proxyRequest, BaseProxyBean baseProxyBean) {
72
73 SingleDestinationMessageSender messageSender =
74 baseProxyBean.getSingleDestinationMessageSender();
75
76 if (messageSender == null) {
77 throw new IllegalStateException(
78 "Asynchronous message sender was not configured properly for " +
79 baseProxyBean.getClass().getName());
80 }
81
82 messageSender.send(proxyRequest);
83 }
84
85 protected Object doInvokeSynchronous(
86 ProxyRequest proxyRequest, BaseProxyBean baseProxyBean)
87 throws Exception {
88
89 SingleDestinationSynchronousMessageSender messageSender =
90 baseProxyBean.getSingleDestinationSynchronousMessageSender();
91
92 if (messageSender == null) {
93 throw new IllegalStateException(
94 "Synchronous message sender was not configured properly for " +
95 baseProxyBean.getClass().getName());
96 }
97
98 ProxyResponse proxyResponse = (ProxyResponse)messageSender.send(
99 proxyRequest);
100
101 if (proxyResponse.hasError()) {
102 throw proxyResponse.getException();
103 }
104 else {
105 return proxyResponse.getResult();
106 }
107 }
108
109 }