From f1bba38ddccbcdfd35201cd93e2a518efdd86d1a Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Thu, 17 Jul 2014 00:32:54 +0200 Subject: [PATCH] Fix for MID-1950. --- .../impl/NotificationManagerImpl.java | 5 ++++- .../notifications/impl/NotificationsUtil.java | 21 +++++++------------ .../impl/api/transports/MailTransport.java | 8 +++---- .../api/transports/SimpleSmsTransport.java | 6 +++--- 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationManagerImpl.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationManagerImpl.java index cf4bf59e4d4..33ca4e8c683 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationManagerImpl.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationManagerImpl.java @@ -131,7 +131,10 @@ public void processEvent(Event event, Task task, OperationResult result) { LOGGER.trace("NotificationManager processing event " + event); } - SystemConfigurationType systemConfigurationType = NotificationsUtil.getSystemConfiguration(cacheRepositoryService, result).asObjectable(); + SystemConfigurationType systemConfigurationType = NotificationsUtil.getSystemConfiguration(cacheRepositoryService, result); + if (systemConfigurationType == null) { // something really wrong happened (or we are doing initial import of objects) + return; + } if (systemConfigurationType.getNotificationConfiguration() == null) { if (LOGGER.isTraceEnabled()) { LOGGER.trace("No notification configuration in repository, exiting the change listener."); diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationsUtil.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationsUtil.java index 8d7b680ff33..4fb46ef319e 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationsUtil.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/NotificationsUtil.java @@ -51,22 +51,15 @@ public class NotificationsUtil { @Qualifier("cacheRepositoryService") private RepositoryService cacheRepositoryService; - public static PrismObject getSystemConfiguration(RepositoryService repositoryService, OperationResult result) { - PrismObject systemConfiguration; + // beware, may return null if there's any problem getting sysconfig (e.g. during initial import) + public static SystemConfigurationType getSystemConfiguration(RepositoryService repositoryService, OperationResult result) { try { - systemConfiguration = repositoryService.getObject(SystemConfigurationType.class, SystemObjectsType.SYSTEM_CONFIGURATION.value(), - null, result); - } catch (ObjectNotFoundException e) { - LoggingUtils.logException(LOGGER, "Couldn't get system configuration", e); - throw new SystemException("Couldn't get system configuration", e); - } catch (SchemaException e) { - LoggingUtils.logException(LOGGER, "Couldn't get system configuration", e); - throw new SystemException("Couldn't get system configuration", e); - } - if (systemConfiguration == null) { - throw new SystemException("Couldn't get system configuration"); + return repositoryService.getObject(SystemConfigurationType.class, SystemObjectsType.SYSTEM_CONFIGURATION.value(), + null, result).asObjectable(); + } catch (ObjectNotFoundException|SchemaException e) { + LoggingUtils.logException(LOGGER, "Notification(s) couldn't be processed, because the system configuration couldn't be retrieved", e); + return null; } - return systemConfiguration; } public static String getResourceNameFromRepo(RepositoryService repositoryService, String oid, OperationResult result) { diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/MailTransport.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/MailTransport.java index 0b28c1137fc..c34249f560f 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/MailTransport.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/MailTransport.java @@ -85,16 +85,16 @@ public void send(Message mailMessage, String transportName, Task task, Operation result.addCollectionOfSerializablesAsParam("mailMessage recipient(s)", mailMessage.getTo()); result.addParam("mailMessage subject", mailMessage.getSubject()); - PrismObject systemConfiguration = NotificationsUtil.getSystemConfiguration(cacheRepositoryService, new OperationResult("dummy")); - if (systemConfiguration == null || systemConfiguration.asObjectable().getNotificationConfiguration() == null - || systemConfiguration.asObjectable().getNotificationConfiguration().getMail() == null) { + SystemConfigurationType systemConfiguration = NotificationsUtil.getSystemConfiguration(cacheRepositoryService, new OperationResult("dummy")); + if (systemConfiguration == null || systemConfiguration.getNotificationConfiguration() == null + || systemConfiguration.getNotificationConfiguration().getMail() == null) { String msg = "No notifications are configured. Mail notification to " + mailMessage.getTo() + " will not be sent."; LOGGER.warn(msg) ; result.recordWarning(msg); return; } - MailConfigurationType mailConfigurationType = systemConfiguration.asObjectable().getNotificationConfiguration().getMail(); + MailConfigurationType mailConfigurationType = systemConfiguration.getNotificationConfiguration().getMail(); if (mailConfigurationType.getRedirectToFile() != null) { try { TransportUtil.appendToFile(mailConfigurationType.getRedirectToFile(), formatToFile(mailMessage)); diff --git a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/SimpleSmsTransport.java b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/SimpleSmsTransport.java index b2b36664535..ce586bbf173 100644 --- a/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/SimpleSmsTransport.java +++ b/model/notifications-impl/src/main/java/com/evolveum/midpoint/notifications/impl/api/transports/SimpleSmsTransport.java @@ -100,8 +100,8 @@ public void send(Message message, String transportName, Task task, OperationResu result.addCollectionOfSerializablesAsParam("message recipient(s)", message.getTo()); result.addParam("message subject", message.getSubject()); - PrismObject systemConfiguration = NotificationsUtil.getSystemConfiguration(cacheRepositoryService, new OperationResult("dummy")); - if (systemConfiguration == null || systemConfiguration.asObjectable().getNotificationConfiguration() == null) { + SystemConfigurationType systemConfiguration = NotificationsUtil.getSystemConfiguration(cacheRepositoryService, new OperationResult("dummy")); + if (systemConfiguration == null || systemConfiguration.getNotificationConfiguration() == null) { String msg = "No notifications are configured. SMS notification to " + message.getTo() + " will not be sent."; LOGGER.warn(msg) ; result.recordWarning(msg); @@ -110,7 +110,7 @@ public void send(Message message, String transportName, Task task, OperationResu String smsConfigName = transportName.length() > NAME.length() ? transportName.substring(NAME.length() + 1) : null; // after "sms:" SmsConfigurationType found = null; - for (SmsConfigurationType smsConfigurationType: systemConfiguration.asObjectable().getNotificationConfiguration().getSms()) { + for (SmsConfigurationType smsConfigurationType: systemConfiguration.getNotificationConfiguration().getSms()) { if ((smsConfigName == null && smsConfigurationType.getName() == null) || (smsConfigName != null && smsConfigName.equals(smsConfigurationType.getName()))) { found = smsConfigurationType; break;