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.SingleDestinationMessageSender;
019    import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
020    
021    import java.util.Map;
022    
023    /**
024     * @author Micha Kiener
025     * @author Michael C. Han
026     * @author Brian Wing Shun Chan
027     * @author Shuyang Zhou
028     */
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    }