diff options
-rw-r--r-- | src/main/java/org/traccar/Context.java | 11 | ||||
-rw-r--r-- | src/main/java/org/traccar/Main.java | 32 | ||||
-rw-r--r-- | src/main/java/org/traccar/MainModule.java | 9 | ||||
-rw-r--r-- | src/main/java/org/traccar/web/WebServer.java | 2 |
4 files changed, 25 insertions, 29 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 46c761094..3e4cc1bb9 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -46,7 +46,6 @@ import org.traccar.session.cache.CacheManager; import org.traccar.sms.HttpSmsClient; import org.traccar.sms.SmsManager; import org.traccar.sms.SnsSmsClient; -import org.traccar.web.WebServer; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -124,12 +123,6 @@ public final class Context { return Main.getInjector() != null ? Main.getInjector().getInstance(Geocoder.class) : null; } - private static WebServer webServer; - - public static WebServer getWebServer() { - return webServer; - } - private static NotificationManager notificationManager; public static NotificationManager getNotificationManager() { @@ -210,10 +203,6 @@ public final class Context { identityManager = deviceManager; - if (config.hasKey(Keys.WEB_PORT)) { - webServer = new WebServer(config); - } - permissionsManager = new PermissionsManager(dataManager, usersManager); connectionManager = new ConnectionManager(); diff --git a/src/main/java/org/traccar/Main.java b/src/main/java/org/traccar/Main.java index c35ea33a9..412ac1f5e 100644 --- a/src/main/java/org/traccar/Main.java +++ b/src/main/java/org/traccar/Main.java @@ -22,6 +22,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.broadcast.BroadcastService; import org.traccar.schedule.ScheduleManager; +import org.traccar.web.WebServer; import java.io.File; import java.lang.management.ManagementFactory; @@ -29,10 +30,10 @@ import java.lang.management.MemoryMXBean; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.nio.charset.Charset; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; import java.util.Locale; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; public final class Main { @@ -119,28 +120,23 @@ public final class Main { LOGGER.info("Version: " + Main.class.getPackage().getImplementationVersion()); LOGGER.info("Starting server..."); - List<LifecycleObject> services = new LinkedList<>(); - services.add(injector.getInstance(ServerManager.class)); - services.add(Context.getWebServer()); - services.add(injector.getInstance(ScheduleManager.class)); - services.add(injector.getInstance(BroadcastService.class)); + var services = Stream.of( + ServerManager.class, WebServer.class, ScheduleManager.class, BroadcastService.class) + .map(injector::getInstance) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - for (LifecycleObject service : services) { - if (service != null) { - service.start(); - } + for (var service : services) { + service.start(); } Thread.setDefaultUncaughtExceptionHandler((t, e) -> LOGGER.error("Thread exception", e)); Runtime.getRuntime().addShutdownHook(new Thread(() -> { - LOGGER.info("Shutting down server..."); + LOGGER.info("Stopping server..."); - Collections.reverse(services); - for (LifecycleObject service : services) { - if (service != null) { - service.stop(); - } + for (var service : services) { + service.stop(); } })); } catch (Exception e) { diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index d070c9fd7..9ec73d819 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -61,6 +61,7 @@ import org.traccar.sms.SmsManager; import org.traccar.speedlimit.OverpassSpeedLimitProvider; import org.traccar.speedlimit.SpeedLimitProvider; import org.traccar.storage.Storage; +import org.traccar.web.WebServer; import javax.annotation.Nullable; import javax.inject.Singleton; @@ -128,6 +129,14 @@ public class MainModule extends AbstractModule { return null; } + @Provides + public static WebServer provideWebServer(Config config) { + if (config.hasKey(Keys.WEB_PORT)) { + return new WebServer(config); + } + return null; + } + @Singleton @Provides public static Geocoder provideGeocoder(Config config) { diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index f1ee8fcb2..fdbfc1464 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -60,6 +60,7 @@ import org.traccar.api.security.SecurityRequestFilter; import org.traccar.api.resource.ServerResource; import org.traccar.config.Keys; +import javax.inject.Inject; import javax.servlet.DispatcherType; import javax.servlet.ServletException; import javax.servlet.SessionCookieConfig; @@ -79,6 +80,7 @@ public class WebServer implements LifecycleObject { private final Server server; + @Inject public WebServer(Config config) { String address = config.getString(Keys.WEB_ADDRESS); int port = config.getInteger(Keys.WEB_PORT); |