diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-18 14:12:05 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-18 14:12:05 -0700 |
commit | ed64af90036d5e29b1e5fdf68df68c5c126beff7 (patch) | |
tree | 515f7695351df860a5c53b242e0a741413a491a7 | |
parent | 8ed8f8051b735a6f7c9480601e27339dbe8dab01 (diff) | |
download | trackermap-server-ed64af90036d5e29b1e5fdf68df68c5c126beff7.tar.gz trackermap-server-ed64af90036d5e29b1e5fdf68df68c5c126beff7.tar.bz2 trackermap-server-ed64af90036d5e29b1e5fdf68df68c5c126beff7.zip |
Fix geocoder injection
-rw-r--r-- | src/main/java/org/traccar/MainModule.java | 56 | ||||
-rw-r--r-- | src/main/java/org/traccar/geocoder/Geocoder.java | 4 | ||||
-rw-r--r-- | src/main/java/org/traccar/geocoder/JsonGeocoder.java | 13 |
3 files changed, 51 insertions, 22 deletions
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index 39d5a656a..3120118fc 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -146,7 +146,7 @@ public class MainModule extends AbstractModule { @Singleton @Provides - public static Geocoder provideGeocoder(Config config, Client client) { + public static Geocoder provideGeocoder(Config config, Client client, StatisticsManager statisticsManager) { if (config.getBoolean(Keys.GEOCODER_ENABLE)) { String type = config.getString(Keys.GEOCODER_TYPE, "google"); String url = config.getString(Keys.GEOCODER_URL); @@ -157,42 +157,62 @@ public class MainModule extends AbstractModule { AddressFormat addressFormat = formatString != null ? new AddressFormat(formatString) : new AddressFormat(); int cacheSize = config.getInteger(Keys.GEOCODER_CACHE_SIZE); + Geocoder geocoder; switch (type) { case "nominatim": - return new NominatimGeocoder(client, url, key, language, cacheSize, addressFormat); + geocoder = new NominatimGeocoder(client, url, key, language, cacheSize, addressFormat); + break; case "gisgraphy": - return new GisgraphyGeocoder(client, url, cacheSize, addressFormat); + geocoder = new GisgraphyGeocoder(client, url, cacheSize, addressFormat); + break; case "mapquest": - return new MapQuestGeocoder(client, url, key, cacheSize, addressFormat); + geocoder = new MapQuestGeocoder(client, url, key, cacheSize, addressFormat); + break; case "opencage": - return new OpenCageGeocoder(client, url, key, language, cacheSize, addressFormat); + geocoder = new OpenCageGeocoder(client, url, key, language, cacheSize, addressFormat); + break; case "bingmaps": - return new BingMapsGeocoder(client, url, key, cacheSize, addressFormat); + geocoder = new BingMapsGeocoder(client, url, key, cacheSize, addressFormat); + break; case "factual": - return new FactualGeocoder(client, url, key, cacheSize, addressFormat); + geocoder = new FactualGeocoder(client, url, key, cacheSize, addressFormat); + break; case "geocodefarm": - return new GeocodeFarmGeocoder(client, key, language, cacheSize, addressFormat); + geocoder = new GeocodeFarmGeocoder(client, key, language, cacheSize, addressFormat); + break; case "geocodexyz": - return new GeocodeXyzGeocoder(client, key, cacheSize, addressFormat); + geocoder = new GeocodeXyzGeocoder(client, key, cacheSize, addressFormat); + break; case "ban": - return new BanGeocoder(client, cacheSize, addressFormat); + geocoder = new BanGeocoder(client, cacheSize, addressFormat); + break; case "here": - return new HereGeocoder(client, url, id, key, language, cacheSize, addressFormat); + geocoder = new HereGeocoder(client, url, id, key, language, cacheSize, addressFormat); + break; case "mapmyindia": - return new MapmyIndiaGeocoder(client, url, key, cacheSize, addressFormat); + geocoder = new MapmyIndiaGeocoder(client, url, key, cacheSize, addressFormat); + break; case "tomtom": - return new TomTomGeocoder(client, url, key, cacheSize, addressFormat); + geocoder = new TomTomGeocoder(client, url, key, cacheSize, addressFormat); + break; case "positionstack": - return new PositionStackGeocoder(client, key, cacheSize, addressFormat); + geocoder = new PositionStackGeocoder(client, key, cacheSize, addressFormat); + break; case "mapbox": - return new MapboxGeocoder(client, key, cacheSize, addressFormat); + geocoder = new MapboxGeocoder(client, key, cacheSize, addressFormat); + break; case "maptiler": - return new MapTilerGeocoder(client, key, cacheSize, addressFormat); + geocoder = new MapTilerGeocoder(client, key, cacheSize, addressFormat); + break; case "geoapify": - return new GeoapifyGeocoder(client, key, language, cacheSize, addressFormat); + geocoder = new GeoapifyGeocoder(client, key, language, cacheSize, addressFormat); + break; default: - return new GoogleGeocoder(client, key, language, cacheSize, addressFormat); + geocoder = new GoogleGeocoder(client, key, language, cacheSize, addressFormat); + break; } + geocoder.setStatisticsManager(statisticsManager); + return geocoder; } return null; } diff --git a/src/main/java/org/traccar/geocoder/Geocoder.java b/src/main/java/org/traccar/geocoder/Geocoder.java index 587a27520..f4abe871a 100644 --- a/src/main/java/org/traccar/geocoder/Geocoder.java +++ b/src/main/java/org/traccar/geocoder/Geocoder.java @@ -15,6 +15,8 @@ */ package org.traccar.geocoder; +import org.traccar.database.StatisticsManager; + public interface Geocoder { interface ReverseGeocoderCallback { @@ -27,4 +29,6 @@ public interface Geocoder { String getAddress(double latitude, double longitude, ReverseGeocoderCallback callback); + void setStatisticsManager(StatisticsManager statisticsManager); + } diff --git a/src/main/java/org/traccar/geocoder/JsonGeocoder.java b/src/main/java/org/traccar/geocoder/JsonGeocoder.java index 0262de18c..6105e8cfd 100644 --- a/src/main/java/org/traccar/geocoder/JsonGeocoder.java +++ b/src/main/java/org/traccar/geocoder/JsonGeocoder.java @@ -17,7 +17,6 @@ package org.traccar.geocoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.traccar.Main; import org.traccar.database.StatisticsManager; import javax.json.JsonObject; @@ -36,6 +35,7 @@ public abstract class JsonGeocoder implements Geocoder { private final Client client; private final String url; private final AddressFormat addressFormat; + private StatisticsManager statisticsManager; private Map<Map.Entry<Double, Double>, String> cache; @@ -44,7 +44,7 @@ public abstract class JsonGeocoder implements Geocoder { this.url = url; this.addressFormat = addressFormat; if (cacheSize > 0) { - this.cache = Collections.synchronizedMap(new LinkedHashMap<Map.Entry<Double, Double>, String>() { + this.cache = Collections.synchronizedMap(new LinkedHashMap<>() { @Override protected boolean removeEldestEntry(Map.Entry eldest) { return size() > cacheSize; @@ -53,6 +53,11 @@ public abstract class JsonGeocoder implements Geocoder { } } + @Override + public void setStatisticsManager(StatisticsManager statisticsManager) { + this.statisticsManager = statisticsManager; + } + protected String readValue(JsonObject object, String key) { if (object.containsKey(key) && !object.isNull(key)) { return object.getString(key); @@ -98,8 +103,8 @@ public abstract class JsonGeocoder implements Geocoder { } } - if (Main.getInjector() != null) { - Main.getInjector().getInstance(StatisticsManager.class).registerGeocoderRequest(); + if (statisticsManager != null) { + statisticsManager.registerGeocoderRequest(); } var request = client.target(String.format(url, latitude, longitude)).request(); |