From 0f7ec1247cff1a68ffa57ade3f45b11f6602e262 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 30 Mar 2024 19:36:35 -0700 Subject: Make handlers nullable --- src/main/java/org/traccar/MainModule.java | 31 ++++++++++++++++++++++ .../org/traccar/handler/CopyAttributesHandler.java | 18 +++++-------- .../java/org/traccar/handler/FilterHandler.java | 4 +-- src/main/java/org/traccar/handler/TimeHandler.java | 22 +++++---------- 4 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index 26654947e..791d61c61 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -72,9 +72,12 @@ import org.traccar.geolocation.GeolocationProvider; import org.traccar.geolocation.GoogleGeolocationProvider; import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.geolocation.UnwiredGeolocationProvider; +import org.traccar.handler.CopyAttributesHandler; +import org.traccar.handler.FilterHandler; import org.traccar.handler.GeocoderHandler; import org.traccar.handler.GeolocationHandler; import org.traccar.handler.SpeedLimitHandler; +import org.traccar.handler.TimeHandler; import org.traccar.helper.ObjectMapperContextResolver; import org.traccar.helper.SanitizerModule; import org.traccar.helper.WebHelper; @@ -336,6 +339,34 @@ public class MainModule extends AbstractModule { return null; } + @Singleton + @Provides + public static CopyAttributesHandler provideCopyAttributesHandler(Config config, CacheManager cacheManager) { + if (config.getBoolean(Keys.PROCESSING_COPY_ATTRIBUTES_ENABLE)) { + return new CopyAttributesHandler(config, cacheManager); + } + return null; + } + + @Singleton + @Provides + public static FilterHandler provideFilterHandler( + Config config, CacheManager cacheManager, Storage storage, StatisticsManager statisticsManager) { + if (config.getBoolean(Keys.FILTER_ENABLE)) { + return new FilterHandler(config, cacheManager, storage, statisticsManager); + } + return null; + } + + @Singleton + @Provides + public static TimeHandler provideTimeHandler(Config config) { + if (config.hasKey(Keys.TIME_OVERRIDE)) { + return new TimeHandler(config); + } + return null; + } + @Singleton @Provides public static BroadcastService provideBroadcastService( diff --git a/src/main/java/org/traccar/handler/CopyAttributesHandler.java b/src/main/java/org/traccar/handler/CopyAttributesHandler.java index 9c31bf56e..f682c99c9 100644 --- a/src/main/java/org/traccar/handler/CopyAttributesHandler.java +++ b/src/main/java/org/traccar/handler/CopyAttributesHandler.java @@ -25,26 +25,22 @@ import org.traccar.session.cache.CacheManager; public class CopyAttributesHandler extends BasePositionHandler { - private final boolean enabled; private final CacheManager cacheManager; @Inject public CopyAttributesHandler(Config config, CacheManager cacheManager) { - enabled = config.getBoolean(Keys.PROCESSING_COPY_ATTRIBUTES_ENABLE); this.cacheManager = cacheManager; } @Override public void handlePosition(Position position, Callback callback) { - if (enabled) { - String attributesString = AttributeUtil.lookup( - cacheManager, Keys.PROCESSING_COPY_ATTRIBUTES, position.getDeviceId()); - Position last = cacheManager.getPosition(position.getDeviceId()); - if (last != null && attributesString != null) { - for (String attribute : attributesString.split("[ ,]")) { - if (last.hasAttribute(attribute) && !position.hasAttribute(attribute)) { - position.getAttributes().put(attribute, last.getAttributes().get(attribute)); - } + String attributesString = AttributeUtil.lookup( + cacheManager, Keys.PROCESSING_COPY_ATTRIBUTES, position.getDeviceId()); + Position last = cacheManager.getPosition(position.getDeviceId()); + if (last != null && attributesString != null) { + for (String attribute : attributesString.split("[ ,]")) { + if (last.hasAttribute(attribute) && !position.hasAttribute(attribute)) { + position.getAttributes().put(attribute, last.getAttributes().get(attribute)); } } } diff --git a/src/main/java/org/traccar/handler/FilterHandler.java b/src/main/java/org/traccar/handler/FilterHandler.java index 4cc6233d0..a9e6024c8 100644 --- a/src/main/java/org/traccar/handler/FilterHandler.java +++ b/src/main/java/org/traccar/handler/FilterHandler.java @@ -40,7 +40,6 @@ public class FilterHandler extends BasePositionHandler { private static final Logger LOGGER = LoggerFactory.getLogger(FilterHandler.class); - private final boolean enabled; private final boolean filterInvalid; private final boolean filterZero; private final boolean filterDuplicate; @@ -65,7 +64,6 @@ public class FilterHandler extends BasePositionHandler { @Inject public FilterHandler( Config config, CacheManager cacheManager, Storage storage, StatisticsManager statisticsManager) { - enabled = config.getBoolean(Keys.FILTER_ENABLE); filterInvalid = config.getBoolean(Keys.FILTER_INVALID); filterZero = config.getBoolean(Keys.FILTER_ZERO); filterDuplicate = config.getBoolean(Keys.FILTER_DUPLICATE); @@ -272,7 +270,7 @@ public class FilterHandler extends BasePositionHandler { @Override public void handlePosition(Position position, Callback callback) { - if (enabled && filter(position)) { + if (filter(position)) { callback.processed(null); } else { callback.processed(position); diff --git a/src/main/java/org/traccar/handler/TimeHandler.java b/src/main/java/org/traccar/handler/TimeHandler.java index 2e703c681..052ad41c3 100644 --- a/src/main/java/org/traccar/handler/TimeHandler.java +++ b/src/main/java/org/traccar/handler/TimeHandler.java @@ -26,18 +26,12 @@ import java.util.Set; public class TimeHandler extends BasePositionHandler { - private final boolean enabled; private final boolean useServerTime; private final Set protocols; @Inject public TimeHandler(Config config) { - enabled = config.hasKey(Keys.TIME_OVERRIDE); - if (enabled) { - useServerTime = config.getString(Keys.TIME_OVERRIDE).equalsIgnoreCase("serverTime"); - } else { - useServerTime = false; - } + useServerTime = config.getString(Keys.TIME_OVERRIDE).equalsIgnoreCase("serverTime"); String protocolList = config.getString(Keys.TIME_PROTOCOLS); if (protocolList != null) { protocols = new HashSet<>(Arrays.asList(protocolList.split("[, ]"))); @@ -49,14 +43,12 @@ public class TimeHandler extends BasePositionHandler { @Override public void handlePosition(Position position, Callback callback) { - if (enabled) { - if (protocols == null || protocols.contains(position.getProtocol())) { - if (useServerTime) { - position.setDeviceTime(position.getServerTime()); - position.setFixTime(position.getServerTime()); - } else { - position.setFixTime(position.getDeviceTime()); - } + if (protocols == null || protocols.contains(position.getProtocol())) { + if (useServerTime) { + position.setDeviceTime(position.getServerTime()); + position.setFixTime(position.getServerTime()); + } else { + position.setFixTime(position.getDeviceTime()); } } callback.processed(position); -- cgit v1.2.3