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