1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.messaging;
21  
22  import com.liferay.portal.kernel.messaging.sender.MessageSender;
23  import com.liferay.portal.kernel.messaging.sender.SynchronousMessageSender;
24  
25  /**
26   * <a href="MessageBusUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Michael C. Han
29   *
30   */
31  public class MessageBusUtil {
32  
33      public static void addDestination(Destination destination) {
34          _instance._addDestination(destination);
35      }
36  
37      public static Message createResponseMessage(Message requestMessage) {
38  
39          Message response = new Message();
40          response.setDestination(requestMessage.getResponseDestination());
41          response.setResponseId(requestMessage.getResponseId());
42  
43          return response;
44      }
45  
46      public static Message createResponseMessage(
47          Message requestMessage, Object payload) {
48  
49          Message response = createResponseMessage(requestMessage);
50          response.setPayload(payload);
51  
52          return response;
53      }
54  
55      public static MessageBus getMessageBus() {
56          return _instance._messageBus;
57      }
58  
59      public static MessageSender getMessageSender() {
60          return _instance._messageSender;
61      }
62  
63      public static boolean hasMessageListener(String destination) {
64          return _instance._hasMessageListener(destination);
65      }
66  
67      public static void init(
68          MessageBus messageBus, MessageSender messageSender,
69          SynchronousMessageSender synchronousMessageSender) {
70  
71          _instance._init(messageBus, messageSender, synchronousMessageSender);
72      }
73  
74      public static void registerMessageListener(
75          String destination, MessageListener listener) {
76  
77          _instance._registerMessageListener(destination, listener);
78      }
79  
80      public static void removeDestination(String destination) {
81          _instance._removeDestination(destination);
82      }
83  
84      public static void sendMessage(String destination, Message message) {
85          _instance._sendMessage(destination, message);
86      }
87  
88      public static void sendMessage(String destination, Object payload) {
89          _instance._sendMessage(destination, payload);
90      }
91  
92      public static Object sendSynchronousMessage(
93              String destination, Message message)
94          throws MessageBusException {
95  
96          return _instance._sendSynchronousMessage(destination, message);
97      }
98  
99      public static Object sendSynchronousMessage(
100             String destination, Message message, long timeout)
101         throws MessageBusException {
102 
103         return _instance._sendSynchronousMessage(destination, message, timeout);
104     }
105 
106     public static Object sendSynchronousMessage(
107             String destination, Object payload)
108         throws MessageBusException {
109 
110         return _instance._sendSynchronousMessage(destination, payload, null);
111     }
112 
113     public static Object sendSynchronousMessage(
114             String destination, Object payload, long timeout)
115         throws MessageBusException {
116 
117         return _instance._sendSynchronousMessage(
118             destination, payload, null, timeout);
119     }
120 
121     public static Object sendSynchronousMessage(
122             String destination, Object payload, String responseDestination)
123         throws MessageBusException {
124 
125         return _instance._sendSynchronousMessage(
126             destination, payload, responseDestination);
127     }
128 
129     public static Object sendSynchronousMessage(
130             String destination, Object payload, String responseDestination,
131             long timeout)
132         throws MessageBusException {
133 
134         return _instance._sendSynchronousMessage(
135             destination, payload, responseDestination, timeout);
136     }
137 
138     public static boolean unregisterMessageListener(
139         String destination, MessageListener listener) {
140 
141         return _instance._unregisterMessageListener(destination, listener);
142     }
143 
144     private MessageBusUtil() {
145     }
146 
147     private void _addDestination(Destination destination) {
148         _messageBus.addDestination(destination);
149     }
150 
151     private boolean _hasMessageListener(String destination) {
152         return _messageBus.hasMessageListener(destination);
153     }
154 
155     private void _init(
156         MessageBus messageBus, MessageSender messageSender,
157         SynchronousMessageSender synchronousMessageSender) {
158 
159         _messageBus = messageBus;
160         _messageSender = messageSender;
161         _synchronousMessageSender = synchronousMessageSender;
162     }
163 
164     private void _registerMessageListener(
165         String destination, MessageListener listener) {
166 
167         _messageBus.registerMessageListener(destination, listener);
168     }
169 
170     private void _removeDestination(String destination) {
171         _messageBus.removeDestination(destination);
172     }
173 
174     private void _sendMessage(String destination, Message message) {
175         _messageBus.sendMessage(destination, message);
176     }
177 
178     private void _sendMessage(String destination, Object payload) {
179         Message message = new Message();
180 
181         message.setPayload(payload);
182 
183         _sendMessage(destination, message);
184     }
185 
186     private Object _sendSynchronousMessage(String destination, Message message)
187         throws MessageBusException {
188 
189         return _synchronousMessageSender.sendMessage(destination, message);
190     }
191 
192     private Object _sendSynchronousMessage(
193             String destination, Message message, long timeout)
194         throws MessageBusException {
195 
196         return _synchronousMessageSender.sendMessage(
197             destination, message, timeout);
198     }
199 
200     private Object _sendSynchronousMessage(
201             String destination, Object payload, String responseDestination)
202         throws MessageBusException {
203 
204         Message message = new Message();
205 
206         message.setResponseDestination(responseDestination);
207         message.setPayload(payload);
208 
209         return _sendSynchronousMessage(destination, message);
210     }
211 
212     private Object _sendSynchronousMessage(
213             String destination, Object payload, String responseDestination,
214             long timeout)
215         throws MessageBusException {
216 
217         Message message = new Message();
218 
219         message.setResponseDestination(responseDestination);
220         message.setPayload(payload);
221 
222         return _sendSynchronousMessage(destination, message, timeout);
223     }
224 
225     private boolean _unregisterMessageListener(
226         String destination, MessageListener listener) {
227 
228         return _messageBus.unregisterMessageListener(destination, listener);
229     }
230 
231     private static MessageBusUtil _instance = new MessageBusUtil();
232 
233     private MessageBus _messageBus;
234     private MessageSender _messageSender;
235     private SynchronousMessageSender _synchronousMessageSender;
236 
237 }