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