001
014
015 package com.liferay.portal.kernel.notifications;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019
020 import java.util.List;
021 import java.util.concurrent.CopyOnWriteArrayList;
022 import java.util.concurrent.atomic.AtomicLong;
023
024
027 public abstract class BaseChannelImpl implements Channel {
028
029 public void cleanUp() throws ChannelException {
030 long currentTime = System.currentTimeMillis();
031
032 long nextCleanUpTime = _nextCleanUpTime.get();
033
034 if ((currentTime > nextCleanUpTime) &&
035 _nextCleanUpTime.compareAndSet(
036 nextCleanUpTime, currentTime + _cleanUpInterval)) {
037
038 try {
039 doCleanUp();
040 }
041 catch (ChannelException ce) {
042 throw ce;
043 }
044 catch (Exception e) {
045 throw new ChannelException(e);
046 }
047 }
048 }
049
050 public void close() throws ChannelException {
051 flush();
052 }
053
054 public long getCompanyId() {
055 return _companyId;
056 }
057
058 public List<NotificationEvent> getNotificationEvents()
059 throws ChannelException {
060
061 return getNotificationEvents(true);
062 }
063
064 public long getUserId() {
065 return _userId;
066 }
067
068 public boolean hasNotificationEvents() {
069 try {
070 List<NotificationEvent> notificationEvents = getNotificationEvents(
071 false);
072
073 if (!notificationEvents.isEmpty()) {
074 return true;
075 }
076 }
077 catch (ChannelException ce) {
078 if (_log.isErrorEnabled()) {
079 _log.error("Unable to fetch notifications", ce);
080 }
081 }
082
083 return false;
084 }
085
086 public void registerChannelListener(ChannelListener channelListener) {
087 _channelListeners.add(channelListener);
088
089 if (hasNotificationEvents()) {
090 notifyChannelListeners();
091 }
092 }
093
094 public void setCleanUpInterval(long cleanUpInterval) {
095 _cleanUpInterval = cleanUpInterval;
096 }
097
098 public void unregisterChannelListener(ChannelListener channelListener) {
099 _channelListeners.remove(channelListener);
100
101 channelListener.channelListenerRemoved(_userId);
102 }
103
104 protected BaseChannelImpl(long companyId, long usedId) {
105 _companyId = companyId;
106 _userId = usedId;
107 }
108
109 protected abstract void doCleanUp() throws Exception;
110
111 protected void notifyChannelListeners() {
112 for (ChannelListener channelListener : _channelListeners) {
113 channelListener.notificationEventsAvailable(_userId);
114 }
115 }
116
117 private static Log _log = LogFactoryUtil.getLog(BaseChannelImpl.class);
118
119 private List<ChannelListener> _channelListeners =
120 new CopyOnWriteArrayList<ChannelListener>();
121 private long _cleanUpInterval;
122 private long _companyId;
123 private AtomicLong _nextCleanUpTime = new AtomicLong();
124 private long _userId;
125
126 }