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 | |
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')
-rw-r--r-- | src/org/traccar/notification/MessageException.java (renamed from src/org/traccar/notification/NotificationException.java) | 4 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationFormatter.java | 7 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 4 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationSms.java | 22 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificationWeb.java | 3 | ||||
-rw-r--r-- | src/org/traccar/notification/Notificator.java | 6 | ||||
-rw-r--r-- | src/org/traccar/notification/NotificatorManager.java | 50 |
7 files changed, 46 insertions, 50 deletions
diff --git a/src/org/traccar/notification/NotificationException.java b/src/org/traccar/notification/MessageException.java index 525e5ee37..ce4b9f6ee 100644 --- a/src/org/traccar/notification/NotificationException.java +++ b/src/org/traccar/notification/MessageException.java @@ -16,9 +16,9 @@ */ package org.traccar.notification; -public class NotificationException extends Exception { +public class MessageException extends Exception { - public NotificationException(Throwable cause) { + public MessageException(Throwable cause) { super(cause); } diff --git a/src/org/traccar/notification/NotificationFormatter.java b/src/org/traccar/notification/NotificationFormatter.java index a1abdd0cc..c011403c5 100644 --- a/src/org/traccar/notification/NotificationFormatter.java +++ b/src/org/traccar/notification/NotificationFormatter.java @@ -89,17 +89,14 @@ public final class NotificationFormatter { } public static FullMessage formatFullMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("message.full.templatesPath", "full"); VelocityContext velocityContext = prepareContext(userId, event, position); - String formattedMessage = formatMessage(velocityContext, userId, event, position, templatePath); + String formattedMessage = formatMessage(velocityContext, userId, event, position, "full"); return new FullMessage((String) velocityContext.get("subject"), formattedMessage); } public static String formatShortMessage(long userId, Event event, Position position) { - String templatePath = Context.getConfig().getString("message.short.templatesPath", "short"); - - return formatMessage(null, userId, event, position, templatePath); + return formatMessage(null, userId, event, position, "short"); } private static String formatMessage(VelocityContext vc, Long userId, Event event, Position position, diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index 808acca76..c2ee67299 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -82,7 +82,7 @@ public final class NotificationMail extends Notificator { } @Override - public void sendSync(long userId, Event event, Position position) throws NotificationException { + public void sendSync(long userId, Event event, Position position) throws MessageException { User user = Context.getPermissionsManager().getUser(userId); Properties properties = null; @@ -125,7 +125,7 @@ public final class NotificationMail extends Notificator { transport.close(); } } catch (MessagingException e) { - throw new NotificationException(e); + throw new MessageException(e); } } diff --git a/src/org/traccar/notification/NotificationSms.java b/src/org/traccar/notification/NotificationSms.java index dd3304d85..ed651ac11 100644 --- a/src/org/traccar/notification/NotificationSms.java +++ b/src/org/traccar/notification/NotificationSms.java @@ -20,41 +20,37 @@ import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.model.User; -import org.traccar.sms.SMSManager; -import org.traccar.sms.SMSException; +import org.traccar.sms.SmsManager; public final class NotificationSms extends Notificator { - private final SMSManager sms; + private final SmsManager smsManager; - public NotificationSms(String methodId) throws ClassNotFoundException, - InstantiationException, IllegalAccessException { - final String smsClass = Context.getConfig().getString("notificator." + methodId + ".sms.class", ""); + public NotificationSms() throws ClassNotFoundException, InstantiationException, IllegalAccessException { + final String smsClass = Context.getConfig().getString("notificator.sms.manager.class", ""); if (smsClass.length() > 0) { - sms = (SMSManager) Class.forName(smsClass).newInstance(); + smsManager = (SmsManager) Class.forName(smsClass).newInstance(); } else { - sms = Context.getSmsManager(); + smsManager = Context.getSmsManager(); } } - @Override public void sendAsync(long userId, Event event, Position position) { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - sms.sendMessageAsync(user.getPhone(), + smsManager.sendMessageAsync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } @Override - public void sendSync(long userId, Event event, Position position) throws SMSException, - InterruptedException { + public void sendSync(long userId, Event event, Position position) throws MessageException, InterruptedException { final User user = Context.getPermissionsManager().getUser(userId); if (user.getPhone() != null) { Context.getStatisticsManager().registerSms(); - sms.sendMessageSync(user.getPhone(), + smsManager.sendMessageSync(user.getPhone(), NotificationFormatter.formatShortMessage(userId, event, position), false); } } diff --git a/src/org/traccar/notification/NotificationWeb.java b/src/org/traccar/notification/NotificationWeb.java index 8e04fc2c4..afc401d24 100644 --- a/src/org/traccar/notification/NotificationWeb.java +++ b/src/org/traccar/notification/NotificationWeb.java @@ -23,8 +23,7 @@ import org.traccar.model.Position; public final class NotificationWeb extends Notificator { @Override - public void sendSync(long userId, Event event, Position position) throws NotificationException, - InterruptedException { + public void sendSync(long userId, Event event, Position position) { Context.getConnectionManager().updateEvent(userId, event); } diff --git a/src/org/traccar/notification/Notificator.java b/src/org/traccar/notification/Notificator.java index 09f98d3ad..d912b445d 100644 --- a/src/org/traccar/notification/Notificator.java +++ b/src/org/traccar/notification/Notificator.java @@ -27,14 +27,14 @@ public abstract class Notificator { public void run() { try { sendSync(userId, event, position); - } catch (NotificationException | InterruptedException error) { + } catch (MessageException | InterruptedException error) { Log.warning(error); } } }).start(); } - public abstract void sendSync(long userId, Event event, Position position) - throws NotificationException, InterruptedException; + throws MessageException, InterruptedException; + } 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; } - } - |