001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }