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