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
051             * {@link #setSynchronousMessageSenderMode(SynchronousMessageSender.Mode)}
052             */
053            @Deprecated
054            public void setSynchronousMessageSender(
055                    SynchronousMessageSender synchronousMessageSender) {
056            }
057    
058            public void setSynchronousMessageSenderMode(
059                    SynchronousMessageSender.Mode mode) {
060    
061                    _mode = mode;
062            }
063    
064            public Object synchronousSend(ProxyRequest proxyRequest) throws Exception {
065                    ProxyResponse proxyResponse =
066                            (ProxyResponse)_synchronousMessageSender.send(
067                                    getDestinationName(proxyRequest), buildMessage(proxyRequest));
068    
069                    if (proxyResponse == null) {
070                            return proxyRequest.execute(this);
071                    }
072                    else if (proxyResponse.hasError()) {
073                            throw proxyResponse.getException();
074                    }
075                    else {
076                            return proxyResponse.getResult();
077                    }
078            }
079    
080            protected Message buildMessage(ProxyRequest proxyRequest) {
081                    Message message = new Message();
082    
083                    message.setPayload(proxyRequest);
084    
085                    MessageValuesThreadLocal.populateMessageFromThreadLocals(message);
086    
087                    return message;
088            }
089    
090            private SynchronousMessageSender.Mode _mode;
091            private SynchronousMessageSender _synchronousMessageSender;
092    
093    }