001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.MessageBus;
019    import com.liferay.portal.kernel.messaging.MessageBusUtil;
020    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSenderFactoryUtil;
021    import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
022    
023    /**
024     * @author Michael C. Han
025     * @author Shuyang Zhou
026     */
027    public abstract class BaseMultiDestinationProxyBean {
028    
029            public void afterPropertiesSet() {
030                    _synchronousMessageSender =
031                            SingleDestinationMessageSenderFactoryUtil.
032                                    getSynchronousMessageSender(_mode);
033            }
034    
035            public abstract String getDestinationName(ProxyRequest proxyRequest);
036    
037            public void send(ProxyRequest proxyRequest) {
038                    MessageBusUtil.sendMessage(
039                            getDestinationName(proxyRequest), buildMessage(proxyRequest));
040            }
041    
042            /**
043             * @deprecated As of 7.0.0, replaced by {@link MessageBusUtil#getMessageBus)
044             */
045            @Deprecated
046            public void setMessageBus(MessageBus messageBus) {
047            }
048    
049            /**
050             * @deprecated As of 7.0.0, replaced by {@link
051             *             #setSynchronousMessageSenderMode(
052             *             SynchronousMessageSender.Mode)}
053             */
054            @Deprecated
055            public void setSynchronousMessageSender(
056                    SynchronousMessageSender synchronousMessageSender) {
057            }
058    
059            public void setSynchronousMessageSenderMode(
060                    SynchronousMessageSender.Mode mode) {
061    
062                    _mode = mode;
063            }
064    
065            public Object synchronousSend(ProxyRequest proxyRequest) throws Exception {
066                    ProxyResponse proxyResponse =
067                            (ProxyResponse)_synchronousMessageSender.send(
068                                    getDestinationName(proxyRequest), buildMessage(proxyRequest));
069    
070                    if (proxyResponse == null) {
071                            return proxyRequest.execute(this);
072                    }
073                    else if (proxyResponse.hasError()) {
074                            throw proxyResponse.getException();
075                    }
076                    else {
077                            return proxyResponse.getResult();
078                    }
079            }
080    
081            protected Message buildMessage(ProxyRequest proxyRequest) {
082                    Message message = new Message();
083    
084                    message.setPayload(proxyRequest);
085    
086                    MessageValuesThreadLocal.populateMessageFromThreadLocals(message);
087    
088                    return message;
089            }
090    
091            private SynchronousMessageSender.Mode _mode;
092            private SynchronousMessageSender _synchronousMessageSender;
093    
094    }