feat(redis): rework configuration to support multiple data sources
This commit is contained in:
parent
e00875faa2
commit
46aa3225d3
11 changed files with 103 additions and 94 deletions
|
@ -1,36 +1,40 @@
|
|||
package net.artelnatif.nicko.config;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Configuration {
|
||||
private final String address;
|
||||
private final String username;
|
||||
private final String password;
|
||||
@JsonProperty("sql")
|
||||
private final DataSourceConfiguration sqlConfiguration;
|
||||
@JsonProperty("redis")
|
||||
private final DataSourceConfiguration redisConfiguration;
|
||||
private final String prefix;
|
||||
private final Boolean local;
|
||||
private final Boolean customLocale;
|
||||
|
||||
public Configuration(String address, String username, String password, String prefix, Boolean local, Boolean customLocale) {
|
||||
this.address = address;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
public Configuration(DataSourceConfiguration sqlConfiguration, DataSourceConfiguration redisConfiguration, String prefix, Boolean local, Boolean customLocale) {
|
||||
this.sqlConfiguration = sqlConfiguration;
|
||||
this.redisConfiguration = redisConfiguration;
|
||||
this.prefix = prefix;
|
||||
this.local = local;
|
||||
this.customLocale = customLocale;
|
||||
}
|
||||
|
||||
public Configuration() {
|
||||
this("", "", "", "", false, false);
|
||||
this(
|
||||
new DataSourceConfiguration("", 3306, "", ""),
|
||||
new DataSourceConfiguration("", 6379, "", ""),
|
||||
"",
|
||||
false,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
public DataSourceConfiguration getSqlConfiguration() {
|
||||
return sqlConfiguration;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
public DataSourceConfiguration getRedisConfiguration() {
|
||||
return redisConfiguration;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.artelnatif.nicko.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
|
||||
|
@ -15,6 +16,7 @@ public class ConfigurationManager {
|
|||
|
||||
public ConfigurationManager(File directory) {
|
||||
this.file = new File(directory, "config.yml");
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
}
|
||||
|
||||
public void save(Configuration configuration) throws IOException {
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
package net.artelnatif.nicko.config;
|
||||
|
||||
public class DataSourceConfiguration {
|
||||
public static final DataSourceConfiguration SQL_EMPTY = new DataSourceConfiguration("127.0.0.1", 3306, "root", "");
|
||||
public static final DataSourceConfiguration REDIS_EMPTY = new DataSourceConfiguration("127.0.0.1", 6379, "", "");
|
||||
|
||||
private final String address;
|
||||
private final String port;
|
||||
private final Integer port;
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
public DataSourceConfiguration(String address, String port, String username, String password) {
|
||||
public DataSourceConfiguration(String address, Integer port, String username, String password) {
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
this.username = username;
|
||||
|
@ -17,7 +20,7 @@ public class DataSourceConfiguration {
|
|||
return address;
|
||||
}
|
||||
|
||||
public String getPort() {
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
package net.artelnatif.nicko.config;
|
||||
|
||||
public class NewConfiguration {
|
||||
private final DataSourceConfiguration sqlConfiguration;
|
||||
private final DataSourceConfiguration redisConfiguration;
|
||||
private final String prefix;
|
||||
private final Boolean local;
|
||||
private final Boolean customLocale;
|
||||
|
||||
public NewConfiguration(DataSourceConfiguration sqlConfiguration, DataSourceConfiguration redisConfiguration, String prefix, Boolean local, Boolean customLocale) {
|
||||
this.sqlConfiguration = sqlConfiguration;
|
||||
this.redisConfiguration = redisConfiguration;
|
||||
this.prefix = prefix;
|
||||
this.local = local;
|
||||
this.customLocale = customLocale;
|
||||
}
|
||||
|
||||
public NewConfiguration() {
|
||||
this(
|
||||
new DataSourceConfiguration("", "", "", ""),
|
||||
new DataSourceConfiguration("", "", "", ""),
|
||||
"",
|
||||
false,
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
public DataSourceConfiguration getSqlConfiguration() {
|
||||
return sqlConfiguration;
|
||||
}
|
||||
|
||||
public DataSourceConfiguration getRedisConfiguration() {
|
||||
return redisConfiguration;
|
||||
}
|
||||
|
||||
public String getPrefix() {
|
||||
return prefix;
|
||||
}
|
||||
|
||||
public Boolean isLocal() {
|
||||
return local;
|
||||
}
|
||||
|
||||
public Boolean isCustomLocale() {
|
||||
return customLocale;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package net.artelnatif.nicko.storage.sql;
|
||||
|
||||
import net.artelnatif.nicko.config.Configuration;
|
||||
import net.artelnatif.nicko.config.DataSourceConfiguration;
|
||||
import net.artelnatif.nicko.storage.StorageProvider;
|
||||
import org.mariadb.jdbc.MariaDbDataSource;
|
||||
|
||||
|
@ -25,9 +26,10 @@ public class SQLStorageProvider implements StorageProvider {
|
|||
public boolean init() {
|
||||
try {
|
||||
final MariaDbDataSource dataSource = new MariaDbDataSource();
|
||||
dataSource.setUrl("jdbc:mariadb://" + configuration.getAddress());
|
||||
dataSource.setUser(configuration.getUsername());
|
||||
dataSource.setPassword(configuration.getPassword());
|
||||
final DataSourceConfiguration dataSourceConfiguration = configuration.getSqlConfiguration();
|
||||
dataSource.setUrl("jdbc:mariadb://" + dataSourceConfiguration.getAddress() + ":" + dataSourceConfiguration.getPort());
|
||||
dataSource.setUser(dataSourceConfiguration.getUsername());
|
||||
dataSource.setPassword(dataSourceConfiguration.getPassword());
|
||||
connection = dataSource.getConnection();
|
||||
final boolean initialized = connection != null && !connection.isClosed();
|
||||
|
||||
|
|
|
@ -9,18 +9,20 @@
|
|||
# Accepted values: false (Disabled), true (Enabled)
|
||||
local: true
|
||||
|
||||
# SQL database's address
|
||||
# Accepted values: valid IP address (e.g. localhost, 127.0.0.1)
|
||||
address: "localhost"
|
||||
# SQL database's port
|
||||
# Accepted values: valid integer (e.g. 3306, 25565)
|
||||
port: 3306
|
||||
# SQL database's username.
|
||||
# Accepted values: any string
|
||||
username: "username"
|
||||
# SQL database's password.
|
||||
# Accepted values: any string
|
||||
password: "password"
|
||||
# This configuration section manages SQL.
|
||||
sql:
|
||||
# SQL database's address
|
||||
# Accepted values: valid IP address (e.g. localhost, 127.0.0.1)
|
||||
address: "localhost"
|
||||
# SQL database's port
|
||||
# Accepted values: valid integer (e.g. 3306, 25565)
|
||||
port: 3306
|
||||
# SQL database's username.
|
||||
# Accepted values: any string
|
||||
username: "username"
|
||||
# SQL database's password.
|
||||
# Accepted values: any string
|
||||
password: "password"
|
||||
|
||||
# This configuration section manages Redis (BungeeCord support).
|
||||
# It is used to transfer data between multiple servers.
|
||||
|
|
|
@ -3,6 +3,7 @@ package net.artelnatif.nicko.test;
|
|||
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||
import net.artelnatif.nicko.NickoBukkit;
|
||||
import net.artelnatif.nicko.config.Configuration;
|
||||
import net.artelnatif.nicko.config.DataSourceConfiguration;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
public class NickoPluginTest {
|
||||
|
@ -11,9 +12,8 @@ public class NickoPluginTest {
|
|||
@BeforeAll
|
||||
public static void setup() {
|
||||
final Configuration config = new Configuration(
|
||||
"",
|
||||
"",
|
||||
"",
|
||||
DataSourceConfiguration.SQL_EMPTY,
|
||||
DataSourceConfiguration.REDIS_EMPTY,
|
||||
"",
|
||||
true,
|
||||
false);
|
||||
|
|
|
@ -5,8 +5,12 @@ import be.seeseemelk.mockbukkit.ServerMock;
|
|||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||
import net.artelnatif.nicko.NickoBukkit;
|
||||
import net.artelnatif.nicko.config.Configuration;
|
||||
import net.artelnatif.nicko.config.DataSourceConfiguration;
|
||||
import net.artelnatif.nicko.disguise.NickoProfile;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
|
@ -20,9 +24,8 @@ public class CacheStorageTest {
|
|||
@BeforeAll
|
||||
public static void setup() {
|
||||
final Configuration config = new Configuration(
|
||||
"127.0.0.1",
|
||||
"root",
|
||||
"12345",
|
||||
new DataSourceConfiguration("127.0.0.1", 3306, "root", "12345"),
|
||||
DataSourceConfiguration.REDIS_EMPTY,
|
||||
"",
|
||||
false,
|
||||
false);
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package net.artelnatif.nicko.test.config;
|
||||
|
||||
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||
import net.artelnatif.nicko.NickoBukkit;
|
||||
import net.artelnatif.nicko.config.Configuration;
|
||||
import net.artelnatif.nicko.config.DataSourceConfiguration;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ConfigurationTest {
|
||||
private static NickoBukkit plugin;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
final Configuration config = new Configuration(
|
||||
DataSourceConfiguration.SQL_EMPTY,
|
||||
DataSourceConfiguration.REDIS_EMPTY,
|
||||
"",
|
||||
true,
|
||||
false);
|
||||
MockBukkit.mock();
|
||||
plugin = MockBukkit.load(NickoBukkit.class, config);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Read configuration")
|
||||
public void readConfiguration() {
|
||||
final Configuration configuration = plugin.getNickoConfig();
|
||||
assertTrue(configuration.isLocal());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void shutdown() {
|
||||
MockBukkit.unmock();
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ import be.seeseemelk.mockbukkit.ServerMock;
|
|||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||
import net.artelnatif.nicko.NickoBukkit;
|
||||
import net.artelnatif.nicko.config.Configuration;
|
||||
import net.artelnatif.nicko.config.DataSourceConfiguration;
|
||||
import net.artelnatif.nicko.disguise.ActionResult;
|
||||
import net.artelnatif.nicko.disguise.NickoProfile;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
@ -22,9 +23,8 @@ public class BrokenSQLTest {
|
|||
@BeforeAll
|
||||
public static void setup() {
|
||||
final Configuration config = new Configuration(
|
||||
"127.0.0.1",
|
||||
"root",
|
||||
"INVALID_PASSWORD",
|
||||
DataSourceConfiguration.SQL_EMPTY,
|
||||
DataSourceConfiguration.REDIS_EMPTY,
|
||||
"",
|
||||
false,
|
||||
false);
|
||||
|
|
|
@ -4,6 +4,7 @@ import be.seeseemelk.mockbukkit.MockBukkit;
|
|||
import be.seeseemelk.mockbukkit.ServerMock;
|
||||
import be.seeseemelk.mockbukkit.entity.PlayerMock;
|
||||
import net.artelnatif.nicko.NickoBukkit;
|
||||
import net.artelnatif.nicko.config.DataSourceConfiguration;
|
||||
import net.artelnatif.nicko.i18n.Locale;
|
||||
import net.artelnatif.nicko.config.Configuration;
|
||||
import net.artelnatif.nicko.disguise.ActionResult;
|
||||
|
@ -24,9 +25,8 @@ public class SQLStorageTest {
|
|||
@BeforeAll
|
||||
public static void setup() {
|
||||
final Configuration config = new Configuration(
|
||||
"127.0.0.1",
|
||||
"root",
|
||||
"12345",
|
||||
new DataSourceConfiguration("127.0.0.1", 3306, "root", "12345"),
|
||||
DataSourceConfiguration.REDIS_EMPTY,
|
||||
"",
|
||||
false,
|
||||
false);
|
||||
|
|
Loading…
Reference in a new issue