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