From ee30e6e63f110476f557ed4ca8a081eb5b589101 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 16 Jul 2022 20:47:59 -0700 Subject: Parameter to enable protocols (fix #3991) --- src/main/java/org/traccar/ServerManager.java | 18 ++++++++++++++---- src/main/java/org/traccar/config/Keys.java | 10 ++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/ServerManager.java b/src/main/java/org/traccar/ServerManager.java index 8d6e615dc..57afb01fd 100644 --- a/src/main/java/org/traccar/ServerManager.java +++ b/src/main/java/org/traccar/ServerManager.java @@ -28,9 +28,12 @@ import java.io.IOException; import java.net.BindException; import java.net.ConnectException; import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @Singleton @@ -44,11 +47,18 @@ public class ServerManager implements LifecycleObject { @Inject public ServerManager( Injector injector, Config config) throws IOException, URISyntaxException, ReflectiveOperationException { + Set enabledProtocols = null; + if (config.hasKey(Keys.PROTOCOLS_ENABLE)) { + enabledProtocols = new HashSet<>(Arrays.asList(config.getString(Keys.PROTOCOLS_ENABLE).split("[, ]"))); + } for (Class protocolClass : ClassScanner.findSubclasses(BaseProtocol.class, "org.traccar.protocol")) { - if (config.hasKey(Keys.PROTOCOL_PORT.withPrefix(BaseProtocol.nameFromClass(protocolClass)))) { - BaseProtocol protocol = (BaseProtocol) injector.getInstance(protocolClass); - connectorList.addAll(protocol.getConnectorList()); - protocolList.put(protocol.getName(), protocol); + String protocolName = BaseProtocol.nameFromClass(protocolClass); + if (enabledProtocols == null || enabledProtocols.contains(protocolName)) { + if (config.hasKey(Keys.PROTOCOL_PORT.withPrefix(protocolName))) { + BaseProtocol protocol = (BaseProtocol) injector.getInstance(protocolClass); + 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 1217ab94c..2190f82f7 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1210,6 +1210,16 @@ public final class Keys { "time.override", List.of(KeyType.CONFIG)); + /** + * List of protocols to enable. If not specified, Traccar enabled all protocols that have port numbers listed. + * The value is a comma-separated list of protocol names. + * + * Example value: teltonika,osmand + */ + public static final ConfigKey PROTOCOLS_ENABLE = new StringConfigKey( + "protocols.enable", + List.of(KeyType.CONFIG)); + /** * List of protocols for overriding time. If not specified override is applied globally. List consist of protocol * names that can be separated by comma or single space character. -- cgit v1.2.3