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