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