From 21e5246ace0f6ecfd00248e5d29fd64a564e03c1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 27 Dec 2020 17:33:00 -0800 Subject: Migrate more config keys --- src/main/java/org/traccar/Context.java | 3 +- src/main/java/org/traccar/config/Keys.java | 44 ++++++++++++++++++++++ .../java/org/traccar/database/DataManager.java | 29 +++++++------- src/main/java/org/traccar/web/ConsoleServlet.java | 9 +++-- 4 files changed, 66 insertions(+), 19 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/Context.java b/src/main/java/org/traccar/Context.java index 2b031cfd3..2fb6998b8 100644 --- a/src/main/java/org/traccar/Context.java +++ b/src/main/java/org/traccar/Context.java @@ -23,6 +23,7 @@ import org.eclipse.jetty.util.URIUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.config.Config; +import org.traccar.config.Keys; import org.traccar.database.AttributesManager; import org.traccar.database.BaseObjectManager; import org.traccar.database.CalendarManager; @@ -294,7 +295,7 @@ public final class Context { client = ClientBuilder.newClient().register(new ObjectMapperContextResolver()); - if (config.hasKey("database.url")) { + if (config.hasKey(Keys.DATABASE_URL)) { dataManager = new DataManager(config); } diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 77b42943b..206295499 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -75,6 +75,50 @@ public final class Keys { public static final ConfigKey EXTRA_HANDLERS = new ConfigKey( "extra.handlers", String.class); + /** + * Path to the database driver JAR file. Traccar includes drivers for MySQL, PostgreSQL and H2 databases. If you use + * one of those, you don't need to specify this parameter. + */ + public static final ConfigKey DATABASE_DRIVER_FILE = new ConfigKey( + "database.driverFile", String.class); + + /** + * Database driver Java class. For H2 use 'org.h2.Driver'. MySQL driver class name is 'com.mysql.jdbc.Driver'. + */ + public static final ConfigKey DATABASE_DRIVER = new ConfigKey( + "database.driver", String.class); + + /** + * Database connection URL. By default Traccar uses H2 database. + */ + public static final ConfigKey DATABASE_URL = new ConfigKey( + "database.url", String.class); + + /** + * Database user name. Default administrator user for H2 database is 'sa'. + */ + public static final ConfigKey DATABASE_USER = new ConfigKey( + "database.user", String.class); + + /** + * Database user password. Default password for H2 admin (sa) user is empty. + */ + public static final ConfigKey DATABASE_PASSWORD = new ConfigKey( + "database.password", String.class); + + /** + * Path to Liquibase master changelog file. + */ + public static final ConfigKey DATABASE_CHANGELOG = new ConfigKey( + "database.changelog", String.class); + + /** + * SQL query to check connection status. Default value is 'SELECT 1'. For Oracle database you can use + * 'SELECT 1 FROM DUAL'. + */ + public static final ConfigKey DATABASE_CHECK_CONNECTION = new ConfigKey( + "database.checkConnection", String.class); + /** * Store original HEX or string data as "raw" attribute in the corresponding position. */ diff --git a/src/main/java/org/traccar/database/DataManager.java b/src/main/java/org/traccar/database/DataManager.java index 8b9690f77..ac4749a15 100644 --- a/src/main/java/org/traccar/database/DataManager.java +++ b/src/main/java/org/traccar/database/DataManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2020 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. @@ -41,6 +41,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.config.Config; import org.traccar.Context; +import org.traccar.config.Keys; import org.traccar.helper.DateUtil; import org.traccar.model.Attribute; import org.traccar.model.Device; @@ -100,7 +101,7 @@ public class DataManager { } else { - String driverFile = config.getString("database.driverFile"); + String driverFile = config.getString(Keys.DATABASE_DRIVER_FILE); if (driverFile != null) { ClassLoader classLoader = ClassLoader.getSystemClassLoader(); try { @@ -115,17 +116,17 @@ public class DataManager { } } - String driver = config.getString("database.driver"); + String driver = config.getString(Keys.DATABASE_DRIVER); if (driver != null) { Class.forName(driver); } HikariConfig hikariConfig = new HikariConfig(); - hikariConfig.setDriverClassName(config.getString("database.driver")); - hikariConfig.setJdbcUrl(config.getString("database.url")); - hikariConfig.setUsername(config.getString("database.user")); - hikariConfig.setPassword(config.getString("database.password")); - hikariConfig.setConnectionInitSql(config.getString("database.checkConnection", "SELECT 1")); + hikariConfig.setDriverClassName(driver); + hikariConfig.setJdbcUrl(config.getString(Keys.DATABASE_URL)); + hikariConfig.setUsername(config.getString(Keys.DATABASE_USER)); + hikariConfig.setPassword(config.getString(Keys.DATABASE_PASSWORD)); + hikariConfig.setConnectionInitSql(config.getString(Keys.DATABASE_CHECK_CONNECTION, "SELECT 1")); hikariConfig.setIdleTimeout(600000); int maxPoolSize = config.getInteger("database.maxPoolSize"); @@ -293,19 +294,19 @@ public class DataManager { private void initDatabaseSchema() throws SQLException, LiquibaseException { - if (config.hasKey("database.changelog")) { + if (config.hasKey(Keys.DATABASE_CHANGELOG)) { ResourceAccessor resourceAccessor = new FileSystemResourceAccessor(); Database database = DatabaseFactory.getInstance().openDatabase( - config.getString("database.url"), - config.getString("database.user"), - config.getString("database.password"), - config.getString("database.driver"), + config.getString(Keys.DATABASE_URL), + config.getString(Keys.DATABASE_USER), + config.getString(Keys.DATABASE_PASSWORD), + config.getString(Keys.DATABASE_DRIVER), null, null, null, resourceAccessor); Liquibase liquibase = new Liquibase( - config.getString("database.changelog"), resourceAccessor, database); + config.getString(Keys.DATABASE_CHANGELOG), resourceAccessor, database); liquibase.clearCheckSums(); diff --git a/src/main/java/org/traccar/web/ConsoleServlet.java b/src/main/java/org/traccar/web/ConsoleServlet.java index 2b38a935a..0f3dcd8fd 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 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2020 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. @@ -20,6 +20,7 @@ import org.h2.server.web.WebServlet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; +import org.traccar.config.Keys; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; @@ -39,9 +40,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("database.driver") + "|" - + Context.getConfig().getString("database.url") + "|" - + Context.getConfig().getString("database.user")); + + Context.getConfig().getString(Keys.DATABASE_DRIVER) + "|" + + Context.getConfig().getString(Keys.DATABASE_URL) + "|" + + Context.getConfig().getString(Keys.DATABASE_USER)); Method method; -- cgit v1.2.3