1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.cluster;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.messaging.Message;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  /**
25   * <a href="ClusterLinkUtil.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Shuyang Zhou
28   */
29  public class ClusterLinkUtil {
30  
31      public static Address getAddress(Message message) {
32          return (Address)message.get(_ADDRESS);
33      }
34  
35      public static ClusterLink getClusterLink() {
36          if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
37              if (_log.isWarnEnabled()) {
38                  _log.warn("ClusterLinkUtil has not been initialized");
39              }
40  
41              return null;
42          }
43  
44          return _clusterLink;
45      }
46  
47      public static List<Address> getLocalTransportAddresses() {
48          if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
49              if (_log.isWarnEnabled()) {
50                  _log.warn("ClusterLinkUtil has not been initialized");
51              }
52  
53              return Collections.EMPTY_LIST;
54          }
55  
56          return _clusterLink.getLocalTransportAddresses();
57      }
58  
59      public static List<Address> getTransportAddresses(Priority priority) {
60          if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
61              if (_log.isWarnEnabled()) {
62                  _log.warn("ClusterLinkUtil has not been initialized");
63              }
64  
65              return Collections.EMPTY_LIST;
66          }
67  
68          return _clusterLink.getTransportAddresses(priority);
69      }
70  
71      public static boolean isForwardMessage(Message message) {
72          return message.getBoolean(_CLUSTER_FORWARD_MESSAGE);
73      }
74  
75      public static void sendMulticastMessage(
76          Message message, Priority priority) {
77  
78          if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
79              if (_log.isWarnEnabled()) {
80                  _log.warn("ClusterLinkUtil has not been initialized");
81              }
82  
83              return;
84          }
85  
86          _clusterLink.sendMulticastMessage(message, priority);
87      }
88  
89      public static void sendMulticastMessage(
90          Object payload, Priority priority) {
91  
92          Message message = new Message();
93  
94          message.setPayload(payload);
95  
96          sendMulticastMessage(message, priority);
97      }
98  
99      public static void sendUnicastMessage(
100         Address address, Message message, Priority priority) {
101 
102         if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
103             if (_log.isWarnEnabled()) {
104                 _log.warn("ClusterLinkUtil has not been initialized");
105             }
106 
107             return;
108         }
109 
110         _clusterLink.sendUnicastMessage(address, message, priority);
111     }
112 
113     public static Message setAddress(Message message, Address address) {
114         message.put(_ADDRESS, address);
115 
116         return message;
117     }
118 
119     public static void setForwardMessage(Message message) {
120         message.put(_CLUSTER_FORWARD_MESSAGE, true);
121     }
122 
123     public void setClusterLink(ClusterLink clusterLink) {
124         _clusterLink = clusterLink;
125     }
126 
127     private static final String _ADDRESS = "CLUSTER_ADDRESS";
128 
129     private static final String _CLUSTER_FORWARD_MESSAGE =
130         "CLUSTER_FORWARD_MESSAGE";
131 
132     private static Log _log = LogFactoryUtil.getLog(ClusterLinkUtil.class);
133 
134     private static ClusterLink _clusterLink;
135 
136 }