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.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  public class MessageBusUtil {
34  
35      public static void addDestination(Destination destination) {
36          _instance._addDestination(destination);
37      }
38  
39      public static Message createResponseMessage(Message requestMessage) {
40          Message responseMessage = new Message();
41  
42          responseMessage.setDestinationName(
43              requestMessage.getResponseDestinationName());
44          responseMessage.setResponseId(requestMessage.getResponseId());
45  
46          return responseMessage;
47      }
48  
49      public static Message createResponseMessage(
50          Message requestMessage, Object payload) {
51  
52          Message responseMessage = createResponseMessage(requestMessage);
53  
54          responseMessage.setPayload(payload);
55  
56          return responseMessage;
57      }
58  
59      public static MessageBus getMessageBus() {
60          return _instance._messageBus;
61      }
62  
63      public static MessageSender getMessageSender() {
64          return _instance._messageSender;
65      }
66  
67      public static boolean hasMessageListener(String destination) {
68          return _instance._hasMessageListener(destination);
69      }
70  
71      public static void init(
72          MessageBus messageBus, MessageSender messageSender,
73          SynchronousMessageSender synchronousMessageSender) {
74  
75          _instance._init(messageBus, messageSender, synchronousMessageSender);
76      }
77  
78      public static void registerMessageListener(
79          String destinationName, MessageListener messageListener) {
80  
81          _instance._registerMessageListener(destinationName, messageListener);
82      }
83  
84      public static void removeDestination(String destinationName) {
85          _instance._removeDestination(destinationName);
86      }
87  
88      public static void sendMessage(String destinationName, Message message) {
89          _instance._sendMessage(destinationName, message);
90      }
91  
92      public static void sendMessage(String destinationName, Object payload) {
93          _instance._sendMessage(destinationName, payload);
94      }
95  
96      public static Object sendSynchronousMessage(
97              String destinationName, Message message)
98          throws MessageBusException {
99  
100         return _instance._sendSynchronousMessage(destinationName, message);
101     }
102 
103     public static Object sendSynchronousMessage(
104             String destinationName, Message message, long timeout)
105         throws MessageBusException {
106 
107         return _instance._sendSynchronousMessage(
108             destinationName, message, timeout);
109     }
110 
111     public static Object sendSynchronousMessage(
112             String destinationName, Object payload)
113         throws MessageBusException {
114 
115         return _instance._sendSynchronousMessage(
116             destinationName, payload, null);
117     }
118 
119     public static Object sendSynchronousMessage(
120             String destinationName, Object payload, long timeout)
121         throws MessageBusException {
122 
123         return _instance._sendSynchronousMessage(
124             destinationName, payload, null, timeout);
125     }
126 
127     public static Object sendSynchronousMessage(
128             String destinationName, Object payload,
129             String responseDestinationName)
130         throws MessageBusException {
131 
132         return _instance._sendSynchronousMessage(
133             destinationName, payload, responseDestinationName);
134     }
135 
136     public static Object sendSynchronousMessage(
137             String destinationName, Object payload,
138             String responseDestinationName, long timeout)
139         throws MessageBusException {
140 
141         return _instance._sendSynchronousMessage(
142             destinationName, payload, responseDestinationName, timeout);
143     }
144 
145     public static boolean unregisterMessageListener(
146         String destinationName, MessageListener messageListener) {
147 
148         return _instance._unregisterMessageListener(
149             destinationName, messageListener);
150     }
151 
152     private MessageBusUtil() {
153     }
154 
155     private void _addDestination(Destination destination) {
156         _messageBus.addDestination(destination);
157     }
158 
159     private boolean _hasMessageListener(String destinationName) {
160         return _messageBus.hasMessageListener(destinationName);
161     }
162 
163     private void _init(
164         MessageBus messageBus, MessageSender messageSender,
165         SynchronousMessageSender synchronousMessageSender) {
166 
167         _messageBus = messageBus;
168         _messageSender = messageSender;
169         _synchronousMessageSender = synchronousMessageSender;
170     }
171 
172     private void _registerMessageListener(
173         String destinationName, MessageListener messageListener) {
174 
175         _messageBus.registerMessageListener(destinationName, messageListener);
176     }
177 
178     private void _removeDestination(String destinationName) {
179         _messageBus.removeDestination(destinationName);
180     }
181 
182     private void _sendMessage(String destinationName, Message message) {
183         _messageBus.sendMessage(destinationName, message);
184     }
185 
186     private void _sendMessage(String destinationName, Object payload) {
187         Message message = new Message();
188 
189         message.setPayload(payload);
190 
191         _sendMessage(destinationName, message);
192     }
193 
194     private Object _sendSynchronousMessage(
195             String destinationName, Message message)
196         throws MessageBusException {
197 
198         return _synchronousMessageSender.send(destinationName, message);
199     }
200 
201     private Object _sendSynchronousMessage(
202             String destinationName, Message message, long timeout)
203         throws MessageBusException {
204 
205         return _synchronousMessageSender.send(
206             destinationName, message, timeout);
207     }
208 
209     private Object _sendSynchronousMessage(
210             String destinationName, Object payload,
211             String responseDestinationName)
212         throws MessageBusException {
213 
214         Message message = new Message();
215 
216         message.setResponseDestinationName(responseDestinationName);
217         message.setPayload(payload);
218 
219         return _sendSynchronousMessage(destinationName, message);
220     }
221 
222     private Object _sendSynchronousMessage(
223             String destinationName, Object payload,
224             String responseDestinationName, long timeout)
225         throws MessageBusException {
226 
227         Message message = new Message();
228 
229         message.setResponseDestinationName(responseDestinationName);
230         message.setPayload(payload);
231 
232         return _sendSynchronousMessage(destinationName, message, timeout);
233     }
234 
235     private boolean _unregisterMessageListener(
236         String destinationName, MessageListener messageListener) {
237 
238         return _messageBus.unregisterMessageListener(
239             destinationName, messageListener);
240     }
241 
242     private static MessageBusUtil _instance = new MessageBusUtil();
243 
244     private MessageBus _messageBus;
245     private MessageSender _messageSender;
246     private SynchronousMessageSender _synchronousMessageSender;
247 
248 }