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.sender.SingleDestinationMessageSender;
019    import com.liferay.portal.kernel.messaging.sender.SingleDestinationMessageSenderFactoryUtil;
020    import com.liferay.portal.kernel.messaging.sender.SingleDestinationSynchronousMessageSender;
021    import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    /**
025     * @author Micha Kiener
026     * @author Michael C. Han
027     * @author Brian Wing Shun Chan
028     * @author Shuyang Zhou
029     */
030    public abstract class BaseProxyBean {
031    
032            public void afterPropertiesSet() {
033                    if ((_singleDestinationSynchronousMessageSender == null) &&
034                            Validator.isNotNull(_synchronousDestinationName)) {
035    
036                            _singleDestinationSynchronousMessageSender =
037                                    SingleDestinationMessageSenderFactoryUtil.
038                                            createSingleDestinationSynchronousMessageSender(
039                                                    _synchronousDestinationName,
040                                                    _synchronousMessageSenderMode);
041                    }
042    
043                    if ((_singleDestinationMessageSender == null) &&
044                            Validator.isNotNull(_destinationName)) {
045    
046                            _singleDestinationMessageSender =
047                                    SingleDestinationMessageSenderFactoryUtil.
048                                            createSingleDestinationMessageSender(_destinationName);
049                    }
050            }
051    
052            public void send(ProxyRequest proxyRequest) {
053                    _singleDestinationMessageSender.send(buildMessage(proxyRequest));
054            }
055    
056            public void setDestinationName(String destinationName) {
057                    _destinationName = destinationName;
058            }
059    
060            /**
061             * @deprecated As of 7.0.0, replaced by {@link #setDestinationName)
062             */
063            @Deprecated
064            public void setSingleDestinationMessageSender(
065                    SingleDestinationMessageSender singleDestinationMessageSender) {
066    
067                    _singleDestinationMessageSender = singleDestinationMessageSender;
068            }
069    
070            /**
071             * @deprecated As of 7.0.0, replaced by {@link
072             *             #setSynchronousMessageSenderMode} and {@link
073             *             #setSynchronousDestinationName}
074             */
075            @Deprecated
076            public void setSingleDestinationSynchronousMessageSender(
077                    SingleDestinationSynchronousMessageSender
078                            singleDestinationSynchronousMessageSender) {
079    
080                    _singleDestinationSynchronousMessageSender =
081                            singleDestinationSynchronousMessageSender;
082            }
083    
084            public void setSynchronousDestinationName(
085                    String synchronousDestinationName) {
086    
087                    _synchronousDestinationName = synchronousDestinationName;
088            }
089    
090            public void setSynchronousMessageSenderMode(
091                    SynchronousMessageSender.Mode synchronousMessageSenderMode) {
092    
093                    _synchronousMessageSenderMode = synchronousMessageSenderMode;
094            }
095    
096            public Object synchronousSend(ProxyRequest proxyRequest) throws Exception {
097                    ProxyResponse proxyResponse =
098                            (ProxyResponse)_singleDestinationSynchronousMessageSender.send(
099                                    buildMessage(proxyRequest));
100    
101                    if (proxyResponse == null) {
102                            return proxyRequest.execute(this);
103                    }
104                    else if (proxyResponse.hasError()) {
105                            throw proxyResponse.getException();
106                    }
107                    else {
108                            return proxyResponse.getResult();
109                    }
110            }
111    
112            protected Message buildMessage(ProxyRequest proxyRequest) {
113                    Message message = new Message();
114    
115                    message.setPayload(proxyRequest);
116    
117                    MessageValuesThreadLocal.populateMessageFromThreadLocals(message);
118    
119                    if (proxyRequest.isLocal()) {
120                            message.put(MessagingProxy.LOCAL_MESSAGE, Boolean.TRUE);
121                    }
122    
123                    return message;
124            }
125    
126            private String _destinationName;
127            private SingleDestinationMessageSender _singleDestinationMessageSender;
128            private SingleDestinationSynchronousMessageSender
129                    _singleDestinationSynchronousMessageSender;
130            private String _synchronousDestinationName;
131            private SynchronousMessageSender.Mode _synchronousMessageSenderMode;
132    
133    }