001    /**
002     * Copyright (c) 2000-2013 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;
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    }