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 void initialize() {
090                    ClusterLink clusterLink = getClusterLink();
091    
092                    if (clusterLink == null) {
093                            return;
094                    }
095    
096                    clusterLink.initialize();
097            }
098    
099            public static boolean isForwardMessage(Message message) {
100                    return message.getBoolean(ClusterLink.CLUSTER_FORWARD_MESSAGE);
101            }
102    
103            public static void sendMulticastMessage(
104                    Message message, Priority priority) {
105    
106                    ClusterLink clusterLink = getClusterLink();
107    
108                    if (clusterLink == null) {
109                            return;
110                    }
111    
112                    clusterLink.sendMulticastMessage(message, priority);
113            }
114    
115            public static void sendMulticastMessage(Object payload, Priority priority) {
116                    Message message = new Message();
117    
118                    message.setPayload(payload);
119    
120                    sendMulticastMessage(message, priority);
121            }
122    
123            public static void sendUnicastMessage(
124                    Address address, Message message, Priority priority) {
125    
126                    ClusterLink clusterLink = getClusterLink();
127    
128                    if (clusterLink == null) {
129                            return;
130                    }
131    
132                    clusterLink.sendUnicastMessage(address, message, priority);
133            }
134    
135            public static Message setAddress(Message message, Address address) {
136                    message.put(_ADDRESS, address);
137    
138                    return message;
139            }
140    
141            public static void setForwardMessage(Message message) {
142                    message.put(ClusterLink.CLUSTER_FORWARD_MESSAGE, true);
143            }
144    
145            public void setClusterLink(ClusterLink clusterLink) {
146                    PortalRuntimePermission.checkSetBeanProperty(getClass());
147    
148                    _clusterLink = clusterLink;
149            }
150    
151            private static final String _ADDRESS = "CLUSTER_ADDRESS";
152    
153            private static final Log _log = LogFactoryUtil.getLog(
154                    ClusterLinkUtil.class);
155    
156            private static ClusterLink _clusterLink;
157    
158    }