From 9339c709e5a6d521fd0bdd4b1221b909c0a888d6 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 9 Apr 2018 21:20:15 -0300 Subject: configuration support for notificators --- .../traccar/notification/NotificatorManager.java | 41 +++++++++++++++------- 1 file changed, 28 insertions(+), 13 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 1aa76beff..4353b7914 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -16,30 +16,45 @@ */ package org.traccar.notification; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.traccar.Context; +import org.traccar.helper.Log; -public class NotificatorManager { +public final class NotificatorManager { - protected NotificatorManager() { - //not called - } - - private static final Map NOTIFICATORS = new HashMap<>(); - private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + private static final NotificatorManager INSTANCE = new NotificatorManager(); - static { - if (Context.getSmsManager() != null) { - NOTIFICATORS.put("sms", new NotificationSms()); + private 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 c = (Class) Class.forName(className); + try { + final Constructor constructor = c.getConstructor(new Class[]{String.class}); + notificators.put(type, constructor.newInstance(type)); + } catch (NoSuchMethodException e) { + // No constructor with String argument + notificators.put(type, c.newInstance()); + } + } catch (ClassNotFoundException | InstantiationException + | IllegalAccessException | InvocationTargetException e) { + Log.error("Unable to load notificator class for " + type + " " + className + " " + e.getMessage()); + } + } } - NOTIFICATORS.put("mail", new NotificationMail()); - NOTIFICATORS.put("web", new NotificationWeb()); } + private final Map notificators = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + public static Notificator getNotificator(String type) { - return NOTIFICATORS.getOrDefault(type, NULL_NOTIFICATOR); + return INSTANCE.notificators.getOrDefault(type, NULL_NOTIFICATOR); } public static Notificator getSms() { -- cgit v1.2.3