001
014
015 package com.liferay.portal.kernel.notifications;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.model.UserNotificationDeliveryConstants;
022 import com.liferay.portal.model.UserNotificationEvent;
023 import com.liferay.portal.service.ServiceContext;
024 import com.liferay.registry.Registry;
025 import com.liferay.registry.RegistryUtil;
026 import com.liferay.registry.ServiceReference;
027 import com.liferay.registry.ServiceRegistration;
028 import com.liferay.registry.ServiceTracker;
029 import com.liferay.registry.ServiceTrackerCustomizer;
030 import com.liferay.registry.collections.ServiceRegistrationMap;
031 import com.liferay.registry.collections.ServiceTrackerCollections;
032 import com.liferay.registry.collections.ServiceTrackerMap;
033
034 import java.util.ArrayList;
035 import java.util.Collections;
036 import java.util.HashMap;
037 import java.util.List;
038 import java.util.Map;
039 import java.util.concurrent.ConcurrentHashMap;
040
041
045 public class UserNotificationManagerUtil {
046
047 public static void addUserNotificationDefinition(
048 String portletId,
049 UserNotificationDefinition userNotificationDefinition) {
050
051 _instance._addUserNotificationDefinition(
052 portletId, userNotificationDefinition);
053 }
054
055 public static void addUserNotificationHandler(
056 UserNotificationHandler userNotificationHandler) {
057
058 _instance._addUserNotificationHandler(userNotificationHandler);
059 }
060
061 public static void deleteUserNotificationDefinitions(String portletId) {
062 _instance._deleteUserNotificationDefinitions(portletId);
063 }
064
065 public static void deleteUserNotificationHandler(
066 UserNotificationHandler userNotificationHandler) {
067
068 _instance._deleteUserNotificationHandler(userNotificationHandler);
069 }
070
071 public static UserNotificationDefinition fetchUserNotificationDefinition(
072 String portletId, long classNameId, int notificationType) {
073
074 return _instance._fetchUserNotificationDefinition(
075 portletId, classNameId, notificationType);
076 }
077
078 public static Map<String, List<UserNotificationDefinition>>
079 getUserNotificationDefinitions() {
080
081 Map<String, List<UserNotificationDefinition>>
082 userNotificationDefinitionsMap = new ConcurrentHashMap<>();
083
084 ServiceTrackerMap<String, List<UserNotificationDefinition>>
085 userNotificationDefinitionsServiceTrackerMap =
086 _instance._userNotificationDefinitions;
087
088 for (String portletId :
089 userNotificationDefinitionsServiceTrackerMap.keySet()) {
090
091 userNotificationDefinitionsMap.put(
092 portletId,
093 userNotificationDefinitionsServiceTrackerMap.getService(
094 portletId));
095 }
096
097 return Collections.unmodifiableMap(userNotificationDefinitionsMap);
098 }
099
100 public static Map<String, Map<String, UserNotificationHandler>>
101 getUserNotificationHandlers() {
102
103 return Collections.unmodifiableMap(_instance._userNotificationHandlers);
104 }
105
106 public static UserNotificationFeedEntry interpret(
107 String selector, UserNotificationEvent userNotificationEvent,
108 ServiceContext serviceContext)
109 throws PortalException {
110
111 return _instance._interpret(
112 selector, userNotificationEvent, serviceContext);
113 }
114
115 public static boolean isDeliver(
116 long userId, String portletId, long classNameId,
117 int notificationType, int deliveryType)
118 throws PortalException {
119
120 return _instance._isDeliver(
121 userId, StringPool.BLANK, portletId, classNameId, notificationType,
122 deliveryType, null);
123 }
124
125 public static boolean isDeliver(
126 long userId, String selector, String portletId, long classNameId,
127 int notificationType, int deliveryType,
128 ServiceContext serviceContext)
129 throws PortalException {
130
131 return _instance._isDeliver(
132 userId, selector, portletId, classNameId, notificationType,
133 deliveryType, serviceContext);
134 }
135
136 private UserNotificationManagerUtil() {
137 Registry registry = RegistryUtil.getRegistry();
138
139 _userNotificationHandlerServiceTracker = registry.trackServices(
140 UserNotificationHandler.class,
141 new UserNotificationHandlerServiceTrackerCustomizer());
142
143 _userNotificationHandlerServiceTracker.open();
144
145 _userNotificationDefinitions.open();
146 }
147
148 private void _addUserNotificationDefinition(
149 String portletId,
150 UserNotificationDefinition userNotificationDefinition) {
151
152 Registry registry = RegistryUtil.getRegistry();
153
154 Map<String, Object> properties = new HashMap<String, Object>();
155
156 properties.put("javax.portlet.name", portletId);
157
158 ServiceRegistration<UserNotificationDefinition> serviceRegistration =
159 registry.registerService(
160 UserNotificationDefinition.class, userNotificationDefinition,
161 properties);
162
163 List<ServiceRegistration<UserNotificationDefinition>>
164 serviceRegistrations = new ArrayList<>();
165
166 List<ServiceRegistration<UserNotificationDefinition>>
167 userNotificationServiceRegistrations =
168 _userNotificationDefinitionServiceRegistrations.get(portletId);
169
170 if ((userNotificationServiceRegistrations != null) &&
171 !userNotificationServiceRegistrations.isEmpty()) {
172
173 serviceRegistrations.addAll(userNotificationServiceRegistrations);
174 }
175
176 serviceRegistrations.add(serviceRegistration);
177
178 _userNotificationDefinitionServiceRegistrations.put(
179 portletId, serviceRegistrations);
180 }
181
182 private void _addUserNotificationHandler(
183 UserNotificationHandler userNotificationHandler) {
184
185 Registry registry = RegistryUtil.getRegistry();
186
187 ServiceRegistration<UserNotificationHandler> serviceRegistration =
188 registry.registerService(
189 UserNotificationHandler.class, userNotificationHandler);
190
191 _userNotificationHandlerServiceRegistrations.put(
192 userNotificationHandler, serviceRegistration);
193 }
194
195 private void _deleteUserNotificationDefinitions(String portletId) {
196 List<ServiceRegistration<UserNotificationDefinition>>
197 serviceRegistrations =
198 _userNotificationDefinitionServiceRegistrations.get(portletId);
199
200 for (ServiceRegistration<UserNotificationDefinition>
201 serviceRegistration : serviceRegistrations) {
202
203 serviceRegistration.unregister();
204 }
205 }
206
207 private void _deleteUserNotificationHandler(
208 UserNotificationHandler userNotificationHandler) {
209
210 ServiceRegistration<UserNotificationHandler> serviceRegistration =
211 _userNotificationHandlerServiceRegistrations.get(
212 userNotificationHandler);
213
214 if (serviceRegistration != null) {
215 serviceRegistration.unregister();
216 }
217 }
218
219 private UserNotificationDefinition _fetchUserNotificationDefinition(
220 String portletId, long classNameId, int notificationType) {
221
222 List<UserNotificationDefinition> userNotificationDefinitions =
223 _userNotificationDefinitions.getService(portletId);
224
225 if (userNotificationDefinitions == null) {
226 return null;
227 }
228
229 for (UserNotificationDefinition userNotificationDefinition :
230 userNotificationDefinitions) {
231
232 if ((userNotificationDefinition.getClassNameId() == classNameId) &&
233 (userNotificationDefinition.getNotificationType() ==
234 notificationType)) {
235
236 return userNotificationDefinition;
237 }
238 }
239
240 return null;
241 }
242
243 private UserNotificationFeedEntry _interpret(
244 String selector, UserNotificationEvent userNotificationEvent,
245 ServiceContext serviceContext)
246 throws PortalException {
247
248 Map<String, UserNotificationHandler> userNotificationHandlers =
249 _userNotificationHandlers.get(selector);
250
251 if (userNotificationHandlers == null) {
252 return null;
253 }
254
255 UserNotificationHandler userNotificationHandler =
256 userNotificationHandlers.get(userNotificationEvent.getType());
257
258 if (userNotificationHandler == null) {
259 if (_log.isWarnEnabled()) {
260 _log.warn("No interpreter found for " + userNotificationEvent);
261 }
262
263 return null;
264 }
265
266 return userNotificationHandler.interpret(
267 userNotificationEvent, serviceContext);
268 }
269
270 private boolean _isDeliver(
271 long userId, String selector, String portletId, long classNameId,
272 int notificationType, int deliveryType,
273 ServiceContext serviceContext)
274 throws PortalException {
275
276 Map<String, UserNotificationHandler> userNotificationHandlers =
277 _userNotificationHandlers.get(selector);
278
279 if (userNotificationHandlers == null) {
280 return false;
281 }
282
283 UserNotificationHandler userNotificationHandler =
284 userNotificationHandlers.get(portletId);
285
286 if (userNotificationHandler == null) {
287 if (deliveryType == UserNotificationDeliveryConstants.TYPE_EMAIL) {
288 return true;
289 }
290
291 return false;
292 }
293
294 return userNotificationHandler.isDeliver(
295 userId, classNameId, notificationType, deliveryType,
296 serviceContext);
297 }
298
299 private static final Log _log = LogFactoryUtil.getLog(
300 UserNotificationManagerUtil.class);
301
302 private static final UserNotificationManagerUtil _instance =
303 new UserNotificationManagerUtil();
304
305 private final ServiceTrackerMap<String, List<UserNotificationDefinition>>
306 _userNotificationDefinitions = ServiceTrackerCollections.multiValueMap(
307 UserNotificationDefinition.class, "javax.portlet.name");
308 private final ConcurrentHashMap<
309 String, List<ServiceRegistration<UserNotificationDefinition>>>
310 _userNotificationDefinitionServiceRegistrations =
311 new ConcurrentHashMap<>();
312 private final Map<String, Map<String, UserNotificationHandler>>
313 _userNotificationHandlers = new ConcurrentHashMap
314 <String, Map<String, UserNotificationHandler>>();
315 private final ServiceRegistrationMap<UserNotificationHandler>
316 _userNotificationHandlerServiceRegistrations =
317 new ServiceRegistrationMap<UserNotificationHandler>();
318 private final
319 ServiceTracker<UserNotificationHandler, UserNotificationHandler>
320 _userNotificationHandlerServiceTracker;
321
322 private class UserNotificationHandlerServiceTrackerCustomizer
323 implements ServiceTrackerCustomizer
324 <UserNotificationHandler, UserNotificationHandler> {
325
326 @Override
327 public UserNotificationHandler addingService(
328 ServiceReference<UserNotificationHandler> serviceReference) {
329
330 Registry registry = RegistryUtil.getRegistry();
331
332 UserNotificationHandler userNotificationHandler =
333 registry.getService(serviceReference);
334
335 String selector = userNotificationHandler.getSelector();
336
337 Map<String, UserNotificationHandler> userNotificationHandlers =
338 _userNotificationHandlers.get(selector);
339
340 if (userNotificationHandlers == null) {
341 userNotificationHandlers =
342 new HashMap<String, UserNotificationHandler>();
343
344 _userNotificationHandlers.put(
345 selector, userNotificationHandlers);
346 }
347
348 userNotificationHandlers.put(
349 userNotificationHandler.getPortletId(),
350 userNotificationHandler);
351
352 return userNotificationHandler;
353 }
354
355 @Override
356 public void modifiedService(
357 ServiceReference<UserNotificationHandler> serviceReference,
358 UserNotificationHandler userNotificationHandler) {
359 }
360
361 @Override
362 public void removedService(
363 ServiceReference<UserNotificationHandler> serviceReference,
364 UserNotificationHandler userNotificationHandler) {
365
366 Registry registry = RegistryUtil.getRegistry();
367
368 registry.ungetService(serviceReference);
369
370 Map<String, UserNotificationHandler> userNotificationHandlers =
371 _userNotificationHandlers.get(
372 userNotificationHandler.getSelector());
373
374 if (userNotificationHandlers == null) {
375 return;
376 }
377
378 userNotificationHandlers.remove(
379 userNotificationHandler.getPortletId());
380 }
381
382 }
383
384 }