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;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
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.NotificationEvent;
024    import com.liferay.portal.util.PropsValues;
025    
026    import java.util.List;
027    
028    /**
029     * @author Edward Han
030     */
031    public class SynchronousPollerChannelListener implements ChannelListener {
032    
033            public SynchronousPollerChannelListener(
034                    long companyId, long userId,
035                    JSONObject pollerResponseHeaderJSONObject) {
036    
037                    _companyId = companyId;
038                    _userId = userId;
039                    _pollerResponseHeaderJSONObject = pollerResponseHeaderJSONObject;
040            }
041    
042            public synchronized void channelListenerRemoved(long channelId) {
043                    _complete = true;
044    
045                    this.notify();
046            }
047    
048            public synchronized String getNotificationEvents(long timeout)
049                    throws ChannelException {
050    
051                    try {
052                            if (!_complete) {
053                                    this.wait(timeout);
054                            }
055                    }
056                    catch (InterruptedException ie) {
057                    }
058    
059                    try {
060                            Thread.sleep(PropsValues.POLLER_NOTIFICATIONS_TIMEOUT);
061                    }
062                    catch (InterruptedException ie) {
063                    }
064    
065                    List<NotificationEvent> notificationEvents =
066                            ChannelHubManagerUtil.fetchNotificationEvents(
067                                    _companyId, _userId, true);
068    
069                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
070    
071                    jsonArray.put(_pollerResponseHeaderJSONObject);
072    
073                    for (NotificationEvent notificationEvent : notificationEvents) {
074                            jsonArray.put(notificationEvent.toJSONObject());
075                    }
076    
077                    return jsonArray.toString();
078            }
079    
080            public synchronized void notificationEventsAvailable(long channelId) {
081                    _complete = true;
082    
083                    this.notify();
084            }
085    
086            private long _companyId;
087            private boolean _complete;
088            private JSONObject _pollerResponseHeaderJSONObject;
089            private long _userId;
090    
091    }