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.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    /**
034     * @author Edward Han
035     * @author Brian Wing Shun
036     * @author Shuyang Zhou
037     */
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            @Override
182            public void flush() throws ChannelException {
183                    for (ChannelHub channelHub : _channelHubs.values()) {
184                            channelHub.flush();
185                    }
186            }
187    
188            @Override
189            public void flush(long companyId) throws ChannelException {
190                    ChannelHub channelHub = fetchChannelHub(companyId);
191    
192                    if (channelHub != null) {
193                            channelHub.flush();
194                    }
195            }
196    
197            @Override
198            public void flush(long companyId, long userId, long timestamp)
199                    throws ChannelException {
200    
201                    ChannelHub channelHub = fetchChannelHub(companyId);
202    
203                    if (channelHub != null) {
204                            channelHub.flush(userId, timestamp);
205                    }
206            }
207    
208            @Override
209            public Channel getChannel(long companyId, long userId)
210                    throws ChannelException {
211    
212                    return getChannel(companyId, userId, false);
213            }
214    
215            @Override
216            public Channel getChannel(
217                            long companyId, long userId, boolean createIfAbsent)
218                    throws ChannelException {
219    
220                    ChannelHub channelHub = getChannelHub(companyId, createIfAbsent);
221    
222                    return channelHub.getChannel(userId, createIfAbsent);
223            }
224    
225            @Override
226            public ChannelHub getChannelHub(long companyId) throws ChannelException {
227                    return getChannelHub(companyId, false);
228            }
229    
230            @Override
231            public ChannelHub getChannelHub(long companyId, boolean createIfAbsent)
232                    throws ChannelException {
233    
234                    ChannelHub channelHub = fetchChannelHub(companyId, createIfAbsent);
235    
236                    if (channelHub == null) {
237                            throw new UnknownChannelHubException(
238                                    "No channel exists with company id " + companyId);
239                    }
240    
241                    return channelHub;
242            }
243    
244            @Override
245            public List<NotificationEvent> getNotificationEvents(
246                            long companyId, long userId)
247                    throws ChannelException {
248    
249                    ChannelHub channelHub = getChannelHub(companyId);
250    
251                    return channelHub.getNotificationEvents(userId);
252            }
253    
254            @Override
255            public List<NotificationEvent> getNotificationEvents(
256                            long companyId, long userId, boolean flush)
257                    throws ChannelException {
258    
259                    ChannelHub channelHub = getChannelHub(companyId);
260    
261                    return channelHub.getNotificationEvents(userId, flush);
262            }
263    
264            @Override
265            public Collection<Long> getUserIds(long companyId) throws ChannelException {
266                    ChannelHub channelHub = getChannelHub(companyId);
267    
268                    return channelHub.getUserIds();
269            }
270    
271            @Override
272            public void registerChannelListener(
273                            long companyId, long userId, ChannelListener channelListener)
274                    throws ChannelException {
275    
276                    ChannelHub channelHub = getChannelHub(companyId);
277    
278                    channelHub.registerChannelListener(userId, channelListener);
279            }
280    
281            @Override
282            public void removeTransientNotificationEvents(
283                            long companyId, long userId,
284                            Collection<NotificationEvent> notificationEvents)
285                    throws ChannelException {
286    
287                    ChannelHub channelHub = getChannelHub(companyId);
288    
289                    channelHub.removeTransientNotificationEvents(
290                            userId, notificationEvents);
291            }
292    
293            @Override
294            public void removeTransientNotificationEventsByUuid(
295                            long companyId, long userId,
296                            Collection<String> notificationEventUuids)
297                    throws ChannelException {
298    
299                    ChannelHub channelHub = getChannelHub(companyId);
300    
301                    channelHub.removeTransientNotificationEventsByUuid(
302                            userId, notificationEventUuids);
303            }
304    
305            @Override
306            public void sendNotificationEvent(
307                            long companyId, long userId, NotificationEvent notificationEvent)
308                    throws ChannelException {
309    
310                    ChannelHub channelHub = getChannelHub(companyId);
311    
312                    channelHub.sendNotificationEvent(userId, notificationEvent);
313            }
314    
315            @Override
316            public void sendNotificationEvents(
317                            long companyId, long userId,
318                            Collection<NotificationEvent> notificationEvents)
319                    throws ChannelException {
320    
321                    ChannelHub channelHub = getChannelHub(companyId);
322    
323                    channelHub.sendNotificationEvents(userId, notificationEvents);
324            }
325    
326            public void setChannelHubPrototype(ChannelHub channelHub) {
327                    _channelHub = channelHub;
328            }
329    
330            @Override
331            public void unregisterChannelListener(
332                            long companyId, long userId, ChannelListener channelListener)
333                    throws ChannelException {
334    
335                    ChannelHub channelHub = getChannelHub(companyId);
336    
337                    channelHub.unregisterChannelListener(userId, channelListener);
338            }
339    
340            private ChannelHub _channelHub;
341            private ConcurrentMap<Long, ChannelHub> _channelHubs =
342                    new ConcurrentHashMap<Long, ChannelHub>();
343    
344    }