001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.messaging.sender;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.Destination;
020    import com.liferay.portal.kernel.messaging.DestinationNames;
021    import com.liferay.portal.kernel.messaging.Message;
022    import com.liferay.portal.kernel.messaging.MessageBus;
023    import com.liferay.portal.kernel.messaging.MessageBusException;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.uuid.PortalUUID;
026    
027    /**
028     * @author Michael C. Han
029     */
030    public class DefaultSynchronousMessageSender
031            implements SynchronousMessageSender {
032    
033            public DefaultSynchronousMessageSender() {
034            }
035    
036            /**
037             * @deprecated As of 6.1.0
038             */
039            public DefaultSynchronousMessageSender(
040                    MessageBus messageBus, PortalUUID portalUUID, long timeout) {
041    
042                    _messageBus = messageBus;
043                    _portalUUID = portalUUID;
044                    _timeout = timeout;
045            }
046    
047            @Override
048            public Object send(String destinationName, Message message)
049                    throws MessageBusException {
050    
051                    return send(destinationName, message, _timeout);
052            }
053    
054            @Override
055            public Object send(String destinationName, Message message, long timeout)
056                    throws MessageBusException {
057    
058                    Destination destination = _messageBus.getDestination(destinationName);
059    
060                    if (destination == null) {
061                            if (_log.isInfoEnabled()) {
062                                    _log.info(
063                                            "Destination " + destinationName + " is not configured");
064                            }
065    
066                            return null;
067                    }
068    
069                    if (destination.getMessageListenerCount() == 0) {
070                            if (_log.isInfoEnabled()) {
071                                    _log.info(
072                                            "Destination " + destinationName +
073                                                    " does not have any message listeners");
074                            }
075    
076                            return null;
077                    }
078    
079                    message.setDestinationName(destinationName);
080    
081                    String responseDestinationName = message.getResponseDestinationName();
082    
083                    // Create a temporary destination if no response destination is
084                    // configured
085    
086                    if (Validator.isNull(responseDestinationName) ||
087                            !_messageBus.hasDestination(responseDestinationName)) {
088    
089                            if (_log.isDebugEnabled()) {
090                                    _log.debug(
091                                            "Response destination " + responseDestinationName +
092                                                    " is not configured");
093                            }
094    
095                            message.setResponseDestinationName(
096                                    DestinationNames.MESSAGE_BUS_DEFAULT_RESPONSE);
097                    }
098    
099                    String responseId = _portalUUID.generate();
100    
101                    message.setResponseId(responseId);
102    
103                    SynchronousMessageListener synchronousMessageListener =
104                            new SynchronousMessageListener(_messageBus, message, timeout);
105    
106                    return synchronousMessageListener.send();
107            }
108    
109            public void setMessageBus(MessageBus messageBus) {
110                    _messageBus = messageBus;
111            }
112    
113            public void setPortalUUID(PortalUUID portalUUID) {
114                    _portalUUID = portalUUID;
115            }
116    
117            public void setTimeout(long timeout) {
118                    _timeout = timeout;
119            }
120    
121            private static Log _log = LogFactoryUtil.getLog(
122                    DefaultSynchronousMessageSender.class);
123    
124            private MessageBus _messageBus;
125            private PortalUUID _portalUUID;
126            private long _timeout;
127    
128    }