001
014
015 package com.liferay.portal.messaging.proxy;
016
017 import com.liferay.portal.kernel.messaging.Message;
018 import com.liferay.portal.kernel.messaging.proxy.BaseProxyBean;
019 import com.liferay.portal.kernel.messaging.proxy.ProxyRequest;
020 import com.liferay.portal.kernel.messaging.proxy.ProxyResponse;
021 import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
022 import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
023 import com.liferay.util.aspectj.AspectJUtil;
024
025 import java.util.Map;
026
027 import org.aspectj.lang.ProceedingJoinPoint;
028
029
034 public class MessagingProxyAdvice {
035
036 public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
037 throws Throwable {
038
039 Message message = new Message();
040
041 ProxyRequest proxyRequest = createProxyRequest(proceedingJoinPoint);
042
043 message.setPayload(proxyRequest);
044
045 Map<String, Object> messageValues =
046 MessageValuesThreadLocal.getValues();
047
048 if (!messageValues.isEmpty()) {
049 for (String key : messageValues.keySet()) {
050 message.put(key, messageValues.get(key));
051 }
052 }
053
054 BaseProxyBean baseProxyBean =
055 (BaseProxyBean)proceedingJoinPoint.getTarget();
056
057 if (proxyRequest.isSynchronous() ||
058 ProxyModeThreadLocal.isForceSync()) {
059
060 return doInvokeSynchronous(message, baseProxyBean);
061 }
062 else {
063 doInvokeAsynchronous(message, baseProxyBean);
064
065 return null;
066 }
067 }
068
069 protected ProxyRequest createProxyRequest(
070 ProceedingJoinPoint proceedingJoinPoint)
071 throws Exception {
072
073 return new ProxyRequest(
074 AspectJUtil.getMethod(proceedingJoinPoint),
075 proceedingJoinPoint.getArgs());
076 }
077
078 protected void doInvokeAsynchronous(
079 Message message, BaseProxyBean baseProxyBean) {
080
081 SingleDestinationMessageSender messageSender =
082 baseProxyBean.getSingleDestinationMessageSender();
083
084 if (messageSender == null) {
085 throw new IllegalStateException(
086 "Asynchronous message sender was not configured properly for " +
087 baseProxyBean.getClass().getName());
088 }
089
090 messageSender.send(message);
091 }
092
093 protected Object doInvokeSynchronous(
094 Message message, BaseProxyBean baseProxyBean)
095 throws Exception {
096
097 SingleDestinationSynchronousMessageSender messageSender =
098 baseProxyBean.getSingleDestinationSynchronousMessageSender();
099
100 if (messageSender == null) {
101 throw new IllegalStateException(
102 "Synchronous message sender was not configured properly for " +
103 baseProxyBean.getClass().getName());
104 }
105
106 ProxyResponse proxyResponse = (ProxyResponse)messageSender.send(
107 message);
108
109 if (proxyResponse == null) {
110 return null;
111 }
112 else if (proxyResponse.hasError()) {
113 throw proxyResponse.getException();
114 }
115 else {
116 return proxyResponse.getResult();
117 }
118 }
119
120 }