1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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     public void setMessageBus(MessageBus messageBus) {
111         _messageBus = messageBus;
112     }
113 
114     public void setPortalUUID(PortalUUID portalUUID) {
115         _portalUUID = portalUUID;
116     }
117 
118     public void setTimeout(long timeout) {
119         _timeout = timeout;
120     }
121 
122     private static Log _log = LogFactoryUtil.getLog(
123         DefaultSynchronousMessageSender.class);
124 
125     private MessageBus _messageBus;
126     private PortalUUID _portalUUID;
127     private long _timeout;
128 
129 }