1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.messaging;
24  
25  import com.liferay.portal.kernel.messaging.sender.MessageSender;
26  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
27  
28  /**
29   * <a href="MessageBusUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Michael C. Han
32   *
33   */
34  public class MessageBusUtil {
35  
36      public static void addDestination(Destination destination) {
37          _instance._addDestination(destination);
38      }
39  
40      public static MessageBus getMessageBus() {
41          return _instance._messageBus;
42      }
43  
44      public static MessageSender getMessageSender() {
45          return _instance._messageSender;
46      }
47  
48      public static boolean hasMessageListener(String destination) {
49          return _instance._hasMessageListener(destination);
50      }
51  
52      public static void init(
53          MessageBus messageBus, MessageSender messageSender,
54          SynchronousMessageSender synchronousMessageSender) {
55  
56          _instance._init(messageBus, messageSender, synchronousMessageSender);
57      }
58  
59      public static void registerMessageListener(
60          String destination, MessageListener listener) {
61  
62          _instance._registerMessageListener(destination, listener);
63      }
64  
65      public static void removeDestination(String destination) {
66          _instance._removeDestination(destination);
67      }
68  
69      public static void sendMessage(String destination, Message message) {
70          _instance._sendMessage(destination, message);
71      }
72  
73      public static void sendMessage(String destination, Object payload) {
74          _instance._sendMessage(destination, payload);
75      }
76  
77      public static Object sendSynchronousMessage(
78              String destination, Message message)
79          throws MessageBusException {
80  
81          return _instance._sendSynchronousMessage(destination, message);
82      }
83  
84      public static Object sendSynchronousMessage(
85              String destination, Object payload)
86          throws MessageBusException {
87  
88          return _instance._sendSynchronousMessage(destination, payload);
89      }
90  
91      public static Object sendSynchronousMessage(
92              String destination, Message message, long timeout)
93          throws MessageBusException {
94  
95          return _instance._sendSynchronousMessage(
96              destination, message, timeout);
97      }
98  
99      public static Object sendSynchronousMessage(
100             String destination, Object payload, long timeout)
101         throws MessageBusException {
102 
103         return _instance._sendSynchronousMessage(
104             destination, payload, timeout);
105     }
106 
107     public static boolean unregisterMessageListener(
108         String destination, MessageListener listener) {
109 
110         return _instance._unregisterMessageListener(destination, listener);
111     }
112 
113     private MessageBusUtil() {
114     }
115 
116     private void _addDestination(Destination destination) {
117         _messageBus.addDestination(destination);
118     }
119 
120     private boolean _hasMessageListener(String destination) {
121         return _messageBus.hasMessageListener(destination);
122     }
123 
124     private void _init(
125         MessageBus messageBus, MessageSender messageSender,
126         SynchronousMessageSender synchronousMessageSender) {
127 
128         _messageBus = messageBus;
129         _messageSender = messageSender;
130         _synchronousMessageSender = synchronousMessageSender;
131     }
132 
133     private void _registerMessageListener(
134         String destination, MessageListener listener) {
135 
136         _messageBus.registerMessageListener(destination, listener);
137     }
138 
139     private void _removeDestination(String destination) {
140         _messageBus.removeDestination(destination);
141     }
142 
143     private void _sendMessage(String destination, Message message) {
144         _messageBus.sendMessage(destination, message);
145     }
146 
147     private void _sendMessage(String destination, Object payload) {
148         Message message = new Message();
149 
150         message.setPayload(payload);
151 
152         _sendMessage(destination, message);
153     }
154 
155     private Object _sendSynchronousMessage(String destination, Message message)
156         throws MessageBusException {
157 
158         return _synchronousMessageSender.sendMessage(destination, message);
159     }
160 
161     private Object _sendSynchronousMessage(String destination, Object payload)
162         throws MessageBusException {
163 
164         Message message = new Message();
165 
166         message.setPayload(payload);
167 
168         return _sendSynchronousMessage(destination, message);
169     }
170 
171     private Object _sendSynchronousMessage(
172             String destination, Message message, long timeout)
173         throws MessageBusException {
174 
175         return _synchronousMessageSender.sendMessage(
176             destination, message, timeout);
177     }
178 
179     private Object _sendSynchronousMessage(
180             String destination, Object payload, long timeout)
181         throws MessageBusException {
182 
183         Message message = new Message();
184 
185         message.setPayload(payload);
186 
187         return _sendSynchronousMessage(destination, message, timeout);
188     }
189 
190     private boolean _unregisterMessageListener(
191         String destination, MessageListener listener) {
192 
193         return _messageBus.unregisterMessageListener(destination, listener);
194     }
195 
196     private static MessageBusUtil _instance = new MessageBusUtil();
197 
198     private MessageBus _messageBus;
199     private MessageSender _messageSender;
200     private SynchronousMessageSender _synchronousMessageSender;
201 
202 }