001    /**
002     * Copyright (c) 2000-2012 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.comet;
016    
017    import com.liferay.portal.kernel.json.JSONObject;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.notifications.ChannelException;
021    import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
022    import com.liferay.portal.kernel.notifications.ChannelListener;
023    import com.liferay.portal.kernel.notifications.UnknownChannelException;
024    import com.liferay.portal.kernel.poller.comet.CometRequest;
025    import com.liferay.portal.kernel.poller.comet.CometSession;
026    
027    /**
028     * @author Edward Han
029     */
030    public class PollerCometChannelListener implements ChannelListener {
031    
032            public PollerCometChannelListener(
033                    CometSession cometSession, JSONObject pollerResponseHeaderJSONObject) {
034    
035                    _cometSession = cometSession;
036                    _pollerResponseHeaderJSONObject = pollerResponseHeaderJSONObject;
037            }
038    
039            public void channelListenerRemoved(long channelId) {
040            }
041    
042            public void notificationEventsAvailable(long channelId) {
043                    sendProcessMessage();
044            }
045    
046            protected void sendProcessMessage() {
047                    CometRequest cometRequest = _cometSession.getCometRequest();
048    
049                    try {
050                            ChannelHubManagerUtil.unregisterChannelListener(
051                                    cometRequest.getCompanyId(), cometRequest.getUserId(), this);
052                    }
053                    catch (UnknownChannelException uce) {
054                    }
055                    catch (ChannelException ce) {
056                            if (_log.isWarnEnabled()) {
057                                    _log.warn("Unable to unregister channel listener", ce);
058                            }
059                    }
060    
061                    PollerCometDelayedTask pollerCometDelayedTask =
062                            new PollerCometDelayedTask(
063                                    _cometSession, _pollerResponseHeaderJSONObject);
064    
065                    PollerCometDelayedJobUtil.addPollerCometDelayedTask(
066                            pollerCometDelayedTask);
067            }
068    
069            private static Log _log = LogFactoryUtil.getLog(
070                    PollerCometChannelListener.class);
071    
072            private CometSession _cometSession;
073            private JSONObject _pollerResponseHeaderJSONObject;
074    
075    }