001    /**
002     * Copyright (c) 2000-2012 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.Message;
021    import com.liferay.portal.kernel.messaging.MessageBus;
022    import com.liferay.portal.kernel.messaging.MessageBusException;
023    import com.liferay.portal.kernel.messaging.MessageListener;
024    import com.liferay.portal.kernel.messaging.MessageListenerException;
025    import com.liferay.portal.kernel.messaging.SynchronousDestination;
026    
027    import java.util.Set;
028    
029    /**
030     * @author Shuyang Zhou
031     */
032    public class DirectSynchronousMessageSender
033            implements SynchronousMessageSender {
034    
035            public Object send(String destinationName, Message message)
036                    throws MessageBusException {
037    
038                    Destination destination = _messageBus.getDestination(destinationName);
039    
040                    if (destination == null) {
041                            if (_log.isInfoEnabled()) {
042                                    _log.info(
043                                            "Destination " + destinationName + " is not configured");
044                            }
045    
046                            return null;
047                    }
048    
049                    if (destination.getMessageListenerCount() == 0) {
050                            if (_log.isInfoEnabled()) {
051                                    _log.info(
052                                            "Destination " + destinationName +
053                                                    " does not have any message listeners");
054                            }
055    
056                            return null;
057                    }
058    
059                    if (destination instanceof SynchronousDestination) {
060                            destination.send(message);
061                    }
062                    else {
063                            Set<MessageListener> messageListeners =
064                                    destination.getMessageListeners();
065    
066                            for (MessageListener messageListener : messageListeners) {
067                                    try {
068                                            messageListener.receive(message);
069                                    }
070                                    catch (MessageListenerException mle) {
071                                            throw new MessageBusException(mle);
072                                    }
073                            }
074                    }
075    
076                    return message.getResponse();
077            }
078    
079            public Object send(String destinationName, Message message, long timeout)
080                    throws MessageBusException {
081    
082                    if (_log.isWarnEnabled()) {
083                            _log.warn(
084                                    DirectSynchronousMessageSender.class.getName() +
085                                            " does not support timeout");
086                    }
087    
088                    return send(destinationName, message);
089            }
090    
091            public void setMessageBus(MessageBus messageBus) {
092                    _messageBus = messageBus;
093            }
094    
095            private static Log _log = LogFactoryUtil.getLog(
096                    DirectSynchronousMessageSender.class);
097    
098            private MessageBus _messageBus;
099    
100    }