001    /**
002     * Copyright (c) 2000-present 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.BaseMessageListener;
020    import com.liferay.portal.kernel.messaging.Message;
021    import com.liferay.portal.kernel.poller.PollerException;
022    import com.liferay.portal.kernel.poller.PollerProcessor;
023    import com.liferay.portal.kernel.poller.PollerRequest;
024    import com.liferay.portal.kernel.poller.PollerResponse;
025    import com.liferay.portal.poller.PollerProcessorUtil;
026    
027    /**
028     * @author Michael C. Han
029     * @author Brian Wing Shun Chan
030     */
031    public class PollerRequestMessageListener extends BaseMessageListener {
032    
033            @Override
034            protected void doReceive(Message message) throws Exception {
035                    PollerRequest pollerRequest = (PollerRequest)message.getPayload();
036    
037                    String portletId = pollerRequest.getPortletId();
038    
039                    PollerProcessor pollerProcessor =
040                            PollerProcessorUtil.getPollerProcessor(portletId);
041    
042                    if (pollerRequest.isReceiveRequest()) {
043                            PollerResponse pollerResponse = null;
044    
045                            try {
046                                    pollerResponse = pollerProcessor.receive(pollerRequest);
047                            }
048                            catch (PollerException pe) {
049                                    _log.error(
050                                            "Unable to receive poller request " + pollerRequest, pe);
051    
052                                    pollerResponse = pollerRequest.createPollerResponse();
053    
054                                    pollerResponse.setParameter("pollerException", pe.getMessage());
055                            }
056                            finally {
057                                    if (pollerResponse == null) {
058                                            pollerResponse = pollerRequest.createPollerResponse();
059                                    }
060    
061                                    pollerResponse.close(
062                                            message, pollerRequest.getPollerHeader(),
063                                            pollerRequest.getPortletId(), pollerRequest.getChunkId());
064                            }
065                    }
066                    else {
067                            try {
068                                    pollerProcessor.send(pollerRequest);
069                            }
070                            catch (PollerException pe) {
071                                    _log.error(
072                                            "Unable to send poller request " + pollerRequest, pe);
073                            }
074                    }
075            }
076    
077            private static final Log _log = LogFactoryUtil.getLog(
078                    PollerRequestMessageListener.class);
079    
080    }