001
014
015 package com.liferay.portal.notifications;
016
017 import com.liferay.portal.kernel.notifications.Channel;
018 import com.liferay.portal.kernel.notifications.ChannelException;
019 import com.liferay.portal.kernel.notifications.ChannelHub;
020 import com.liferay.portal.kernel.notifications.ChannelHubManager;
021 import com.liferay.portal.kernel.notifications.ChannelListener;
022 import com.liferay.portal.kernel.notifications.DuplicateChannelHubException;
023 import com.liferay.portal.kernel.notifications.NotificationEvent;
024 import com.liferay.portal.kernel.notifications.UnknownChannelHubException;
025 import com.liferay.portal.kernel.security.pacl.DoPrivileged;
026
027 import java.util.Collection;
028 import java.util.Collections;
029 import java.util.List;
030 import java.util.concurrent.ConcurrentHashMap;
031 import java.util.concurrent.ConcurrentMap;
032
033
038 @DoPrivileged
039 public class ChannelHubManagerImpl implements ChannelHubManager {
040
041 @Override
042 public void confirmDelivery(
043 long companyId, long userId,
044 Collection<String> notificationEventUuids)
045 throws ChannelException {
046
047 confirmDelivery(companyId, userId, notificationEventUuids, false);
048 }
049
050 @Override
051 public void confirmDelivery(
052 long companyId, long userId,
053 Collection<String> notificationEventUuids, boolean archive)
054 throws ChannelException {
055
056 ChannelHub channelHub = getChannelHub(companyId);
057
058 channelHub.confirmDelivery(userId, notificationEventUuids, archive);
059 }
060
061 @Override
062 public void confirmDelivery(
063 long companyId, long userId, String notificationEventUuid)
064 throws ChannelException {
065
066 confirmDelivery(companyId, userId, notificationEventUuid, false);
067 }
068
069 @Override
070 public void confirmDelivery(
071 long companyId, long userId, String notificationEventUuid,
072 boolean archive)
073 throws ChannelException {
074
075 ChannelHub channelHub = getChannelHub(companyId);
076
077 channelHub.confirmDelivery(userId, notificationEventUuid, archive);
078 }
079
080 @Override
081 public Channel createChannel(long companyId, long userId)
082 throws ChannelException {
083
084 ChannelHub channelHub = getChannelHub(companyId);
085
086 return channelHub.createChannel(userId);
087 }
088
089 @Override
090 public ChannelHub createChannelHub(long companyId) throws ChannelException {
091 ChannelHub channelHub = _channelHub.clone(companyId);
092
093 if (_channelHubs.putIfAbsent(companyId, channelHub) != null) {
094 throw new DuplicateChannelHubException(
095 "Channel already exists with company id " + companyId);
096 }
097
098 return channelHub;
099 }
100
101 @Override
102 public void deleteUserNotificiationEvent(
103 long companyId, long userId, String notificationEventUuid)
104 throws ChannelException {
105
106 ChannelHub channelHub = getChannelHub(companyId);
107
108 channelHub.deleteUserNotificiationEvent(userId, notificationEventUuid);
109 }
110
111 @Override
112 public void deleteUserNotificiationEvents(
113 long companyId, long userId,
114 Collection<String> notificationEventUuids)
115 throws ChannelException {
116
117 ChannelHub channelHub = getChannelHub(companyId);
118
119 channelHub.deleteUserNotificiationEvents(
120 userId, notificationEventUuids);
121 }
122
123 @Override
124 public void destroyChannel(long companyId, long userId)
125 throws ChannelException {
126
127 ChannelHub channelHub = getChannelHub(companyId);
128
129 channelHub.destroyChannel(userId);
130 }
131
132 @Override
133 public void destroyChannelHub(long companyId) throws ChannelException {
134 ChannelHub channelHub = _channelHubs.remove(companyId);
135
136 if (channelHub != null) {
137 channelHub.destroy();
138 }
139 }
140
141 @Override
142 public ChannelHub fetchChannelHub(long companyId) throws ChannelException {
143 return fetchChannelHub(companyId, false);
144 }
145
146 @Override
147 public ChannelHub fetchChannelHub(long companyId, boolean createIfAbsent)
148 throws ChannelException {
149
150 ChannelHub channelHub = _channelHubs.get(companyId);
151
152 if (channelHub == null) {
153 synchronized(_channelHubs) {
154 channelHub = _channelHubs.get(companyId);
155
156 if (channelHub == null) {
157 if (createIfAbsent) {
158 channelHub = createChannelHub(companyId);
159 }
160 }
161 }
162 }
163
164 return channelHub;
165 }
166
167 @Override
168 public List<NotificationEvent> fetchNotificationEvents(
169 long companyId, long userId, boolean flush)
170 throws ChannelException {
171
172 ChannelHub channelHub = fetchChannelHub(companyId);
173
174 if (channelHub == null) {
175 return Collections.emptyList();
176 }
177
178 return channelHub.fetchNotificationEvents(userId, flush);
179
180 }
181
182 @Override
183 public void flush() throws ChannelException {
184 for (ChannelHub channelHub : _channelHubs.values()) {
185 channelHub.flush();
186 }
187 }
188
189 @Override
190 public void flush(long companyId) throws ChannelException {
191 ChannelHub channelHub = fetchChannelHub(companyId);
192
193 if (channelHub != null) {
194 channelHub.flush();
195 }
196 }
197
198 @Override
199 public void flush(long companyId, long userId, long timestamp)
200 throws ChannelException {
201
202 ChannelHub channelHub = fetchChannelHub(companyId);
203
204 if (channelHub != null) {
205 channelHub.flush(userId, timestamp);
206 }
207 }
208
209 @Override
210 public Channel getChannel(long companyId, long userId)
211 throws ChannelException {
212
213 return getChannel(companyId, userId, false);
214 }
215
216 @Override
217 public Channel getChannel(
218 long companyId, long userId, boolean createIfAbsent)
219 throws ChannelException {
220
221 ChannelHub channelHub = getChannelHub(companyId, createIfAbsent);
222
223 return channelHub.getChannel(userId, createIfAbsent);
224 }
225
226 @Override
227 public ChannelHub getChannelHub(long companyId) throws ChannelException {
228 return getChannelHub(companyId, false);
229 }
230
231 @Override
232 public ChannelHub getChannelHub(long companyId, boolean createIfAbsent)
233 throws ChannelException {
234
235 ChannelHub channelHub = fetchChannelHub(companyId, createIfAbsent);
236
237 if (channelHub == null) {
238 throw new UnknownChannelHubException(
239 "No channel exists with company id " + companyId);
240 }
241
242 return channelHub;
243 }
244
245 @Override
246 public List<NotificationEvent> getNotificationEvents(
247 long companyId, long userId)
248 throws ChannelException {
249
250 ChannelHub channelHub = getChannelHub(companyId);
251
252 return channelHub.getNotificationEvents(userId);
253 }
254
255 @Override
256 public List<NotificationEvent> getNotificationEvents(
257 long companyId, long userId, boolean flush)
258 throws ChannelException {
259
260 ChannelHub channelHub = getChannelHub(companyId);
261
262 return channelHub.getNotificationEvents(userId, flush);
263 }
264
265 @Override
266 public Collection<Long> getUserIds(long companyId) throws ChannelException {
267 ChannelHub channelHub = getChannelHub(companyId);
268
269 return channelHub.getUserIds();
270 }
271
272 @Override
273 public void registerChannelListener(
274 long companyId, long userId, ChannelListener channelListener)
275 throws ChannelException {
276
277 ChannelHub channelHub = getChannelHub(companyId);
278
279 channelHub.registerChannelListener(userId, channelListener);
280 }
281
282 @Override
283 public void removeTransientNotificationEvents(
284 long companyId, long userId,
285 Collection<NotificationEvent> notificationEvents)
286 throws ChannelException {
287
288 ChannelHub channelHub = getChannelHub(companyId);
289
290 channelHub.removeTransientNotificationEvents(
291 userId, notificationEvents);
292 }
293
294 @Override
295 public void removeTransientNotificationEventsByUuid(
296 long companyId, long userId,
297 Collection<String> notificationEventUuids)
298 throws ChannelException {
299
300 ChannelHub channelHub = getChannelHub(companyId);
301
302 channelHub.removeTransientNotificationEventsByUuid(
303 userId, notificationEventUuids);
304 }
305
306 @Override
307 public void sendNotificationEvent(
308 long companyId, long userId, NotificationEvent notificationEvent)
309 throws ChannelException {
310
311 ChannelHub channelHub = getChannelHub(companyId);
312
313 channelHub.sendNotificationEvent(userId, notificationEvent);
314 }
315
316 @Override
317 public void sendNotificationEvents(
318 long companyId, long userId,
319 Collection<NotificationEvent> notificationEvents)
320 throws ChannelException {
321
322 ChannelHub channelHub = getChannelHub(companyId);
323
324 channelHub.sendNotificationEvents(userId, notificationEvents);
325 }
326
327 public void setChannelHubPrototype(ChannelHub channelHub) {
328 _channelHub = channelHub;
329 }
330
331 @Override
332 public void unregisterChannelListener(
333 long companyId, long userId, ChannelListener channelListener)
334 throws ChannelException {
335
336 ChannelHub channelHub = getChannelHub(companyId);
337
338 channelHub.unregisterChannelListener(userId, channelListener);
339 }
340
341 private ChannelHub _channelHub;
342 private ConcurrentMap<Long, ChannelHub> _channelHubs =
343 new ConcurrentHashMap<Long, ChannelHub>();
344
345 }