1   /**
2    * Copyright (c) 2000-2008 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  /**
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 MessageBus getMessageBus() {
38          return _instance._messageBus;
39      }
40  
41      public static MessageSender getMessageSender() {
42          return _instance._messageSender;
43      }
44  
45      public static void init(
46          MessageBus messageBus, MessageSender messageSender) {
47  
48          _instance._init(messageBus, messageSender);
49      }
50  
51      public static void registerMessageListener(
52          String destination, MessageListener listener) {
53  
54          _instance._registerMessageListener(destination, listener);
55      }
56  
57      public static void removeDestination(String destination) {
58          _instance._removeDestination(destination);
59      }
60  
61      public static void sendMessage(String destination, Object message) {
62          _instance._sendMessage(destination, message);
63      }
64  
65      public static void sendMessage(String destination, String message) {
66          _instance._sendMessage(destination, message);
67      }
68  
69      public static Object sendSynchronizedMessage(
70              String destination, Message message)
71          throws MessageBusException {
72  
73          return _instance._sendSynchronizedMessage(destination, message);
74      }
75  
76      public static Object sendSynchronizedMessage(
77              String destination, Message message, long timeout)
78          throws MessageBusException {
79  
80          return _instance._sendSynchronizedMessage(
81              destination, message, timeout);
82      }
83  
84      public static String sendSynchronizedMessage(
85              String destination, String message)
86          throws MessageBusException {
87  
88          return _instance._sendSynchronizedMessage(destination, message);
89      }
90  
91      public static String sendSynchronizedMessage(
92              String destination, String message, long timeout)
93          throws MessageBusException {
94  
95          return _instance._sendSynchronizedMessage(
96              destination, message, timeout);
97      }
98  
99      public static boolean unregisterMessageListener(
100         String destination, MessageListener listener) {
101 
102         return _instance._unregisterMessageListener(destination, listener);
103     }
104 
105     private MessageBusUtil() {
106     }
107 
108     private void _addDestination(Destination destination) {
109         _messageBus.addDestination(destination);
110     }
111 
112     private void _init(MessageBus messageBus, MessageSender messageSender) {
113         _messageBus = messageBus;
114         _messageSender = messageSender;
115     }
116 
117     private void _registerMessageListener(
118         String destination, MessageListener listener) {
119 
120         _messageBus.registerMessageListener(destination, listener);
121     }
122 
123     private void _removeDestination(String destination) {
124         _messageBus.removeDestination(destination);
125     }
126 
127     private void _sendMessage(String destination, Object message) {
128         _messageBus.sendMessage(destination, message);
129     }
130 
131     private void _sendMessage(String destination, String message) {
132         _messageBus.sendMessage(destination, message);
133     }
134 
135     private Object _sendSynchronizedMessage(String destination, Message message)
136         throws MessageBusException {
137 
138         return _messageBus.sendSynchronizedMessage(
139             destination, message, _DEFAULT_TIMEOUT);
140     }
141 
142     private Object _sendSynchronizedMessage(
143             String destination, Message message, long timeout)
144         throws MessageBusException {
145 
146         return _messageBus.sendSynchronizedMessage(
147             destination, message, timeout);
148     }
149 
150     private String _sendSynchronizedMessage(String destination, String message)
151         throws MessageBusException {
152 
153         return _messageBus.sendSynchronizedMessage(
154             destination, message, _DEFAULT_TIMEOUT);
155     }
156 
157     private String _sendSynchronizedMessage(
158             String destination, String message, long timeout)
159         throws MessageBusException {
160 
161         return _messageBus.sendSynchronizedMessage(
162             destination, message, timeout);
163     }
164 
165     private boolean _unregisterMessageListener(
166         String destination, MessageListener listener) {
167 
168         return _messageBus.unregisterMessageListener(destination, listener);
169     }
170 
171     private static final long _DEFAULT_TIMEOUT = 10000;
172 
173     private static MessageBusUtil _instance = new MessageBusUtil();
174 
175     private MessageBus _messageBus;
176     private MessageSender _messageSender;
177 
178 }