001    /**
002     * Copyright (c) 2000-2011 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.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    }