001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }