aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--setup/default.xml5
-rw-r--r--src/org/traccar/notification/NotificatorManager.java41
2 files changed, 33 insertions, 13 deletions
diff --git a/setup/default.xml b/setup/default.xml
index 1788cb901..123f67572 100644
--- a/setup/default.xml
+++ b/setup/default.xml
@@ -27,6 +27,11 @@
<entry key='media.path'>./media</entry>
+ <entry key='notificator.types'>web,mail</entry>
+ <entry key='notificator.web.class'>org.traccar.notification.NotificationWeb</entry>
+ <entry key='notificator.sms.class'>org.traccar.notification.NotificationSms</entry>
+ <entry key='notificator.mail.class'>org.traccar.notification.NotificationMail</entry>
+
<entry key='server.statistics'>https://www.traccar.org/analytics/</entry>
<!-- DATABASE CONFIG -->
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<String, Notificator> 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<Notificator> c = (Class<Notificator>) Class.forName(className);
+ try {
+ final Constructor<Notificator> 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<String, Notificator> 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() {