diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/org/traccar/Context.java | 45 | ||||
-rw-r--r-- | src/main/java/org/traccar/Main.java | 3 | ||||
-rw-r--r-- | src/main/java/org/traccar/MainModule.java | 22 | ||||
-rw-r--r-- | src/main/java/org/traccar/TrackerClient.java | 2 | ||||
-rw-r--r-- | src/main/java/org/traccar/TrackerServer.java | 2 | ||||
-rw-r--r-- | src/main/java/org/traccar/api/AsyncSocketServlet.java | 9 | ||||
-rw-r--r-- | src/main/java/org/traccar/api/CorsResponseFilter.java | 14 |
7 files changed, 39 insertions, 58 deletions
diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java deleted file mode 100644 index 583ee460b..000000000 --- a/src/main/java/org/traccar/Context.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2015 - 2022 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar; - -import org.traccar.config.Config; -import org.traccar.helper.Log; - -public final class Context { - - private Context() { - } - - private static Config config; - - public static Config getConfig() { - return config; - } - - public static void init(String configFile) throws Exception { - - try { - config = new Config(configFile); - Log.setupLogger(config); - } catch (Exception e) { - config = new Config(); - Log.setupDefaultLogger(); - throw e; - } - - } - -} diff --git a/src/main/java/org/traccar/Main.java b/src/main/java/org/traccar/Main.java index bf69b565d..6a968ac7e 100644 --- a/src/main/java/org/traccar/Main.java +++ b/src/main/java/org/traccar/Main.java @@ -115,8 +115,7 @@ public final class Main { public static void run(String configFile) { try { - injector = Guice.createInjector(new MainModule(), new DatabaseModule(), new WebModule()); - Context.init(configFile); + injector = Guice.createInjector(new MainModule(configFile), new DatabaseModule(), new WebModule()); logSystemInfo(); LOGGER.info("Version: " + Main.class.getPackage().getImplementationVersion()); LOGGER.info("Starting server..."); diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index 439dcd1d9..7dcc91f32 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -22,6 +22,8 @@ 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; import org.apache.velocity.app.VelocityEngine; @@ -59,6 +61,7 @@ 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; @@ -83,8 +86,15 @@ import java.util.Properties; public class MainModule extends AbstractModule { + private final String configFile; + + public MainModule(String configFile) { + this.configFile = configFile; + } + @Override protected void configure() { + bindConstant().annotatedWith(Names.named("configFile")).to(configFile); bind(Storage.class).to(DatabaseStorage.class); bind(Timer.class).to(HashedWheelTimer.class).in(Scopes.SINGLETON); } @@ -101,9 +111,17 @@ public class MainModule extends AbstractModule { return objectMapper; } + @Singleton @Provides - public static Config provideConfig() { - return Context.getConfig(); + 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 diff --git a/src/main/java/org/traccar/TrackerClient.java b/src/main/java/org/traccar/TrackerClient.java index 5a3c38212..2e8a73677 100644 --- a/src/main/java/org/traccar/TrackerClient.java +++ b/src/main/java/org/traccar/TrackerClient.java @@ -54,7 +54,7 @@ public abstract class TrackerClient implements TrackerConnector { } public TrackerClient(String protocol) { - Config config = Context.getConfig(); + Config config = Main.getInjector().getInstance(Config.class); secure = config.getBoolean(Keys.PROTOCOL_SSL.withPrefix(protocol)); interval = config.getLong(Keys.PROTOCOL_INTERVAL.withPrefix(protocol)); diff --git a/src/main/java/org/traccar/TrackerServer.java b/src/main/java/org/traccar/TrackerServer.java index dd83ca6b0..ccf3cd640 100644 --- a/src/main/java/org/traccar/TrackerServer.java +++ b/src/main/java/org/traccar/TrackerServer.java @@ -58,7 +58,7 @@ public abstract class TrackerServer implements TrackerConnector { public TrackerServer(boolean datagram, String protocol) { this.datagram = datagram; - Config config = Context.getConfig(); + Config config = Main.getInjector().getInstance(Config.class); secure = config.getBoolean(Keys.PROTOCOL_SSL.withPrefix(protocol)); address = config.getString(Keys.PROTOCOL_ADDRESS.withPrefix(protocol)); diff --git a/src/main/java/org/traccar/api/AsyncSocketServlet.java b/src/main/java/org/traccar/api/AsyncSocketServlet.java index 7d9fdf0ed..40e1551a1 100644 --- a/src/main/java/org/traccar/api/AsyncSocketServlet.java +++ b/src/main/java/org/traccar/api/AsyncSocketServlet.java @@ -18,8 +18,8 @@ package org.traccar.api; import com.fasterxml.jackson.databind.ObjectMapper; import org.eclipse.jetty.websocket.server.JettyWebSocketServlet; import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory; -import org.traccar.Context; import org.traccar.api.resource.SessionResource; +import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.session.ConnectionManager; import org.traccar.storage.Storage; @@ -32,12 +32,15 @@ import java.time.Duration; @Singleton public class AsyncSocketServlet extends JettyWebSocketServlet { + private final Config config; private final ObjectMapper objectMapper; private final ConnectionManager connectionManager; private final Storage storage; @Inject - public AsyncSocketServlet(ObjectMapper objectMapper, ConnectionManager connectionManager, Storage storage) { + public AsyncSocketServlet( + Config config, ObjectMapper objectMapper, ConnectionManager connectionManager, Storage storage) { + this.config = config; this.objectMapper = objectMapper; this.connectionManager = connectionManager; this.storage = storage; @@ -45,7 +48,7 @@ public class AsyncSocketServlet extends JettyWebSocketServlet { @Override public void configure(JettyWebSocketServletFactory factory) { - factory.setIdleTimeout(Duration.ofMillis(Context.getConfig().getLong(Keys.WEB_TIMEOUT))); + factory.setIdleTimeout(Duration.ofMillis(config.getLong(Keys.WEB_TIMEOUT))); factory.setCreator((req, resp) -> { if (req.getSession() != null) { long userId = (Long) ((HttpSession) req.getSession()).getAttribute(SessionResource.USER_ID_KEY); diff --git a/src/main/java/org/traccar/api/CorsResponseFilter.java b/src/main/java/org/traccar/api/CorsResponseFilter.java index 91aea5718..5375e207f 100644 --- a/src/main/java/org/traccar/api/CorsResponseFilter.java +++ b/src/main/java/org/traccar/api/CorsResponseFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2022 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,9 +16,10 @@ package org.traccar.api; import io.netty.handler.codec.http.HttpHeaderNames; -import org.traccar.Context; +import org.traccar.config.Config; import org.traccar.config.Keys; +import javax.inject.Inject; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; @@ -26,6 +27,13 @@ import java.io.IOException; public class CorsResponseFilter implements ContainerResponseFilter { + private final String allowed; + + @Inject + public CorsResponseFilter(Config config) { + allowed = config.getString(Keys.WEB_ORIGIN); + } + private static final String ORIGIN_ALL = "*"; private static final String HEADERS_ALL = "origin, content-type, accept, authorization"; private static final String METHODS_ALL = "GET, POST, PUT, DELETE, OPTIONS"; @@ -46,8 +54,6 @@ public class CorsResponseFilter implements ContainerResponseFilter { if (!response.getHeaders().containsKey(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString())) { String origin = request.getHeaderString(HttpHeaderNames.ORIGIN.toString()); - String allowed = Context.getConfig().getString(Keys.WEB_ORIGIN); - if (origin == null) { response.getHeaders().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString(), ORIGIN_ALL); } else if (allowed == null || allowed.equals(ORIGIN_ALL) || allowed.contains(origin)) { |