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> getControlAddresses() {
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.getControlAddresses();
57      }
58  
59      public static Address getLocalControlAddress() {
60          if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
61              if (_log.isWarnEnabled()) {
62                  _log.warn("ClusterLinkUtil has not been initialized");
63              }
64  
65              return null;
66          }
67  
68          return _clusterLink.getLocalControlAddress();
69      }
70  
71      public static List<Address> getLocalTransportAddresses() {
72          if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
73              if (_log.isWarnEnabled()) {
74                  _log.warn("ClusterLinkUtil has not been initialized");
75              }
76  
77              return Collections.EMPTY_LIST;
78          }
79  
80          return _clusterLink.getLocalTransportAddresses();
81      }
82  
83      public static List<Address> getTransportAddresses(Priority priority) {
84          if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
85              if (_log.isWarnEnabled()) {
86                  _log.warn("ClusterLinkUtil has not been initialized");
87              }
88  
89              return Collections.EMPTY_LIST;
90          }
91  
92          return _clusterLink.getTransportAddresses(priority);
93      }
94  
95      public static boolean isForwardMessage(Message message) {
96          return message.getBoolean(_CLUSTER_FORWARD_MESSAGE);
97      }
98  
99      public static void sendMulticastMessage(
100         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.sendMulticastMessage(message, priority);
111     }
112 
113     public static void sendMulticastMessage(
114         Object payload, Priority priority) {
115 
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         if ((_clusterLink == null) || !_clusterLink.isEnabled()) {
127             if (_log.isWarnEnabled()) {
128                 _log.warn("ClusterLinkUtil has not been initialized");
129             }
130 
131             return;
132         }
133 
134         _clusterLink.sendUnicastMessage(address, message, priority);
135     }
136 
137     public static Message setAddress(Message message, Address address) {
138         message.put(_ADDRESS, address);
139 
140         return message;
141     }
142 
143     public static void setForwardMessage(Message message) {
144         message.put(_CLUSTER_FORWARD_MESSAGE, true);
145     }
146 
147     public void setClusterLink(ClusterLink clusterLink) {
148         _clusterLink = clusterLink;
149     }
150 
151     private static final String _ADDRESS = "CLUSTER_ADDRESS";
152 
153     private static final String _CLUSTER_FORWARD_MESSAGE =
154         "CLUSTER_FORWARD_MESSAGE";
155 
156     private static Log _log = LogFactoryUtil.getLog(ClusterLinkUtil.class);
157 
158     private static ClusterLink _clusterLink;
159 
160 }