aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-06-09 17:56:37 -0700
committerAnton Tananaev <anton@traccar.org>2022-06-09 17:56:37 -0700
commitadafc5f6130854dd88c191dd04489073419ee41d (patch)
treee101d03069894f9a2fbfd388d9b3f93424cbe4d0
parent29b5d05cfd66a9f51b7ce272c1fb07077bc715b2 (diff)
downloadtrackermap-server-adafc5f6130854dd88c191dd04489073419ee41d.tar.gz
trackermap-server-adafc5f6130854dd88c191dd04489073419ee41d.tar.bz2
trackermap-server-adafc5f6130854dd88c191dd04489073419ee41d.zip
Remove SMS from context
-rw-r--r--src/main/java/org/traccar/BaseProtocol.java18
-rw-r--r--src/main/java/org/traccar/Context.java15
-rw-r--r--src/main/java/org/traccar/MainModule.java12
-rw-r--r--src/main/java/org/traccar/ServerManager.java6
-rw-r--r--src/main/java/org/traccar/config/Keys.java8
-rw-r--r--src/main/java/org/traccar/notification/NotificatorManager.java56
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorSms.java24
7 files changed, 74 insertions, 65 deletions
diff --git a/src/main/java/org/traccar/BaseProtocol.java b/src/main/java/org/traccar/BaseProtocol.java
index ec1933dc8..d19fc307e 100644
--- a/src/main/java/org/traccar/BaseProtocol.java
+++ b/src/main/java/org/traccar/BaseProtocol.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2022 Anton Tananaev (anton@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,10 @@ import io.netty.channel.Channel;
import io.netty.handler.codec.string.StringEncoder;
import org.traccar.helper.DataConverter;
import org.traccar.model.Command;
+import org.traccar.sms.SmsManager;
+import javax.annotation.Nullable;
+import javax.inject.Inject;
import java.net.SocketAddress;
import java.util.Arrays;
import java.util.Collection;
@@ -37,6 +40,8 @@ public abstract class BaseProtocol implements Protocol {
private final Set<String> supportedTextCommands = new HashSet<>();
private final List<TrackerConnector> connectorList = new LinkedList<>();
+ private SmsManager smsManager;
+
private StringProtocolEncoder textCommandEncoder = null;
public static String nameFromClass(Class<?> clazz) {
@@ -48,6 +53,11 @@ public abstract class BaseProtocol implements Protocol {
name = nameFromClass(getClass());
}
+ @Inject
+ public void setSmsManager(@Nullable SmsManager smsManager) {
+ this.smsManager = smsManager;
+ }
+
@Override
public String getName() {
return name;
@@ -111,13 +121,13 @@ public abstract class BaseProtocol implements Protocol {
@Override
public void sendTextCommand(String destAddress, Command command) throws Exception {
- if (Context.getSmsManager() != null) {
+ if (smsManager != null) {
if (command.getType().equals(Command.TYPE_CUSTOM)) {
- Context.getSmsManager().sendMessage(destAddress, command.getString(Command.KEY_DATA), true);
+ smsManager.sendMessage(destAddress, command.getString(Command.KEY_DATA), true);
} else if (supportedTextCommands.contains(command.getType()) && textCommandEncoder != null) {
String encodedCommand = (String) textCommandEncoder.encodeCommand(command);
if (encodedCommand != null) {
- Context.getSmsManager().sendMessage(destAddress, encodedCommand, true);
+ smsManager.sendMessage(destAddress, encodedCommand, true);
} else {
throw new RuntimeException("Failed to encode command");
}
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java
index 3e4cc1bb9..5011764c6 100644
--- a/src/main/java/org/traccar/Context.java
+++ b/src/main/java/org/traccar/Context.java
@@ -43,9 +43,6 @@ import org.traccar.notification.EventForwarder;
import org.traccar.notification.NotificatorManager;
import org.traccar.session.ConnectionManager;
import org.traccar.session.cache.CacheManager;
-import org.traccar.sms.HttpSmsClient;
-import org.traccar.sms.SmsManager;
-import org.traccar.sms.SnsSmsClient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
@@ -153,12 +150,6 @@ public final class Context {
return eventForwarder;
}
- private static SmsManager smsManager;
-
- public static SmsManager getSmsManager() {
- return smsManager;
- }
-
private static class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
@Override
@@ -207,12 +198,6 @@ public final class Context {
connectionManager = new ConnectionManager();
- if (config.hasKey(Keys.SMS_HTTP_URL)) {
- smsManager = new HttpSmsClient();
- } else if (config.hasKey(Keys.SMS_AWS_REGION)) {
- smsManager = new SnsSmsClient();
- }
-
initEventsModule();
if (config.hasKey(Keys.EVENT_FORWARD_URL)) {
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java
index 9ec73d819..f6621a18e 100644
--- a/src/main/java/org/traccar/MainModule.java
+++ b/src/main/java/org/traccar/MainModule.java
@@ -57,7 +57,9 @@ import org.traccar.geolocation.UnwiredGeolocationProvider;
import org.traccar.handler.GeocoderHandler;
import org.traccar.handler.GeolocationHandler;
import org.traccar.handler.SpeedLimitHandler;
+import org.traccar.sms.HttpSmsClient;
import org.traccar.sms.SmsManager;
+import org.traccar.sms.SnsSmsClient;
import org.traccar.speedlimit.OverpassSpeedLimitProvider;
import org.traccar.speedlimit.SpeedLimitProvider;
import org.traccar.storage.Storage;
@@ -115,9 +117,15 @@ public class MainModule extends AbstractModule {
return Context.getDeviceManager();
}
+ @Singleton
@Provides
- public static SmsManager provideSmsManager() {
- return Context.getSmsManager();
+ public static SmsManager provideSmsManager(Config config) {
+ if (config.hasKey(Keys.SMS_HTTP_URL)) {
+ return new HttpSmsClient();
+ } else if (config.hasKey(Keys.SMS_AWS_REGION)) {
+ return new SnsSmsClient();
+ }
+ return null;
}
@Singleton
diff --git a/src/main/java/org/traccar/ServerManager.java b/src/main/java/org/traccar/ServerManager.java
index a6bb6888c..f4f6e1ba4 100644
--- a/src/main/java/org/traccar/ServerManager.java
+++ b/src/main/java/org/traccar/ServerManager.java
@@ -15,11 +15,13 @@
*/
package org.traccar;
+import com.google.inject.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.config.Keys;
import org.traccar.helper.ClassScanner;
+import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.net.BindException;
@@ -38,10 +40,12 @@ public class ServerManager implements LifecycleObject {
private final List<TrackerConnector> connectorList = new LinkedList<>();
private final Map<String, BaseProtocol> protocolList = new ConcurrentHashMap<>();
- public ServerManager() throws IOException, URISyntaxException, ReflectiveOperationException {
+ @Inject
+ public ServerManager(Injector injector) throws IOException, URISyntaxException, ReflectiveOperationException {
for (Class<?> protocolClass : ClassScanner.findSubclasses(BaseProtocol.class, "org.traccar.protocol")) {
if (Context.getConfig().hasKey(Keys.PROTOCOL_PORT.withPrefix(BaseProtocol.nameFromClass(protocolClass)))) {
BaseProtocol protocol = (BaseProtocol) protocolClass.getDeclaredConstructor().newInstance();
+ injector.injectMembers(protocol);
connectorList.addAll(protocol.getConnectorList());
protocolList.put(protocol.getName(), protocol);
}
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java
index ba576397b..d53245c65 100644
--- a/src/main/java/org/traccar/config/Keys.java
+++ b/src/main/java/org/traccar/config/Keys.java
@@ -754,6 +754,14 @@ public final class Keys {
Collections.singletonList(KeyType.GLOBAL));
/**
+ * Enabled notification options. Comma-separated string is expected.
+ * Example: web,mail,sms
+ */
+ public static final ConfigKey<String> NOTIFICATOR_TYPES = new ConfigKey<>(
+ "notificator.types",
+ Collections.singletonList(KeyType.GLOBAL));
+
+ /**
* Traccar notification API key.
*/
public static final ConfigKey<String> NOTIFICATOR_TRACCAR_KEY = new ConfigKey<>(
diff --git a/src/main/java/org/traccar/notification/NotificatorManager.java b/src/main/java/org/traccar/notification/NotificatorManager.java
index dfd8cd3d6..9705377b4 100644
--- a/src/main/java/org/traccar/notification/NotificatorManager.java
+++ b/src/main/java/org/traccar/notification/NotificatorManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 - 2020 Anton Tananaev (anton@traccar.org)
+ * Copyright 2018 - 2022 Anton Tananaev (anton@traccar.org)
* Copyright 2018 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,6 +24,8 @@ import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.Context;
+import org.traccar.Main;
+import org.traccar.config.Keys;
import org.traccar.model.Typed;
import org.traccar.notificators.NotificatorFirebase;
import org.traccar.notificators.NotificatorMail;
@@ -39,45 +41,25 @@ public final class NotificatorManager {
private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorManager.class);
- private static final Notificator NULL_NOTIFICATOR = new NotificatorNull();
+ private static final Map<String, Class<? extends Notificator>> NOTIFICATORS_ALL = Map.of(
+ "web", NotificatorWeb.class,
+ "mail", NotificatorMail.class,
+ "sms", NotificatorSms.class,
+ "firebase", NotificatorFirebase.class,
+ "traccar", NotificatorTraccar.class,
+ "telegram", NotificatorTelegram.class,
+ "pushover", NotificatorPushover.class);
private final Map<String, Notificator> notificators = new HashMap<>();
public NotificatorManager() {
- final String[] types = Context.getConfig().getString("notificator.types", "").split(",");
- for (String type : types) {
- String defaultNotificator = "";
- switch (type) {
- case "web":
- defaultNotificator = NotificatorWeb.class.getCanonicalName();
- break;
- case "mail":
- defaultNotificator = NotificatorMail.class.getCanonicalName();
- break;
- case "sms":
- defaultNotificator = NotificatorSms.class.getCanonicalName();
- break;
- case "firebase":
- defaultNotificator = NotificatorFirebase.class.getCanonicalName();
- break;
- case "traccar":
- defaultNotificator = NotificatorTraccar.class.getCanonicalName();
- break;
- case "telegram":
- defaultNotificator = NotificatorTelegram.class.getCanonicalName();
- break;
- case "pushover":
- defaultNotificator = NotificatorPushover.class.getCanonicalName();
- 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) {
- LOGGER.warn("Unable to load notificator class for " + type + " " + className + " " + e.getMessage());
+ String types = Context.getConfig().getString(Keys.NOTIFICATOR_TYPES);
+ if (types != null) {
+ for (String type : types.split(",")) {
+ var notificatorClass = NOTIFICATORS_ALL.get(type);
+ if (notificatorClass != null) {
+ notificators.put(type, Main.getInjector().getInstance(notificatorClass));
+ }
}
}
}
@@ -86,7 +68,7 @@ public final class NotificatorManager {
final Notificator notificator = notificators.get(type);
if (notificator == null) {
LOGGER.warn("No notificator configured for type : " + type);
- return NULL_NOTIFICATOR;
+ return new NotificatorNull();
}
return notificator;
}
diff --git a/src/main/java/org/traccar/notificators/NotificatorSms.java b/src/main/java/org/traccar/notificators/NotificatorSms.java
index f35b797cd..f4d1de0cb 100644
--- a/src/main/java/org/traccar/notificators/NotificatorSms.java
+++ b/src/main/java/org/traccar/notificators/NotificatorSms.java
@@ -16,8 +16,6 @@
*/
package org.traccar.notificators;
-import org.traccar.Context;
-import org.traccar.Main;
import org.traccar.database.StatisticsManager;
import org.traccar.model.Event;
import org.traccar.model.Position;
@@ -26,16 +24,30 @@ import org.traccar.notification.MessageException;
import org.traccar.notification.NotificationFormatter;
import org.traccar.notification.NotificationMessage;
import org.traccar.session.cache.CacheManager;
+import org.traccar.sms.SmsManager;
-public final class NotificatorSms implements Notificator {
+import javax.inject.Inject;
+
+public class NotificatorSms implements Notificator {
+
+ private final SmsManager smsManager;
+ private final CacheManager cacheManager;
+ private final StatisticsManager statisticsManager;
+
+ @Inject
+ public NotificatorSms(SmsManager smsManager, CacheManager cacheManager, StatisticsManager statisticsManager) {
+ this.smsManager = smsManager;
+ this.cacheManager = cacheManager;
+ this.statisticsManager = statisticsManager;
+ }
@Override
public void send(User user, Event event, Position position) throws MessageException, InterruptedException {
if (user.getPhone() != null) {
NotificationMessage shortMessage = NotificationFormatter.formatMessage(
- Main.getInjector().getInstance(CacheManager.class), user, event, position, "short");
- Main.getInjector().getInstance(StatisticsManager.class).registerSms();
- Context.getSmsManager().sendMessage(user.getPhone(), shortMessage.getBody(), false);
+ cacheManager, user, event, position, "short");
+ statisticsManager.registerSms();
+ smsManager.sendMessage(user.getPhone(), shortMessage.getBody(), false);
}
}