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