From bb289a69fa4d292378c5c534e10985be65b2e392 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 25 Mar 2018 22:16:09 -0300 Subject: generalization for notifications processing --- .../traccar/notification/NotificatorManager.java | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/org/traccar/notification/NotificatorManager.java (limited to 'src/org/traccar/notification/NotificatorManager.java') diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java new file mode 100644 index 000000000..814c8dc1a --- /dev/null +++ b/src/org/traccar/notification/NotificatorManager.java @@ -0,0 +1,51 @@ +/* + * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.notification; + +import java.util.HashMap; +import java.util.Map; + +public class NotificatorManager { + + protected NotificatorManager() { + //not called + } + + private static final Map NOTIFICATORS = new HashMap<>(); + private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); + + static { + NOTIFICATORS.put("sms", new NotificationSms()); + NOTIFICATORS.put("mail", new NotificationMail()); + NOTIFICATORS.put("web", new NotificationWeb()); + } + + public static Notificator getNotificator(String type) { + return NOTIFICATORS.getOrDefault(type, NULL_NOTIFICATOR); + } + + public static Notificator getSms() { + return getNotificator("sms"); + } + + public static Notificator getMail() { + return getNotificator("mail"); + } + + +} + -- cgit v1.2.3 From 2891a1a4eee21e6c80b65915d35558c033503047 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 1 Apr 2018 18:22:29 -0300 Subject: generalization for notifications processing --- src/org/traccar/notification/NotificationSms.java | 4 ++-- src/org/traccar/notification/NotificatorManager.java | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/org/traccar/notification/NotificatorManager.java') diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index 34d68e174..a8eaff798 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -27,7 +27,7 @@ public final class NotificationSms extends Notificator { @Override public void sendAsync(long userId, Event event, Position position) { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmsManager() != null && user.getPhone() != null) { + if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageAsync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); @@ -38,7 +38,7 @@ public final class NotificationSms extends Notificator { public void sendSync(long userId, Event event, Position position) throws SMSException, InterruptedException { User user = Context.getPermissionsManager().getUser(userId); - if (Context.getSmsManager() != null && user.getPhone() != null) { + if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); Context.getSmsManager().sendMessageSync(user.getPhone(), NotificationFormatter.formatSmsMessage(userId, event, position), false); diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 814c8dc1a..1aa76beff 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -19,6 +19,8 @@ package org.traccar.notification; import java.util.HashMap; import java.util.Map; +import org.traccar.Context; + public class NotificatorManager { protected NotificatorManager() { @@ -29,7 +31,9 @@ public class NotificatorManager { private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); static { - NOTIFICATORS.put("sms", new NotificationSms()); + if (Context.getSmsManager() != null) { + NOTIFICATORS.put("sms", new NotificationSms()); + } NOTIFICATORS.put("mail", new NotificationMail()); NOTIFICATORS.put("web", new NotificationWeb()); } -- cgit v1.2.3 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 --- setup/default.xml | 5 +++ .../traccar/notification/NotificatorManager.java | 41 +++++++++++++++------- 2 files changed, 33 insertions(+), 13 deletions(-) (limited to 'src/org/traccar/notification/NotificatorManager.java') 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 @@ ./media + web,mail + org.traccar.notification.NotificationWeb + org.traccar.notification.NotificationSms + org.traccar.notification.NotificationMail + https://www.traccar.org/analytics/ 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 From 8ca7b2d0d9b5e18dd7e96a2cf5b1b8d8d751051a Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 07:50:10 -0300 Subject: make compatible with java 7 --- src/org/traccar/notification/NotificatorManager.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (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 4353b7914..0ffef5d05 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -54,7 +54,12 @@ public final class NotificatorManager { private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); public static Notificator getNotificator(String type) { - return INSTANCE.notificators.getOrDefault(type, NULL_NOTIFICATOR); + final Notificator n = INSTANCE.notificators.get(type); + if (n == null) { + Log.error("No notificator configured for type : " + type); + return NULL_NOTIFICATOR; + } + return n; } public static Notificator getSms() { -- cgit v1.2.3 From 4fc750b585dd6b2953b16408dd57a8ef93fdeee9 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 09:48:07 -0300 Subject: move NotificatorManager instance to Context --- src/org/traccar/Context.java | 8 ++++++++ src/org/traccar/api/resource/NotificationResource.java | 5 ++--- src/org/traccar/database/NotificationManager.java | 3 +-- src/org/traccar/notification/NotificatorManager.java | 12 +++++------- 4 files changed, 16 insertions(+), 12 deletions(-) (limited to 'src/org/traccar/notification/NotificatorManager.java') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index c9ac4ec12..a7c1817cf 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -73,6 +73,7 @@ import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.notification.EventForwarder; import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.MultiPartEventForwarder; +import org.traccar.notification.NotificatorManager; import org.traccar.reports.model.TripsConfig; import org.traccar.sms.SMSManager; import org.traccar.web.WebServer; @@ -196,6 +197,12 @@ public final class Context { return notificationManager; } + private static NotificatorManager notificatorManager; + + public static NotificatorManager getNotificatorManager() { + return notificatorManager; + } + private static VelocityEngine velocityEngine; public static VelocityEngine getVelocityEngine() { @@ -425,6 +432,7 @@ public final class Context { geofenceManager = new GeofenceManager(dataManager); calendarManager = new CalendarManager(dataManager); notificationManager = new NotificationManager(dataManager); + notificatorManager = new NotificatorManager(); Properties velocityProperties = new Properties(); velocityProperties.setProperty("file.resource.loader.path", Context.getConfig().getString("templates.rootPath", "templates") + "/"); diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 2a398f40d..830e34fc0 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -31,7 +31,6 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Typed; import org.traccar.notification.NotificationException; -import org.traccar.notification.NotificatorManager; @Path("notifications") @@ -52,8 +51,8 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - NotificatorManager.getMail().sendSync(getUserId(), new Event("test", 0), null); - NotificatorManager.getSms().sendSync(getUserId(), new Event("test", 0), null); + Context.getNotificatorManager().getMail().sendSync(getUserId(), new Event("test", 0), null); + Context.getNotificatorManager().getSms().sendSync(getUserId(), new Event("test", 0), null); return Response.noContent().build(); } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 10c76181a..a10fbf69a 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -32,7 +32,6 @@ import org.traccar.model.Event; import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Typed; -import org.traccar.notification.NotificatorManager; public class NotificationManager extends ExtendedObjectManager { @@ -90,7 +89,7 @@ public class NotificationManager extends ExtendedObjectManager { } } for (String nm : notificationMethods) { - NotificatorManager.getNotificator(nm).sendAsync(userId, event, position); + Context.getNotificatorManager().getNotificator(nm).sendAsync(userId, event, position); } } } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index 0ffef5d05..c58149847 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -26,9 +26,7 @@ import org.traccar.helper.Log; public final class NotificatorManager { - private static final NotificatorManager INSTANCE = new NotificatorManager(); - - private NotificatorManager() { + public NotificatorManager() { final String[] types = Context.getConfig().getString("notificator.types", "").split(","); for (String type : types) { final String className = Context.getConfig().getString("notificator." + type + ".class", ""); @@ -53,8 +51,8 @@ public final class NotificatorManager { private final Map notificators = new HashMap<>(); private static final Notificator NULL_NOTIFICATOR = new NotificationNull(); - public static Notificator getNotificator(String type) { - final Notificator n = INSTANCE.notificators.get(type); + public Notificator getNotificator(String type) { + final Notificator n = notificators.get(type); if (n == null) { Log.error("No notificator configured for type : " + type); return NULL_NOTIFICATOR; @@ -62,11 +60,11 @@ public final class NotificatorManager { return n; } - public static Notificator getSms() { + public Notificator getSms() { return getNotificator("sms"); } - public static Notificator getMail() { + public Notificator getMail() { return getNotificator("mail"); } -- cgit v1.2.3 From 294c399e5b260313a2d49a0fed1516116a23fbd5 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 1 Jun 2018 10:43:16 -0300 Subject: update api to support notificators --- .../traccar/api/resource/NotificationResource.java | 21 +++++++++++++++++++-- .../traccar/notification/NotificatorManager.java | 8 +++----- 2 files changed, 22 insertions(+), 7 deletions(-) (limited to 'src/org/traccar/notification/NotificatorManager.java') diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java index 830e34fc0..2347b43fa 100644 --- a/src/org/traccar/api/resource/NotificationResource.java +++ b/src/org/traccar/api/resource/NotificationResource.java @@ -21,6 +21,7 @@ import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -51,9 +52,25 @@ public class NotificationResource extends ExtendedObjectResource { @POST @Path("test") public Response testMessage() throws NotificationException, InterruptedException { - Context.getNotificatorManager().getMail().sendSync(getUserId(), new Event("test", 0), null); - Context.getNotificatorManager().getSms().sendSync(getUserId(), new Event("test", 0), null); + for (String method : Context.getNotificatorManager().getNotificatorTypes()) { + Context.getNotificatorManager().getNotificator(method).sendSync(getUserId(), new Event("test", 0), null); + } return Response.noContent().build(); } + @POST + @Path("test/{method}") + public Response testMessage(@PathParam("method") String method) throws NotificationException, InterruptedException { + Context.getNotificatorManager().getNotificator(method).sendSync(getUserId(), new Event("test", 0), null); + return Response.noContent().build(); + } + + + @GET + @Path("notificators") + public Collection getNotificators() { + return Context.getNotificatorManager().getNotificatorTypes(); + } + + } diff --git a/src/org/traccar/notification/NotificatorManager.java b/src/org/traccar/notification/NotificatorManager.java index c58149847..20c7749d2 100644 --- a/src/org/traccar/notification/NotificatorManager.java +++ b/src/org/traccar/notification/NotificatorManager.java @@ -20,6 +20,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.traccar.Context; import org.traccar.helper.Log; @@ -60,12 +61,9 @@ public final class NotificatorManager { return n; } - public Notificator getSms() { - return getNotificator("sms"); - } - public Notificator getMail() { - return getNotificator("mail"); + public Set getNotificatorTypes() { + return notificators.keySet(); } -- cgit v1.2.3