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