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