From 4d59dd0973479bca3d51cf3bba6b0769a0d671c3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 18 Jun 2022 13:47:56 -0700 Subject: Fix config injection --- src/main/java/org/traccar/MainModule.java | 16 +--------------- src/main/java/org/traccar/config/Config.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 16 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index 7dcc91f32..39d5a656a 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -22,7 +22,6 @@ import com.google.inject.AbstractModule; import com.google.inject.Injector; import com.google.inject.Provides; import com.google.inject.Scopes; -import com.google.inject.name.Named; import com.google.inject.name.Names; import io.netty.util.HashedWheelTimer; import io.netty.util.Timer; @@ -61,7 +60,6 @@ import org.traccar.geolocation.UnwiredGeolocationProvider; import org.traccar.handler.GeocoderHandler; import org.traccar.handler.GeolocationHandler; import org.traccar.handler.SpeedLimitHandler; -import org.traccar.helper.Log; import org.traccar.helper.SanitizerModule; import org.traccar.notification.EventForwarder; import org.traccar.session.cache.CacheManager; @@ -95,6 +93,7 @@ public class MainModule extends AbstractModule { @Override protected void configure() { bindConstant().annotatedWith(Names.named("configFile")).to(configFile); + bind(Config.class).asEagerSingleton(); bind(Storage.class).to(DatabaseStorage.class); bind(Timer.class).to(HashedWheelTimer.class).in(Scopes.SINGLETON); } @@ -111,19 +110,6 @@ public class MainModule extends AbstractModule { return objectMapper; } - @Singleton - @Provides - public static Config provideConfig(@Named("configFile") String configFile) throws Exception { - try { - Config config = new Config(configFile); - Log.setupLogger(config); - return config; - } catch (Exception e) { - Log.setupDefaultLogger(); - throw e; - } - } - @Provides public static Client provideClient() { return ClientBuilder.newClient().register( diff --git a/src/main/java/org/traccar/config/Config.java b/src/main/java/org/traccar/config/Config.java index 815a6e86a..5f95c16d9 100644 --- a/src/main/java/org/traccar/config/Config.java +++ b/src/main/java/org/traccar/config/Config.java @@ -16,13 +16,18 @@ package org.traccar.config; import com.google.common.annotations.VisibleForTesting; +import com.google.inject.name.Named; +import org.traccar.helper.Log; +import javax.inject.Inject; +import javax.inject.Singleton; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.InvalidPropertiesFormatException; import java.util.Properties; +@Singleton public class Config { private final Properties properties = new Properties(); @@ -32,7 +37,8 @@ public class Config { public Config() { } - public Config(String file) throws IOException { + @Inject + public Config(@Named("configFile") String file) throws IOException { try { Properties mainProperties = new Properties(); try (InputStream inputStream = new FileInputStream(file)) { @@ -50,8 +56,14 @@ public class Config { useEnvironmentVariables = Boolean.parseBoolean(System.getenv("CONFIG_USE_ENVIRONMENT_VARIABLES")) || Boolean.parseBoolean(properties.getProperty("config.useEnvironmentVariables")); + + Log.setupLogger(this); } catch (InvalidPropertiesFormatException e) { + Log.setupDefaultLogger(); throw new RuntimeException("Configuration file is not a valid XML document", e); + } catch (Exception e) { + Log.setupDefaultLogger(); + throw e; } } -- cgit v1.2.3