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;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.Message;
020    
021    import java.util.Collections;
022    import java.util.List;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class ClusterLinkUtil {
028    
029            public static Address getAddress(Message message) {
030                    return (Address)message.get(_ADDRESS);
031            }
032    
033            public static ClusterLink getClusterLink() {
034                    if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
035                            if (_log.isWarnEnabled()) {
036                                    _log.warn("ClusterLinkUtil has not been initialized");
037                            }
038    
039                            return null;
040                    }
041    
042                    return _clusterLink;
043            }
044    
045            public static List<Address> getLocalTransportAddresses() {
046                    if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
047                            if (_log.isWarnEnabled()) {
048                                    _log.warn("ClusterLinkUtil has not been initialized");
049                            }
050    
051                            return Collections.EMPTY_LIST;
052                    }
053    
054                    return _clusterLink.getLocalTransportAddresses();
055            }
056    
057            public static List<Address> getTransportAddresses(Priority priority) {
058                    if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
059                            if (_log.isWarnEnabled()) {
060                                    _log.warn("ClusterLinkUtil has not been initialized");
061                            }
062    
063                            return Collections.EMPTY_LIST;
064                    }
065    
066                    return _clusterLink.getTransportAddresses(priority);
067            }
068    
069            public static boolean isForwardMessage(Message message) {
070                    return message.getBoolean(_CLUSTER_FORWARD_MESSAGE);
071            }
072    
073            public static void sendMulticastMessage(
074                    Message message, Priority priority) {
075    
076                    if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
077                            if (_log.isWarnEnabled()) {
078                                    _log.warn("ClusterLinkUtil has not been initialized");
079                            }
080    
081                            return;
082                    }
083    
084                    _clusterLink.sendMulticastMessage(message, priority);
085            }
086    
087            public static void sendMulticastMessage(
088                    Object payload, Priority priority) {
089    
090                    Message message = new Message();
091    
092                    message.setPayload(payload);
093    
094                    sendMulticastMessage(message, priority);
095            }
096    
097            public static void sendUnicastMessage(
098                    Address address, Message message, Priority priority) {
099    
100                    if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
101                            if (_log.isWarnEnabled()) {
102                                    _log.warn("ClusterLinkUtil has not been initialized");
103                            }
104    
105                            return;
106                    }
107    
108                    _clusterLink.sendUnicastMessage(address, message, priority);
109            }
110    
111            public static Message setAddress(Message message, Address address) {
112                    message.put(_ADDRESS, address);
113    
114                    return message;
115            }
116    
117            public static void setForwardMessage(Message message) {
118                    message.put(_CLUSTER_FORWARD_MESSAGE, true);
119            }
120    
121            public void setClusterLink(ClusterLink clusterLink) {
122                    _clusterLink = clusterLink;
123            }
124    
125            private static final String _ADDRESS = "CLUSTER_ADDRESS";
126    
127            private static final String _CLUSTER_FORWARD_MESSAGE =
128                    "CLUSTER_FORWARD_MESSAGE";
129    
130            private static Log _log = LogFactoryUtil.getLog(ClusterLinkUtil.class);
131    
132            private static ClusterLink _clusterLink;
133    
134    }