Refactored ConfigSystem

This commit is contained in:
Rsl1122 2017-12-12 10:23:37 +02:00
parent 78851ecf78
commit 63638874de
12 changed files with 319 additions and 45 deletions

View File

@ -44,8 +44,6 @@ import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.settings.locale.Locale; import main.java.com.djrapitops.plan.settings.locale.Locale;
import main.java.com.djrapitops.plan.settings.locale.Msg; import main.java.com.djrapitops.plan.settings.locale.Msg;
import main.java.com.djrapitops.plan.settings.theme.Theme; import main.java.com.djrapitops.plan.settings.theme.Theme;
import main.java.com.djrapitops.plan.systems.DatabaseSystem;
import main.java.com.djrapitops.plan.systems.FileSystem;
import main.java.com.djrapitops.plan.systems.Systems; import main.java.com.djrapitops.plan.systems.Systems;
import main.java.com.djrapitops.plan.systems.cache.DataCache; import main.java.com.djrapitops.plan.systems.cache.DataCache;
import main.java.com.djrapitops.plan.systems.cache.GeolocationCache; import main.java.com.djrapitops.plan.systems.cache.GeolocationCache;
@ -57,10 +55,12 @@ import main.java.com.djrapitops.plan.systems.listeners.*;
import main.java.com.djrapitops.plan.systems.processing.Processor; import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.processing.importing.importers.OfflinePlayerImporter; import main.java.com.djrapitops.plan.systems.processing.importing.importers.OfflinePlayerImporter;
import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue; import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue;
import main.java.com.djrapitops.plan.systems.store.FileSystem;
import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem;
import main.java.com.djrapitops.plan.systems.store.database.DBSystem;
import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer; import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer;
import main.java.com.djrapitops.plan.systems.webserver.PageCache; import main.java.com.djrapitops.plan.systems.webserver.PageCache;
import main.java.com.djrapitops.plan.systems.webserver.WebServer; import main.java.com.djrapitops.plan.systems.webserver.WebServer;
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
import main.java.com.djrapitops.plan.utilities.file.export.HtmlExport; import main.java.com.djrapitops.plan.utilities.file.export.HtmlExport;
import main.java.com.djrapitops.plan.utilities.metrics.BStats; import main.java.com.djrapitops.plan.utilities.metrics.BStats;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -84,7 +84,6 @@ public class Plan extends BukkitPlugin implements IPlan {
private API api; private API api;
private Config config;
private Theme theme; private Theme theme;
private Systems systems; private Systems systems;
@ -145,10 +144,7 @@ public class Plan extends BukkitPlugin implements IPlan {
try { try {
systems = new Systems(this); systems = new Systems(this);
FileSystem.getInstance().init(); FileSystem.getInstance().init();
ConfigSystem.getInstance().init();
config = new Config(FileSystem.getConfigFile());
config.copyDefaults(FileUtil.lines(this, "config.yml"));
config.save();
Log.setDebugMode(Settings.DEBUG.toString()); Log.setDebugMode(Settings.DEBUG.toString());
@ -186,7 +182,7 @@ public class Plan extends BukkitPlugin implements IPlan {
serverVariableHolder = new ServerVariableHolder(getServer()); serverVariableHolder = new ServerVariableHolder(getServer());
Benchmark.stop("Enable", "Reading server variables"); Benchmark.stop("Enable", "Reading server variables");
DatabaseSystem.getInstance().init(); DBSystem.getInstance().init();
Benchmark.start("WebServer Initialization"); Benchmark.start("WebServer Initialization");
webServer = new WebServer(this); webServer = new WebServer(this);
@ -359,11 +355,7 @@ public class Plan extends BukkitPlugin implements IPlan {
@Override @Override
public void onReload() { public void onReload() {
try { ConfigSystem.reload();
config.read();
} catch (IOException e) {
Log.toLog(this.getClass().getName(), e);
}
} }
private void registerListeners() { private void registerListeners() {
@ -393,7 +385,7 @@ public class Plan extends BukkitPlugin implements IPlan {
*/ */
@Deprecated @Deprecated
public Database getDB() { public Database getDB() {
return DatabaseSystem.getInstance().getActiveDatabase(); return DBSystem.getInstance().getActiveDatabase();
} }
/** /**
@ -473,7 +465,7 @@ public class Plan extends BukkitPlugin implements IPlan {
@Override @Override
public Config getMainConfig() { public Config getMainConfig() {
return config; return ConfigSystem.getInstance().getConfig();
} }
public InformationManager getInfoManager() { public InformationManager getInfoManager() {

View File

@ -23,8 +23,6 @@ import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.settings.locale.Locale; import main.java.com.djrapitops.plan.settings.locale.Locale;
import main.java.com.djrapitops.plan.settings.locale.Msg; import main.java.com.djrapitops.plan.settings.locale.Msg;
import main.java.com.djrapitops.plan.settings.theme.Theme; import main.java.com.djrapitops.plan.settings.theme.Theme;
import main.java.com.djrapitops.plan.systems.DatabaseSystem;
import main.java.com.djrapitops.plan.systems.FileSystem;
import main.java.com.djrapitops.plan.systems.Systems; import main.java.com.djrapitops.plan.systems.Systems;
import main.java.com.djrapitops.plan.systems.info.BungeeInformationManager; import main.java.com.djrapitops.plan.systems.info.BungeeInformationManager;
import main.java.com.djrapitops.plan.systems.info.InformationManager; import main.java.com.djrapitops.plan.systems.info.InformationManager;
@ -32,9 +30,11 @@ import main.java.com.djrapitops.plan.systems.info.server.BungeeServerInfoManager
import main.java.com.djrapitops.plan.systems.listeners.BungeePlayerListener; import main.java.com.djrapitops.plan.systems.listeners.BungeePlayerListener;
import main.java.com.djrapitops.plan.systems.processing.Processor; import main.java.com.djrapitops.plan.systems.processing.Processor;
import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue; import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue;
import main.java.com.djrapitops.plan.systems.store.FileSystem;
import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem;
import main.java.com.djrapitops.plan.systems.store.database.DBSystem;
import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer; import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer;
import main.java.com.djrapitops.plan.systems.webserver.WebServer; import main.java.com.djrapitops.plan.systems.webserver.WebServer;
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
import main.java.com.djrapitops.plan.utilities.file.export.HtmlExport; import main.java.com.djrapitops.plan.utilities.file.export.HtmlExport;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
@ -49,7 +49,6 @@ import java.util.UUID;
*/ */
public class PlanBungee extends BungeePlugin implements IPlan { public class PlanBungee extends BungeePlugin implements IPlan {
private Config config;
private Theme theme; private Theme theme;
private Systems systems; private Systems systems;
@ -67,10 +66,7 @@ public class PlanBungee extends BungeePlugin implements IPlan {
try { try {
systems = new Systems(this); systems = new Systems(this);
FileSystem.getInstance().init(); FileSystem.getInstance().init();
ConfigSystem.getInstance().init();
config = new Config(FileSystem.getConfigFile());
config.copyDefaults(FileUtil.lines(this, "bungeeconfig.yml"));
config.save();
Log.setDebugMode(Settings.DEBUG.toString()); Log.setDebugMode(Settings.DEBUG.toString());
@ -95,7 +91,7 @@ public class PlanBungee extends BungeePlugin implements IPlan {
theme = new Theme(); theme = new Theme();
DatabaseSystem.getInstance().init(); DBSystem.getInstance().init();
registerCommand("planbungee", new PlanBungeeCommand(this)); registerCommand("planbungee", new PlanBungeeCommand(this));
@ -181,17 +177,13 @@ public class PlanBungee extends BungeePlugin implements IPlan {
@Override @Override
public void onReload() { public void onReload() {
try { ConfigSystem.reload();
config.read();
} catch (IOException e) {
Log.toLog(this.getClass().getName(), e);
}
} }
@Override @Override
@Deprecated @Deprecated
public Database getDB() { public Database getDB() {
return DatabaseSystem.getInstance().getActiveDatabase(); return DBSystem.getInstance().getActiveDatabase();
} }
public BungeeServerInfoManager getServerInfoManager() { public BungeeServerInfoManager getServerInfoManager() {
@ -228,7 +220,7 @@ public class PlanBungee extends BungeePlugin implements IPlan {
@Override @Override
public Config getMainConfig() { public Config getMainConfig() {
return config; return ConfigSystem.getInstance().getConfig();
} }
@Override @Override

View File

@ -2,7 +2,7 @@ package main.java.com.djrapitops.plan.settings;
import com.djrapitops.plugin.api.Check; import com.djrapitops.plugin.api.Check;
import com.djrapitops.plugin.api.config.Config; import com.djrapitops.plugin.api.config.Config;
import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem;
import java.util.List; import java.util.List;
@ -154,7 +154,7 @@ public enum Settings {
} }
private Config getConfig() { private Config getConfig() {
return MiscUtils.getIPlan().getMainConfig(); return ConfigSystem.getInstance().getConfig();
} }
public static ServerSpecificSettings serverSpecific() { public static ServerSpecificSettings serverSpecific() {

View File

@ -4,7 +4,15 @@
*/ */
package main.java.com.djrapitops.plan.systems; package main.java.com.djrapitops.plan.systems;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.PlanBungee;
import main.java.com.djrapitops.plan.systems.store.FileSystem;
import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem;
import main.java.com.djrapitops.plan.systems.store.config.PlanBungeeConfigSystem;
import main.java.com.djrapitops.plan.systems.store.config.PlanConfigSystem;
import main.java.com.djrapitops.plan.systems.store.database.DBSystem;
import main.java.com.djrapitops.plan.systems.store.database.PlanBungeeDBSystem;
import main.java.com.djrapitops.plan.systems.store.database.PlanDBSystem;
import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.MiscUtils;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
@ -15,18 +23,38 @@ import org.apache.commons.lang3.ArrayUtils;
*/ */
public class Systems { public class Systems {
FileSystem fileSystem; private FileSystem fileSystem;
DatabaseSystem databaseSystem; private DBSystem databaseSystem;
private ConfigSystem configSystem;
public Systems(IPlan plugin) { /**
databaseSystem = new DatabaseSystem(); * Constructor for Bukkit version.
*
* @param plugin Plan instance
*/
public Systems(Plan plugin) {
fileSystem = new FileSystem(plugin); fileSystem = new FileSystem(plugin);
configSystem = new PlanConfigSystem();
databaseSystem = new PlanDBSystem();
}
/**
* Constructor for Bungee version.
*
* @param plugin PlanBungee instance
*/
public Systems(PlanBungee plugin) {
fileSystem = new FileSystem(plugin);
configSystem = new PlanBungeeConfigSystem();
databaseSystem = new PlanBungeeDBSystem();
} }
private SubSystem[] getSubSystems() { private SubSystem[] getSubSystems() {
return new SubSystem[]{ return new SubSystem[]{
databaseSystem, fileSystem,
fileSystem configSystem,
databaseSystem
}; };
} }
@ -41,4 +69,16 @@ public class Systems {
public static Systems getInstance() { public static Systems getInstance() {
return MiscUtils.getIPlan().getSystems(); return MiscUtils.getIPlan().getSystems();
} }
public FileSystem getFileSystem() {
return fileSystem;
}
public DBSystem getDatabaseSystem() {
return databaseSystem;
}
public ConfigSystem getConfigSystem() {
return configSystem;
}
} }

View File

@ -2,12 +2,18 @@
* Licence is provided in the jar as license.yml also here: * Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/ */
package main.java.com.djrapitops.plan.systems; package main.java.com.djrapitops.plan.systems.store;
import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.IPlan;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException; import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.systems.SubSystem;
import main.java.com.djrapitops.plan.systems.Systems;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.file.FileUtil;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.List;
/** /**
* //TODO Class Javadoc Comment * //TODO Class Javadoc Comment
@ -24,7 +30,7 @@ public class FileSystem implements SubSystem {
} }
public static FileSystem getInstance() { public static FileSystem getInstance() {
return Systems.getInstance().fileSystem; return Systems.getInstance().getFileSystem();
} }
public static File getDataFolder() { public static File getDataFolder() {
@ -35,6 +41,10 @@ public class FileSystem implements SubSystem {
return getInstance().configFile; return getInstance().configFile;
} }
public static List<String> readFromResource(String fileName) throws IOException {
return FileUtil.lines(MiscUtils.getIPlan(), fileName);
}
@Override @Override
public void init() throws PlanEnableException { public void init() throws PlanEnableException {
dataFolder.mkdirs(); dataFolder.mkdirs();

View File

@ -0,0 +1,58 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.store.config;
import com.djrapitops.plugin.api.config.Config;
import com.djrapitops.plugin.api.utility.log.Log;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.systems.SubSystem;
import main.java.com.djrapitops.plan.systems.Systems;
import main.java.com.djrapitops.plan.systems.store.FileSystem;
import java.io.IOException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public abstract class ConfigSystem implements SubSystem {
protected Config config;
public static ConfigSystem getInstance() {
return Systems.getInstance().getConfigSystem();
}
public Config getConfig() {
return config;
}
@Override
public void init() throws PlanEnableException {
try {
config = new Config(FileSystem.getConfigFile());
copyDefaults();
config.save();
} catch (IOException e) {
throw new PlanEnableException("Config Subsystem failed to initialize", e);
}
}
protected abstract void copyDefaults() throws IOException;
@Override
public void close() {
}
public static void reload() {
try {
getInstance().config.read();
} catch (IOException e) {
Log.toLog(ConfigSystem.class, e);
}
}
}

View File

@ -0,0 +1,22 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.store.config;
import main.java.com.djrapitops.plan.systems.store.FileSystem;
import java.io.IOException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class PlanBungeeConfigSystem extends ConfigSystem {
@Override
protected void copyDefaults() throws IOException {
config.copyDefaults(FileSystem.readFromResource("bungeeconfig.yml"));
}
}

View File

@ -0,0 +1,22 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.store.config;
import main.java.com.djrapitops.plan.systems.store.FileSystem;
import java.io.IOException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class PlanConfigSystem extends ConfigSystem {
@Override
protected void copyDefaults() throws IOException {
config.copyDefaults(FileSystem.readFromResource("config.yml"));
}
}

View File

@ -0,0 +1,83 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.store.database;
import com.djrapitops.plugin.api.Benchmark;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.settings.locale.Locale;
import main.java.com.djrapitops.plan.settings.locale.Msg;
import main.java.com.djrapitops.plan.systems.SubSystem;
import main.java.com.djrapitops.plan.systems.Systems;
import java.sql.SQLException;
import java.util.Set;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public abstract class DBSystem implements SubSystem {
protected Database db;
protected Set<Database> databases;
public static DBSystem getInstance() {
return Systems.getInstance().getDatabaseSystem();
}
@Override
public void init() throws PlanEnableException {
try {
Benchmark.start("Init Database");
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString());
initDatabase();
Log.info(Locale.get(Msg.ENABLE_DB_INFO).parse(db.getConfigName()));
Benchmark.stop("Systems", "Init Database");
} catch (DatabaseInitException e) {
throw new PlanEnableException(db.getName() + "-Database failed to initialize", e);
}
}
protected abstract void initDatabase() throws DatabaseInitException;
public Set<Database> getDatabases() {
return databases;
}
public void setDatabases(Set<Database> databases) {
this.databases = databases;
}
@Override
public void close() {
try {
if (db != null) {
db.close();
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
public Database getActiveDatabase() {
return db;
}
public static Database getActiveDatabase(String dbName) throws DatabaseInitException {
for (Database database : DBSystem.getInstance().getDatabases()) {
String dbConfigName = database.getConfigName();
if (Verify.equalsIgnoreCase(dbName, dbConfigName)) {
database.init();
return database;
}
}
throw new DatabaseInitException(Locale.get(Msg.ENABLE_FAIL_WRONG_DB) + " " + dbName);
}
}

View File

@ -0,0 +1,24 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.store.database;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.database.databases.MySQLDB;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class PlanBungeeDBSystem extends DBSystem {
@Override
protected void initDatabase() throws DatabaseInitException {
db = new MySQLDB();
databases.add(db);
db.init();
}
}

View File

@ -0,0 +1,31 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems.store.database;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.database.databases.MySQLDB;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.settings.Settings;
import java.util.HashSet;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class PlanDBSystem extends DBSystem {
@Override
protected void initDatabase() throws DatabaseInitException {
databases = new HashSet<>();
databases.add(new MySQLDB());
databases.add(new SQLiteDB());
String dbType = Settings.DB_TYPE.toString().toLowerCase().trim();
db = getActiveDatabase(dbType);
db.init();
}
}

View File

@ -6,7 +6,7 @@ import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.database.tables.move.BatchOperationTable; import main.java.com.djrapitops.plan.database.tables.move.BatchOperationTable;
import main.java.com.djrapitops.plan.systems.DatabaseSystem; import main.java.com.djrapitops.plan.systems.store.database.DBSystem;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
@ -76,6 +76,6 @@ public class ManageUtils {
@Deprecated @Deprecated
public static Database getDB(String dbName) throws DatabaseInitException { public static Database getDB(String dbName) throws DatabaseInitException {
return DatabaseSystem.getActiveDatabase(dbName); return DBSystem.getActiveDatabase(dbName);
} }
} }