From 14840af2abd9976b8f5634af8e77f4a7126dfac1 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 27 Jun 2018 15:55:19 +0500 Subject: - Rename NotificationException to MessageException - Simplify Notificator instantiation - Make sms configuration more clear - Move some defaults in code - Some cleanup --- .../traccar/notification/NotificatorManager.java | 50 ++++++++++++---------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'src/org/traccar/notification/NotificatorManager.java') 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 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 clazz = (Class) Class.forName(className); - try { - final Constructor 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 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 getAllNotificatorTypes() { Set result = new HashSet<>(); for (String notificator : notificators.keySet()) { @@ -72,6 +78,4 @@ public final class NotificatorManager { return result; } - } - -- cgit v1.2.3