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