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