001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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
038             */
039            public DefaultSynchronousMessageSender(
040                    MessageBus messageBus, PortalUUID portalUUID, long timeout) {
041    
042                    _messageBus = messageBus;
043                    _portalUUID = portalUUID;
044                    _timeout = timeout;
045            }
046    
047            public Object send(String destinationName, Message message)
048                    throws MessageBusException {
049    
050                    return send(destinationName, message, _timeout);
051            }
052    
053            public Object send(String destinationName, Message message, long timeout)
054                    throws MessageBusException {
055    
056                    Destination destination = _messageBus.getDestination(destinationName);
057    
058                    if (destination == null) {
059                            if (_log.isInfoEnabled()) {
060                                    _log.info(
061                                            "Destination " + destinationName + " is not configured");
062                            }
063    
064                            return null;
065                    }
066    
067                    if (destination.getMessageListenerCount() == 0) {
068                            if (_log.isInfoEnabled()) {
069                                    _log.info(
070                                            "Destination " + destinationName +
071                                                    " does not have any message listeners");
072                            }
073    
074                            return null;
075                    }
076    
077                    message.setDestinationName(destinationName);
078    
079                    String responseDestinationName = message.getResponseDestinationName();
080    
081                    // Create a temporary destination if no response destination is
082                    // configured
083    
084                    if (Validator.isNull(responseDestinationName) ||
085                            !_messageBus.hasDestination(responseDestinationName)) {
086    
087                            if (_log.isDebugEnabled()) {
088                                    _log.debug(
089                                            "Response destination " + responseDestinationName +
090                                                    " is not configured");
091                            }
092    
093                            message.setResponseDestinationName(
094                                    DestinationNames.MESSAGE_BUS_DEFAULT_RESPONSE);
095                    }
096    
097                    String responseId = _portalUUID.generate();
098    
099                    message.setResponseId(responseId);
100    
101                    SynchronousMessageListener synchronousMessageListener =
102                            new SynchronousMessageListener(_messageBus, message, timeout);
103    
104                    return synchronousMessageListener.send();
105            }
106    
107            public void setMessageBus(MessageBus messageBus) {
108                    _messageBus = messageBus;
109            }
110    
111            public void setPortalUUID(PortalUUID portalUUID) {
112                    _portalUUID = portalUUID;
113            }
114    
115            public void setTimeout(long timeout) {
116                    _timeout = timeout;
117            }
118    
119            private static Log _log = LogFactoryUtil.getLog(
120                    DefaultSynchronousMessageSender.class);
121    
122            private MessageBus _messageBus;
123            private PortalUUID _portalUUID;
124            private long _timeout;
125    
126    }