diff options
author | Abyss777 <abyss@fox5.ru> | 2018-06-27 15:55:19 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2018-06-27 15:55:19 +0500 |
commit | 14840af2abd9976b8f5634af8e77f4a7126dfac1 (patch) | |
tree | d9bd2bc190a1a67bda345cc804f3b21d20416cec /src/org/traccar/notification/NotificatorManager.java | |
parent | b70e46560359a181b0136fa1c6b0400615bfc904 (diff) | |
download | trackermap-server-14840af2abd9976b8f5634af8e77f4a7126dfac1.tar.gz trackermap-server-14840af2abd9976b8f5634af8e77f4a7126dfac1.tar.bz2 trackermap-server-14840af2abd9976b8f5634af8e77f4a7126dfac1.zip |
- Rename NotificationException to MessageException
- Simplify Notificator instantiation
- Make sms configuration more clear
- Move some defaults in code
- Some cleanup
Diffstat (limited to 'src/org/traccar/notification/NotificatorManager.java')
-rw-r--r-- | src/org/traccar/notification/NotificatorManager.java | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index bb55844f3..147de47d3 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -16,8 +16,6 @@ */ package org.traccar.notification; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -29,31 +27,40 @@ import org.traccar.model.Typed; public final class NotificatorManager { + private static final String DEFAULT_WEB_NOTIFICATOR = "org.traccar.notification.NotificationWeb"; + private static final String DEFAULT_MAIL_NOTIFICATOR = "org.traccar.notification.NotificationMail"; + private static final String DEFAULT_SMS_NOTIFICATOR = "org.traccar.notification.NotificationSms"; + + private final Map<String, Notificator> notificators = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + public NotificatorManager() { final String[] types = Context.getConfig().getString("notificator.types", "").split(","); for (String type : types) { - final String className = Context.getConfig().getString("notificator." + type + ".class", ""); - if (className.length() > 0) { - try { - final Class<Notificator> clazz = (Class<Notificator>) Class.forName(className); - try { - final Constructor<Notificator> constructor = clazz.getConstructor(new Class[]{String.class}); - notificators.put(type, constructor.newInstance(type)); - } catch (NoSuchMethodException e) { - // No constructor with String argument - notificators.put(type, clazz.newInstance()); - } - } catch (ClassNotFoundException | InstantiationException - | IllegalAccessException | InvocationTargetException e) { - Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); - } + String defaultNotificator = ""; + switch (type) { + case "web": + defaultNotificator = DEFAULT_WEB_NOTIFICATOR; + break; + case "mail": + defaultNotificator = DEFAULT_MAIL_NOTIFICATOR; + break; + case "sms": + defaultNotificator = DEFAULT_SMS_NOTIFICATOR; + break; + default: + break; + } + final String className = Context.getConfig() + .getString("notificator." + type + ".class", defaultNotificator); + try { + notificators.put(type, (Notificator) Class.forName(className).newInstance()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); } } } - private final Map<String, Notificator> notificators = new HashMap<>(); - private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); - public Notificator getNotificator(String type) { final Notificator notificator = notificators.get(type); if (notificator == null) { @@ -63,7 +70,6 @@ public final class NotificatorManager { return notificator; } - public Set<Typed> getAllNotificatorTypes() { Set<Typed> result = new HashSet<>(); for (String notificator : notificators.keySet()) { @@ -72,6 +78,4 @@ public final class NotificatorManager { return result; } - } - |