001
014
015 package com.liferay.portal.kernel.messaging.proxy;
016
017 import com.liferay.portal.kernel.messaging.Message;
018 import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSender;
019 import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
020
021 import java.util.Map;
022
023
029 public abstract class BaseProxyBean {
030
031 public void send(ProxyRequest proxyRequest) {
032 _singleDestinationMessageSender.send(buildMessage(proxyRequest));
033 }
034
035 public void setSingleDestinationMessageSender(
036 SingleDestinationMessageSender singleDestinationMessageSender) {
037
038 _singleDestinationMessageSender = singleDestinationMessageSender;
039 }
040
041 public void setSingleDestinationSynchronousMessageSender(
042 SingleDestinationSynchronousMessageSender
043 singleDestinationSynchronousMessageSender) {
044
045 _singleDestinationSynchronousMessageSender =
046 singleDestinationSynchronousMessageSender;
047 }
048
049 public Object synchronousSend(ProxyRequest proxyRequest) throws Exception {
050 ProxyResponse proxyResponse =
051 (ProxyResponse)_singleDestinationSynchronousMessageSender.send(
052 buildMessage(proxyRequest));
053
054 if (proxyResponse == null) {
055 return proxyRequest.execute(this);
056 }
057 else if (proxyResponse.hasError()) {
058 throw proxyResponse.getException();
059 }
060 else {
061 return proxyResponse.getResult();
062 }
063 }
064
065 protected Message buildMessage(ProxyRequest proxyRequest) {
066 Message message = new Message();
067
068 message.setPayload(proxyRequest);
069
070 Map<String, Object> values = MessageValuesThreadLocal.getValues();
071
072 if (!values.isEmpty()) {
073 for (String key : values.keySet()) {
074 message.put(key, values.get(key));
075 }
076 }
077
078 if (proxyRequest.isLocal()) {
079 message.put(MessagingProxy.LOCAL_MESSAGE, Boolean.TRUE);
080 }
081
082 return message;
083 }
084
085 private SingleDestinationMessageSender _singleDestinationMessageSender;
086 private SingleDestinationSynchronousMessageSender
087 _singleDestinationSynchronousMessageSender;
088
089 }