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.sender;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.messaging.DestinationNames;
28  import com.liferay.portal.kernel.messaging.Message;
29  import com.liferay.portal.kernel.messaging.MessageBus;
30  import com.liferay.portal.kernel.messaging.MessageBusException;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.kernel.uuid.PortalUUID;
33  
34  /**
35   * <a href="DefaultSynchronousMessageSender.java.html"><b><i>View Source</i></b>
36   * </a>
37   *
38   * @author Michael C. Han
39   */
40  public class DefaultSynchronousMessageSender
41      implements SynchronousMessageSender {
42  
43      public DefaultSynchronousMessageSender() {
44      }
45  
46      /**
47       * @deprecated
48       */
49      public DefaultSynchronousMessageSender(
50          MessageBus messageBus, PortalUUID portalUUID, long timeout) {
51  
52          _messageBus = messageBus;
53          _portalUUID = portalUUID;
54          _timeout = timeout;
55      }
56  
57      public Object send(String destinationName, Message message)
58          throws MessageBusException {
59  
60          return send(destinationName, message, _timeout);
61      }
62  
63      public Object send(String destinationName, Message message, long timeout)
64          throws MessageBusException {
65  
66          if (!_messageBus.hasDestination(destinationName)) {
67              return null;
68          }
69  
70          message.setDestinationName(destinationName);
71  
72          String responseDestinationName = message.getResponseDestinationName();
73  
74          // Create a temporary destination if no response destination is
75          // configured
76  
77          if (Validator.isNull(responseDestinationName) ||
78              !_messageBus.hasDestination(responseDestinationName)) {
79  
80              if (_log.isDebugEnabled()) {
81                  _log.debug(
82                      "Response destination " + responseDestinationName +
83                          " is not configured");
84              }
85  
86              message.setResponseDestinationName(
87                  DestinationNames.MESSAGE_BUS_DEFAULT_RESPONSE);
88          }
89  
90          String responseId = _portalUUID.generate();
91  
92          message.setResponseId(responseId);
93  
94          SynchronousMessageListener synchronousMessageListener =
95              new SynchronousMessageListener(_messageBus, message, timeout);
96  
97          return synchronousMessageListener.send();
98      }
99  
100     /**
101      * @deprecated
102      */
103     public Object sendMessage(String destination, Message message)
104         throws MessageBusException {
105 
106         return send(destination, message);
107     }
108 
109     /**
110      * @deprecated
111      */
112     public Object sendMessage(String destination, Message message, long timeout)
113         throws MessageBusException {
114 
115         return send(destination, message, timeout);
116     }
117 
118     public void setMessageBus(MessageBus messageBus) {
119         _messageBus = messageBus;
120     }
121 
122     public void setPortalUUID(PortalUUID portalUUID) {
123         _portalUUID = portalUUID;
124     }
125 
126     public void setTimeout(long timeout) {
127         _timeout = timeout;
128     }
129 
130     private static Log _log =
131         LogFactoryUtil.getLog(DefaultSynchronousMessageSender.class);
132 
133     private MessageBus _messageBus;
134     private PortalUUID _portalUUID;
135     private long _timeout;
136 
137 }