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