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