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
147 private void _addUserNotificationDefinition(
148 String portletId,
149 UserNotificationDefinition userNotificationDefinition) {
150
151 Registry registry = RegistryUtil.getRegistry();
152
153 Map<String, Object> properties = new HashMap<>();
154
155 properties.put("javax.portlet.name", portletId);
156
157 ServiceRegistration<UserNotificationDefinition> serviceRegistration =
158 registry.registerService(
159 UserNotificationDefinition.class, userNotificationDefinition,
160 properties);
161
162 List<ServiceRegistration<UserNotificationDefinition>>
163 serviceRegistrations = new ArrayList<>();
164
165 List<ServiceRegistration<UserNotificationDefinition>>
166 userNotificationServiceRegistrations =
167 _userNotificationDefinitionServiceRegistrations.get(portletId);
168
169 if ((userNotificationServiceRegistrations != null) &&
170 !userNotificationServiceRegistrations.isEmpty()) {
171
172 serviceRegistrations.addAll(userNotificationServiceRegistrations);
173 }
174
175 serviceRegistrations.add(serviceRegistration);
176
177 _userNotificationDefinitionServiceRegistrations.put(
178 portletId, serviceRegistrations);
179 }
180
181 private void _addUserNotificationHandler(
182 UserNotificationHandler userNotificationHandler) {
183
184 Registry registry = RegistryUtil.getRegistry();
185
186 ServiceRegistration<UserNotificationHandler> serviceRegistration =
187 registry.registerService(
188 UserNotificationHandler.class, userNotificationHandler);
189
190 _userNotificationHandlerServiceRegistrations.put(
191 userNotificationHandler, serviceRegistration);
192 }
193
194 private void _deleteUserNotificationDefinitions(String portletId) {
195 List<ServiceRegistration<UserNotificationDefinition>>
196 serviceRegistrations =
197 _userNotificationDefinitionServiceRegistrations.get(portletId);
198
199 for (ServiceRegistration<UserNotificationDefinition>
200 serviceRegistration : serviceRegistrations) {
201
202 serviceRegistration.unregister();
203 }
204 }
205
206 private void _deleteUserNotificationHandler(
207 UserNotificationHandler userNotificationHandler) {
208
209 ServiceRegistration<UserNotificationHandler> serviceRegistration =
210 _userNotificationHandlerServiceRegistrations.get(
211 userNotificationHandler);
212
213 if (serviceRegistration != null) {
214 serviceRegistration.unregister();
215 }
216 }
217
218 private UserNotificationDefinition _fetchUserNotificationDefinition(
219 String portletId, long classNameId, int notificationType) {
220
221 List<UserNotificationDefinition> userNotificationDefinitions =
222 _userNotificationDefinitions.getService(portletId);
223
224 if (userNotificationDefinitions == null) {
225 return null;
226 }
227
228 for (UserNotificationDefinition userNotificationDefinition :
229 userNotificationDefinitions) {
230
231 if ((userNotificationDefinition.getClassNameId() == classNameId) &&
232 (userNotificationDefinition.getNotificationType() ==
233 notificationType)) {
234
235 return userNotificationDefinition;
236 }
237 }
238
239 return null;
240 }
241
242 private UserNotificationFeedEntry _interpret(
243 String selector, UserNotificationEvent userNotificationEvent,
244 ServiceContext serviceContext)
245 throws PortalException {
246
247 Map<String, UserNotificationHandler> userNotificationHandlers =
248 _userNotificationHandlers.get(selector);
249
250 if (userNotificationHandlers == null) {
251 return null;
252 }
253
254 UserNotificationHandler userNotificationHandler =
255 userNotificationHandlers.get(userNotificationEvent.getType());
256
257 if (userNotificationHandler == null) {
258 if (_log.isWarnEnabled()) {
259 _log.warn("No interpreter found for " + userNotificationEvent);
260 }
261
262 return null;
263 }
264
265 return userNotificationHandler.interpret(
266 userNotificationEvent, serviceContext);
267 }
268
269 private boolean _isDeliver(
270 long userId, String selector, String portletId, long classNameId,
271 int notificationType, int deliveryType,
272 ServiceContext serviceContext)
273 throws PortalException {
274
275 Map<String, UserNotificationHandler> userNotificationHandlers =
276 _userNotificationHandlers.get(selector);
277
278 if (userNotificationHandlers == null) {
279 return false;
280 }
281
282 UserNotificationHandler userNotificationHandler =
283 userNotificationHandlers.get(portletId);
284
285 if (userNotificationHandler == null) {
286 if (deliveryType == UserNotificationDeliveryConstants.TYPE_EMAIL) {
287 return true;
288 }
289
290 return false;
291 }
292
293 return userNotificationHandler.isDeliver(
294 userId, classNameId, notificationType, deliveryType,
295 serviceContext);
296 }
297
298 private static final Log _log = LogFactoryUtil.getLog(
299 UserNotificationManagerUtil.class);
300
301 private static final UserNotificationManagerUtil _instance =
302 new UserNotificationManagerUtil();
303
304 private final ServiceTrackerMap<String, List<UserNotificationDefinition>>
305 _userNotificationDefinitions =
306 ServiceTrackerCollections.openMultiValueMap(
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 private final ServiceRegistrationMap<UserNotificationHandler>
315 _userNotificationHandlerServiceRegistrations =
316 new ServiceRegistrationMapImpl<>();
317 private final
318 ServiceTracker<UserNotificationHandler, UserNotificationHandler>
319 _userNotificationHandlerServiceTracker;
320
321 private class UserNotificationHandlerServiceTrackerCustomizer
322 implements ServiceTrackerCustomizer
323 <UserNotificationHandler, UserNotificationHandler> {
324
325 @Override
326 public UserNotificationHandler addingService(
327 ServiceReference<UserNotificationHandler> serviceReference) {
328
329 Registry registry = RegistryUtil.getRegistry();
330
331 UserNotificationHandler userNotificationHandler =
332 registry.getService(serviceReference);
333
334 String selector = userNotificationHandler.getSelector();
335
336 Map<String, UserNotificationHandler> userNotificationHandlers =
337 _userNotificationHandlers.get(selector);
338
339 if (userNotificationHandlers == null) {
340 userNotificationHandlers = new HashMap<>();
341
342 _userNotificationHandlers.put(
343 selector, userNotificationHandlers);
344 }
345
346 userNotificationHandlers.put(
347 userNotificationHandler.getPortletId(),
348 userNotificationHandler);
349
350 return userNotificationHandler;
351 }
352
353 @Override
354 public void modifiedService(
355 ServiceReference<UserNotificationHandler> serviceReference,
356 UserNotificationHandler userNotificationHandler) {
357 }
358
359 @Override
360 public void removedService(
361 ServiceReference<UserNotificationHandler> serviceReference,
362 UserNotificationHandler userNotificationHandler) {
363
364 Registry registry = RegistryUtil.getRegistry();
365
366 registry.ungetService(serviceReference);
367
368 Map<String, UserNotificationHandler> userNotificationHandlers =
369 _userNotificationHandlers.get(
370 userNotificationHandler.getSelector());
371
372 if (userNotificationHandlers == null) {
373 return;
374 }
375
376 userNotificationHandlers.remove(
377 userNotificationHandler.getPortletId());
378 }
379
380 }
381
382 }