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.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    }