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.notifications.ChannelHubManagerUtil;
022    import com.liferay.portal.kernel.notifications.NotificationEvent;
023    import com.liferay.portal.kernel.notifications.NotificationEventFactoryUtil;
024    import com.liferay.portal.kernel.notifications.UnknownChannelException;
025    import com.liferay.portal.kernel.poller.PollerHeader;
026    import com.liferay.portal.kernel.poller.PollerResponse;
027    
028    /**
029     * @author Edward Han
030     */
031    public class PollerNotificationsBridgeMessageListener
032            extends BaseMessageListener {
033    
034            @Override
035            protected void doReceive(Message message) throws Exception {
036                    Object messagePayload = message.getPayload();
037    
038                    if (!(messagePayload instanceof PollerResponse)) {
039                            if (_log.isWarnEnabled()) {
040                                    _log.warn(
041                                            "Received message with payload not of type PollerResponse");
042                            }
043    
044                            return;
045                    }
046    
047                    PollerResponse pollerResponse = (PollerResponse)messagePayload;
048    
049                    if (pollerResponse.isEmpty()) {
050                            return;
051                    }
052    
053                    PollerHeader pollerHeader = pollerResponse.getPollerHeader();
054    
055                    NotificationEvent notificationEvent =
056                            NotificationEventFactoryUtil.createNotificationEvent(
057                                    System.currentTimeMillis(),
058                                    PollerNotificationsBridgeMessageListener.class.getName(),
059                                    pollerResponse.toJSONObject());
060    
061                    try {
062                            ChannelHubManagerUtil.sendNotificationEvent(
063                                    pollerHeader.getCompanyId(), pollerHeader.getUserId(),
064                                    notificationEvent);
065                    }
066                    catch (UnknownChannelException uce) {
067                            if (_log.isDebugEnabled()) {
068                                    _log.debug(
069                                            "Unable to complete processing because user session ended",
070                                            uce);
071                            }
072                    }
073            }
074    
075            private static Log _log = LogFactoryUtil.getLog(
076                    PollerNotificationsBridgeMessageListener.class);
077    
078    }