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.cluster;
016    
017    import com.liferay.portal.kernel.cluster.ClusterLinkUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.messaging.MessageBusUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.List;
025    
026    import org.jgroups.Address;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class ClusterForwardReceiver extends BaseReceiver {
032    
033            public ClusterForwardReceiver(List<Address> localTransportAddresses) {
034                    _localTransportAddresses = localTransportAddresses;
035            }
036    
037            @Override
038            protected void doReceive(org.jgroups.Message jGroupsMessage) {
039                    if (!_localTransportAddresses.contains(jGroupsMessage.getSrc()) ||
040                            (jGroupsMessage.getDest() != null)) {
041    
042                            Message message = (Message)jGroupsMessage.getObject();
043    
044                            String destinationName = message.getDestinationName();
045    
046                            if (Validator.isNotNull(destinationName)) {
047                                    if (_log.isDebugEnabled()) {
048                                            _log.debug(
049                                                    "Forwarding cluster link message " + message + " to " +
050                                                            destinationName);
051                                    }
052    
053                                    ClusterLinkUtil.setForwardMessage(message);
054    
055                                    MessageBusUtil.sendMessage(destinationName, message);
056                            }
057                            else {
058                                    if (_log.isErrorEnabled()) {
059                                            _log.error(
060                                                    "Forwarded cluster link message has no destination " +
061                                                            message);
062                                    }
063                            }
064                    }
065                    else {
066                            if (_log.isDebugEnabled()) {
067                                    _log.debug("Block received message " + jGroupsMessage);
068                            }
069                    }
070            }
071    
072            private static final Log _log = LogFactoryUtil.getLog(
073                    ClusterForwardReceiver.class);
074    
075            private final List<org.jgroups.Address> _localTransportAddresses;
076    
077    }