001    /**
002     * Copyright (c) 2000-2010 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.poller.messaging;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.messaging.Message;
020    import com.liferay.portal.kernel.messaging.MessageBusUtil;
021    import com.liferay.portal.kernel.messaging.MessageListener;
022    import com.liferay.portal.kernel.poller.PollerException;
023    import com.liferay.portal.kernel.poller.PollerProcessor;
024    import com.liferay.portal.kernel.poller.PollerRequest;
025    import com.liferay.portal.kernel.poller.PollerResponse;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.poller.PollerProcessorUtil;
028    
029    /**
030     * @author Michael C. Han
031     */
032    public class PollerMessageListener implements MessageListener {
033    
034            public void receive(Message message) {
035                    PollerResponse pollerResponse = null;
036    
037                    try {
038                            PollerRequest pollerRequest = (PollerRequest)message.getPayload();
039    
040                            String portletId = pollerRequest.getPortletId();
041    
042                            PollerProcessor pollerProcessor =
043                                    PollerProcessorUtil.getPollerProcessor(portletId);
044    
045                            if (pollerRequest.isReceiveRequest()) {
046                                    pollerResponse = new PollerResponse(
047                                            portletId, pollerRequest.getChunkId());
048    
049                                    try {
050                                            pollerProcessor.receive(pollerRequest, pollerResponse);
051                                    }
052                                    catch (PollerException pe) {
053                                            _log.error(
054                                                    "Unable to receive poller request " + pollerRequest,
055                                                    pe);
056    
057                                            pollerResponse.setParameter(
058                                                    "pollerException", pe.getMessage());
059                                    }
060                            }
061                            else {
062                                    try {
063                                            pollerProcessor.send(pollerRequest);
064                                    }
065                                    catch (PollerException pe) {
066                                            _log.error(
067                                                    "Unable to send poller request " + pollerRequest,
068                                                    pe);
069                                    }
070                            }
071                    }
072                    finally {
073                            String responseDestinationName =
074                                    message.getResponseDestinationName();
075    
076                            if (Validator.isNotNull(responseDestinationName)) {
077                                    Message responseMessage = MessageBusUtil.createResponseMessage(
078                                            message);
079    
080                                    responseMessage.setPayload(pollerResponse);
081    
082                                    MessageBusUtil.sendMessage(
083                                            responseDestinationName, responseMessage);
084                            }
085                    }
086            }
087    
088            private static Log _log = LogFactoryUtil.getLog(
089                    PollerMessageListener.class);
090    
091    }