001    /**
002     * Copyright (c) 2000-2013 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.sender.MessageSender;
019    import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
020    
021    import java.util.Map;
022    
023    /**
024     * @author Michael C. Han
025     * @author Shuyang Zhou
026     */
027    public abstract class BaseMultiDestinationProxyBean {
028    
029            public abstract String getDestinationName(ProxyRequest proxyRequest);
030    
031            public void send(ProxyRequest proxyRequest) {
032                    _messageSender.send(
033                            getDestinationName(proxyRequest), buildMessage(proxyRequest));
034            }
035    
036            public void setMessageSender(MessageSender messageSender) {
037                    _messageSender = messageSender;
038            }
039    
040            public void setSynchronousMessageSender(
041                    SynchronousMessageSender synchronousMessageSender) {
042    
043                    _synchronousMessageSender = synchronousMessageSender;
044            }
045    
046            public Object synchronousSend(ProxyRequest proxyRequest) throws Exception {
047                    ProxyResponse proxyResponse =
048                            (ProxyResponse)_synchronousMessageSender.send(
049                                    getDestinationName(proxyRequest), buildMessage(proxyRequest));
050    
051                    if (proxyResponse == null) {
052                            return proxyRequest.execute(this);
053                    }
054                    else if (proxyResponse.hasError()) {
055                            throw proxyResponse.getException();
056                    }
057                    else {
058                            return proxyResponse.getResult();
059                    }
060            }
061    
062            protected Message buildMessage(ProxyRequest proxyRequest) {
063                    Message message = new Message();
064    
065                    message.setPayload(proxyRequest);
066    
067                    Map<String, Object> values = MessageValuesThreadLocal.getValues();
068    
069                    if (!values.isEmpty()) {
070                            for (String key : values.keySet()) {
071                                    message.put(key, values.get(key));
072                            }
073                    }
074    
075                    return message;
076            }
077    
078            private MessageSender _messageSender;
079            private SynchronousMessageSender _synchronousMessageSender;
080    
081    }