diff options
author | Anton Tananaev <anton@traccar.org> | 2022-06-16 18:19:57 -0700 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2022-06-16 18:19:57 -0700 |
commit | ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f (patch) | |
tree | d4362b6cf9b62ba952366a4eb6081fc576987e80 /src | |
parent | 0de7889d5548ab16bc8dc31e8734dc48dd51d43d (diff) | |
download | trackermap-server-ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f.tar.gz trackermap-server-ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f.tar.bz2 trackermap-server-ee9abd94265eb704b5f5dd2262f1a3460bbe2a8f.zip |
Refactor config usage
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/traccar/MainModule.java | 6 | ||||
-rw-r--r-- | src/main/java/org/traccar/storage/DatabaseStorage.java | 21 | ||||
-rw-r--r-- | src/main/java/org/traccar/storage/QueryBuilder.java | 17 | ||||
-rw-r--r-- | src/main/java/org/traccar/web/ConsoleServlet.java | 16 | ||||
-rw-r--r-- | src/main/java/org/traccar/web/WebServer.java | 2 |
5 files changed, 36 insertions, 26 deletions
diff --git a/src/main/java/org/traccar/MainModule.java b/src/main/java/org/traccar/MainModule.java index aac11e619..a29c2c910 100644 --- a/src/main/java/org/traccar/MainModule.java +++ b/src/main/java/org/traccar/MainModule.java @@ -88,6 +88,7 @@ public class MainModule extends AbstractModule { @Override protected void configure() { + bind(Storage.class).to(DatabaseStorage.class); bind(Timer.class).to(HashedWheelTimer.class).in(Scopes.SINGLETON); } @@ -109,11 +110,6 @@ public class MainModule extends AbstractModule { } @Provides - public static Storage provideStorage(DataSource dataSource, ObjectMapper objectMapper) { - return new DatabaseStorage(dataSource, objectMapper); - } - - @Provides public static IdentityManager provideIdentityManager() { return Context.getIdentityManager(); } diff --git a/src/main/java/org/traccar/storage/DatabaseStorage.java b/src/main/java/org/traccar/storage/DatabaseStorage.java index 661e792d4..052f11ad2 100644 --- a/src/main/java/org/traccar/storage/DatabaseStorage.java +++ b/src/main/java/org/traccar/storage/DatabaseStorage.java @@ -16,6 +16,7 @@ package org.traccar.storage; import com.fasterxml.jackson.databind.ObjectMapper; +import org.traccar.config.Config; import org.traccar.model.BaseModel; import org.traccar.model.Device; import org.traccar.model.Group; @@ -27,6 +28,7 @@ import org.traccar.storage.query.Limit; import org.traccar.storage.query.Order; import org.traccar.storage.query.Request; +import javax.inject.Inject; import javax.sql.DataSource; import java.sql.SQLException; import java.util.HashMap; @@ -38,10 +40,13 @@ import java.util.stream.Collectors; public class DatabaseStorage extends Storage { + private final Config config; private final DataSource dataSource; private final ObjectMapper objectMapper; - public DatabaseStorage(DataSource dataSource, ObjectMapper objectMapper) { + @Inject + public DatabaseStorage(Config config, DataSource dataSource, ObjectMapper objectMapper) { + this.config = config; this.dataSource = dataSource; this.objectMapper = objectMapper; } @@ -55,7 +60,7 @@ public class DatabaseStorage extends Storage { query.append(formatOrder(request.getOrder())); query.append(formatLimit(request.getLimit())); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); } @@ -75,7 +80,7 @@ public class DatabaseStorage extends Storage { query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> ':' + c)); query.append(")"); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString(), true); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString(), true); builder.setObject(entity); return builder.executeUpdate(); } catch (SQLException e) { @@ -91,7 +96,7 @@ public class DatabaseStorage extends Storage { query.append(formatColumns(request.getColumns(), entity.getClass(), "set", c -> c + " = :" + c)); query.append(formatCondition(request.getCondition())); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); builder.setObject(entity); for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); @@ -108,7 +113,7 @@ public class DatabaseStorage extends Storage { query.append(getStorageName(clazz)); query.append(formatCondition(request.getCondition())); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); for (Map.Entry<String, Object> variable : getConditionVariables(request.getCondition()).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); } @@ -136,7 +141,7 @@ public class DatabaseStorage extends Storage { Condition combinedCondition = Condition.merge(conditions); query.append(formatCondition(combinedCondition)); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString()); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString()); for (Map.Entry<String, Object> variable : getConditionVariables(combinedCondition).entrySet()) { builder.setValue(variable.getKey(), variable.getValue()); } @@ -154,7 +159,7 @@ public class DatabaseStorage extends Storage { query.append(permission.get().keySet().stream().map(key -> ':' + key).collect(Collectors.joining(", "))); query.append(")"); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString(), true); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString(), true); for (var entry : permission.get().entrySet()) { builder.setLong(entry.getKey(), entry.getValue()); } @@ -172,7 +177,7 @@ public class DatabaseStorage extends Storage { query.append(permission .get().keySet().stream().map(key -> key + " = :" + key).collect(Collectors.joining(" AND "))); try { - QueryBuilder builder = QueryBuilder.create(dataSource, objectMapper, query.toString(), true); + QueryBuilder builder = QueryBuilder.create(config, dataSource, objectMapper, query.toString(), true); for (var entry : permission.get().entrySet()) { builder.setLong(entry.getKey(), entry.getValue()); } diff --git a/src/main/java/org/traccar/storage/QueryBuilder.java b/src/main/java/org/traccar/storage/QueryBuilder.java index 8502d5f0b..910ebf170 100644 --- a/src/main/java/org/traccar/storage/QueryBuilder.java +++ b/src/main/java/org/traccar/storage/QueryBuilder.java @@ -19,7 +19,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.traccar.Context; +import org.traccar.config.Config; import org.traccar.config.Keys; import org.traccar.model.Permission; @@ -42,10 +42,12 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +@SuppressWarnings("UnusedReturnValue") public final class QueryBuilder { private static final Logger LOGGER = LoggerFactory.getLogger(QueryBuilder.class); + private final Config config; private final ObjectMapper objectMapper; private final Map<String, List<Integer>> indexMap = new HashMap<>(); @@ -55,8 +57,9 @@ public final class QueryBuilder { private final boolean returnGeneratedKeys; private QueryBuilder( - DataSource dataSource, ObjectMapper objectMapper, + Config config, DataSource dataSource, ObjectMapper objectMapper, String query, boolean returnGeneratedKeys) throws SQLException { + this.config = config; this.objectMapper = objectMapper; this.query = query; this.returnGeneratedKeys = returnGeneratedKeys; @@ -133,14 +136,14 @@ public final class QueryBuilder { } public static QueryBuilder create( - DataSource dataSource, ObjectMapper objectMapper, String query) throws SQLException { - return new QueryBuilder(dataSource, objectMapper, query, false); + Config config, DataSource dataSource, ObjectMapper objectMapper, String query) throws SQLException { + return new QueryBuilder(config, dataSource, objectMapper, query, false); } public static QueryBuilder create( - DataSource dataSource, ObjectMapper objectMapper, String query, + Config config, DataSource dataSource, ObjectMapper objectMapper, String query, boolean returnGeneratedKeys) throws SQLException { - return new QueryBuilder(dataSource, objectMapper, query, returnGeneratedKeys); + return new QueryBuilder(config, dataSource, objectMapper, query, returnGeneratedKeys); } private List<Integer> indexes(String name) { @@ -396,7 +399,7 @@ public final class QueryBuilder { } private void logQuery() { - if (Context.getConfig().getBoolean(Keys.LOGGER_QUERIES)) { + if (config.getBoolean(Keys.LOGGER_QUERIES)) { LOGGER.info(query); } } diff --git a/src/main/java/org/traccar/web/ConsoleServlet.java b/src/main/java/org/traccar/web/ConsoleServlet.java index 0f3dcd8fd..902a4f7a9 100644 --- a/src/main/java/org/traccar/web/ConsoleServlet.java +++ b/src/main/java/org/traccar/web/ConsoleServlet.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2020 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. @@ -19,7 +19,7 @@ import org.h2.server.web.ConnectionInfo; import org.h2.server.web.WebServlet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.traccar.Context; +import org.traccar.config.Config; import org.traccar.config.Keys; import java.lang.reflect.Field; @@ -30,6 +30,12 @@ public class ConsoleServlet extends WebServlet { private static final Logger LOGGER = LoggerFactory.getLogger(ConsoleServlet.class); + private final Config config; + + public ConsoleServlet(Config config) { + this.config = config; + } + @Override public void init() { super.init(); @@ -40,9 +46,9 @@ public class ConsoleServlet extends WebServlet { org.h2.server.web.WebServer server = (org.h2.server.web.WebServer) field.get(this); ConnectionInfo connectionInfo = new ConnectionInfo("Traccar|" - + Context.getConfig().getString(Keys.DATABASE_DRIVER) + "|" - + Context.getConfig().getString(Keys.DATABASE_URL) + "|" - + Context.getConfig().getString(Keys.DATABASE_USER)); + + config.getString(Keys.DATABASE_DRIVER) + "|" + + config.getString(Keys.DATABASE_URL) + "|" + + config.getString(Keys.DATABASE_USER)); Method method; diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index d7276d21f..6c8a798b4 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -91,7 +91,7 @@ public class WebServer implements LifecycleObject { initSessionConfig(servletHandler); if (config.getBoolean(Keys.WEB_CONSOLE)) { - servletHandler.addServlet(new ServletHolder(new ConsoleServlet()), "/console/*"); + servletHandler.addServlet(new ServletHolder(new ConsoleServlet(config)), "/console/*"); } initWebApp(servletHandler); |