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.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    import com.liferay.portal.poller.PollerRequestResponsePair;
027    
028    /**
029     * @author Michael C. Han
030     * @author Brian Wing Shun Chan
031     */
032    public class PollerRequestMessageListener extends BaseMessageListener {
033    
034            @Override
035            protected void doReceive(Message message) throws Exception {
036                    PollerRequestResponsePair pollerRequestResponsePair =
037                            (PollerRequestResponsePair)message.getPayload();
038    
039                    PollerRequest pollerRequest =
040                            pollerRequestResponsePair.getPollerRequest();
041    
042                    PollerResponse pollerResponse =
043                            pollerRequestResponsePair.getPollerResponse();
044    
045                    String portletId = pollerRequest.getPortletId();
046    
047                    PollerProcessor pollerProcessor =
048                            PollerProcessorUtil.getPollerProcessor(portletId);
049    
050                    if (pollerRequest.isReceiveRequest()) {
051                            pollerResponse.createResponseMessage(message);
052    
053                            try {
054                                    pollerProcessor.receive(pollerRequest, pollerResponse);
055                            }
056                            catch (PollerException pe) {
057                                    _log.error(
058                                            "Unable to receive poller request " + pollerRequest, pe);
059    
060                                    pollerResponse.setParameter("pollerException", pe.getMessage());
061                            }
062                            finally {
063                                    pollerResponse.close();
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 Log _log = LogFactoryUtil.getLog(
078                    PollerRequestMessageListener.class);
079    
080    }