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.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            @Override
040            public void channelListenerRemoved(long channelId) {
041            }
042    
043            @Override
044            public void notificationEventsAvailable(long channelId) {
045                    sendProcessMessage();
046            }
047    
048            protected void sendProcessMessage() {
049                    CometRequest cometRequest = _cometSession.getCometRequest();
050    
051                    try {
052                            ChannelHubManagerUtil.unregisterChannelListener(
053                                    cometRequest.getCompanyId(), cometRequest.getUserId(), this);
054                    }
055                    catch (UnknownChannelException uce) {
056                    }
057                    catch (ChannelException ce) {
058                            if (_log.isWarnEnabled()) {
059                                    _log.warn("Unable to unregister channel listener", ce);
060                            }
061                    }
062    
063                    PollerCometDelayedTask pollerCometDelayedTask =
064                            new PollerCometDelayedTask(
065                                    _cometSession, _pollerResponseHeaderJSONObject);
066    
067                    PollerCometDelayedJobUtil.addPollerCometDelayedTask(
068                            pollerCometDelayedTask);
069            }
070    
071            private static Log _log = LogFactoryUtil.getLog(
072                    PollerCometChannelListener.class);
073    
074            private CometSession _cometSession;
075            private JSONObject _pollerResponseHeaderJSONObject;
076    
077    }