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