001
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.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.notifications.ChannelException;
023 import com.liferay.portal.kernel.notifications.ChannelHubManagerUtil;
024 import com.liferay.portal.kernel.notifications.ChannelListener;
025 import com.liferay.portal.kernel.notifications.NotificationEvent;
026 import com.liferay.portal.kernel.notifications.UnknownChannelException;
027 import com.liferay.portal.util.PropsValues;
028
029 import java.util.List;
030
031
034 public class SynchronousPollerChannelListener implements ChannelListener {
035
036 public SynchronousPollerChannelListener(
037 long companyId, long userId,
038 JSONObject pollerResponseHeaderJSONObject) {
039
040 _companyId = companyId;
041 _userId = userId;
042 _pollerResponseHeaderJSONObject = pollerResponseHeaderJSONObject;
043 }
044
045 public synchronized void channelListenerRemoved(long channelId) {
046 _complete = true;
047
048 this.notify();
049 }
050
051 public synchronized String getNotificationEvents(long timeout)
052 throws ChannelException {
053
054 try {
055 if (!_complete) {
056 this.wait(timeout);
057 }
058 }
059 catch (InterruptedException ie) {
060 }
061
062 try {
063 Thread.sleep(PropsValues.POLLER_NOTIFICATIONS_TIMEOUT);
064 }
065 catch (InterruptedException ie) {
066 }
067
068 List<NotificationEvent> notificationEvents = null;
069
070 try {
071 notificationEvents = ChannelHubManagerUtil.getNotificationEvents(
072 _companyId, _userId, true);
073 }
074 catch (UnknownChannelException uce) {
075 if (_log.isDebugEnabled()) {
076 _log.debug(
077 "Unable to complete processing because user session ended",
078 uce);
079 }
080
081 return null;
082 }
083
084 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
085
086 jsonArray.put(_pollerResponseHeaderJSONObject);
087
088 for (NotificationEvent notificationEvent : notificationEvents) {
089 jsonArray.put(notificationEvent.toJSONObject());
090 }
091
092 return jsonArray.toString();
093 }
094
095 public synchronized void notificationEventsAvailable(long channelId) {
096 _complete = true;
097
098 this.notify();
099 }
100
101 private static Log _log = LogFactoryUtil.getLog(
102 SynchronousPollerChannelListener.class);
103
104 private long _companyId;
105 private boolean _complete;
106 private JSONObject _pollerResponseHeaderJSONObject;
107 private long _userId;
108
109 }