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