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;
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            @Override
043            public synchronized void channelListenerRemoved(long channelId) {
044                    _complete = true;
045    
046                    this.notify();
047            }
048    
049            public synchronized String getNotificationEvents(long timeout)
050                    throws ChannelException {
051    
052                    try {
053                            if (!_complete) {
054                                    this.wait(timeout);
055                            }
056                    }
057                    catch (InterruptedException ie) {
058                    }
059    
060                    try {
061                            Thread.sleep(PropsValues.POLLER_NOTIFICATIONS_TIMEOUT);
062                    }
063                    catch (InterruptedException ie) {
064                    }
065    
066                    List<NotificationEvent> notificationEvents =
067                            ChannelHubManagerUtil.fetchNotificationEvents(
068                                    _companyId, _userId, true);
069    
070                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
071    
072                    jsonArray.put(_pollerResponseHeaderJSONObject);
073    
074                    for (NotificationEvent notificationEvent : notificationEvents) {
075                            jsonArray.put(notificationEvent.toJSONObject());
076                    }
077    
078                    return jsonArray.toString();
079            }
080    
081            @Override
082            public synchronized void notificationEventsAvailable(long channelId) {
083                    _complete = true;
084    
085                    this.notify();
086            }
087    
088            private long _companyId;
089            private boolean _complete;
090            private JSONObject _pollerResponseHeaderJSONObject;
091            private long _userId;
092    
093    }