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