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.cluster.messaging;
016    
017    import com.liferay.portal.kernel.cluster.Address;
018    import com.liferay.portal.kernel.cluster.ClusterLinkUtil;
019    import com.liferay.portal.kernel.cluster.Priority;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.messaging.BaseMessageListener;
023    import com.liferay.portal.kernel.messaging.Message;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class ClusterBridgeMessageListener extends BaseMessageListener {
029    
030            public void setActive(boolean active) {
031                    _active = active;
032            }
033    
034            public void setPriority(Priority priority) {
035                    _priority = priority;
036            }
037    
038            @Override
039            protected void doReceive(Message message) throws Exception {
040                    if (!_active) {
041                            return;
042                    }
043    
044                    if (ClusterLinkUtil.isForwardMessage(message)) {
045                            return;
046                    }
047    
048                    Address address = ClusterLinkUtil.getAddress(message);
049    
050                    if (address == null) {
051                            if (_log.isInfoEnabled()) {
052                                    _log.info("Bridging cluster link multicast message " + message);
053                            }
054    
055                            ClusterLinkUtil.sendMulticastMessage(message, _priority);
056                    }
057                    else {
058                            if (_log.isInfoEnabled()) {
059                                    _log.info(
060                                            "Bridging cluster link unicast message " + message +
061                                                    " to " + address);
062                            }
063    
064                            ClusterLinkUtil.sendUnicastMessage(address, message, _priority);
065                    }
066            }
067    
068            private static Log _log = LogFactoryUtil.getLog(
069                    ClusterBridgeMessageListener.class);
070    
071            private boolean _active = true;
072            private Priority _priority;
073    
074    }