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    
025    import java.util.List;
026    
027    /**
028     * @author Edward Han
029     */
030    public class SynchronousPollerChannelListener implements ChannelListener {
031    
032            @Override
033            public synchronized void channelListenerRemoved(long channelId) {
034                    _complete = true;
035    
036                    this.notify();
037            }
038    
039            public synchronized String getNotificationEvents(
040                            long companyId, long userId,
041                            JSONObject pollerResponseHeaderJSONObject, long timeout)
042                    throws ChannelException {
043    
044                    try {
045                            if (!_complete) {
046                                    this.wait(timeout);
047                            }
048                    }
049                    catch (InterruptedException ie) {
050                    }
051    
052                    List<NotificationEvent> notificationEvents =
053                            ChannelHubManagerUtil.fetchNotificationEvents(
054                                    companyId, userId, true);
055    
056                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
057    
058                    jsonArray.put(pollerResponseHeaderJSONObject);
059    
060                    for (NotificationEvent notificationEvent : notificationEvents) {
061                            jsonArray.put(notificationEvent.toJSONObject());
062                    }
063    
064                    return jsonArray.toString();
065            }
066    
067            @Override
068            public synchronized void notificationEventsAvailable(long channelId) {
069                    _complete = true;
070    
071                    this.notify();
072            }
073    
074            private boolean _complete;
075    
076    }