Improve ServerInfo so that it will work when swapping from sqlite to mysql.

This commit is contained in:
Rsl1122 2017-08-21 13:30:19 +03:00
parent e44b20d74f
commit 54b9dc4374
7 changed files with 70 additions and 59 deletions

View File

@ -34,12 +34,12 @@ import main.java.com.djrapitops.plan.data.cache.DataCacheHandler;
import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler; import main.java.com.djrapitops.plan.data.cache.InspectCacheHandler;
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler; import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
import main.java.com.djrapitops.plan.data.listeners.*; import main.java.com.djrapitops.plan.data.listeners.*;
import main.java.com.djrapitops.plan.data.server.ServerInfoManager;
import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.MySQLDB; import main.java.com.djrapitops.plan.database.databases.MySQLDB;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.locale.Locale; import main.java.com.djrapitops.plan.locale.Locale;
import main.java.com.djrapitops.plan.locale.Msg; import main.java.com.djrapitops.plan.locale.Msg;
import main.java.com.djrapitops.plan.ui.theme.Theme;
import main.java.com.djrapitops.plan.ui.webserver.WebServer; import main.java.com.djrapitops.plan.ui.webserver.WebServer;
import main.java.com.djrapitops.plan.ui.webserver.api.bukkit.*; import main.java.com.djrapitops.plan.ui.webserver.api.bukkit.*;
import main.java.com.djrapitops.plan.utilities.Benchmark; import main.java.com.djrapitops.plan.utilities.Benchmark;
@ -77,6 +77,8 @@ public class Plan extends BukkitPlugin<Plan> {
private WebServer uiServer; private WebServer uiServer;
private ServerInfoManager serverInfoManager;
private ServerVariableHolder serverVariableHolder; private ServerVariableHolder serverVariableHolder;
private int bootAnalysisTaskID = -1; private int bootAnalysisTaskID = -1;
@ -185,6 +187,11 @@ public class Plan extends BukkitPlugin<Plan> {
if (!uiServer.isEnabled()) { if (!uiServer.isEnabled()) {
Log.error("WebServer was not successfully initialized."); Log.error("WebServer was not successfully initialized.");
} }
Benchmark.start("ServerInfo Registration");
serverInfoManager = new ServerInfoManager(this);
Benchmark.stop("Enable", "ServerInfo Registration");
setupFilter(); // TODO Move to RegisterCommand Constructor setupFilter(); // TODO Move to RegisterCommand Constructor
// Data view settings // TODO Rewrite. (TextUI removed & webserver might be running on bungee // Data view settings // TODO Rewrite. (TextUI removed & webserver might be running on bungee
@ -210,8 +217,6 @@ public class Plan extends BukkitPlugin<Plan> {
// BStats bStats = new BStats(this); // BStats bStats = new BStats(this);
// bStats.registerMetrics(); // bStats.registerMetrics();
Theme.test(); //TODO Remove
Log.debug("Verbose debug messages are enabled."); Log.debug("Verbose debug messages are enabled.");
Log.logDebug("Enable", Benchmark.stop("Enable", "Enable")); Log.logDebug("Enable", Benchmark.stop("Enable", "Enable"));
Log.info(Locale.get(Msg.ENABLED).toString()); Log.info(Locale.get(Msg.ENABLED).toString());

View File

@ -12,7 +12,7 @@ import java.util.UUID;
* @author Rsl1122 * @author Rsl1122
*/ */
public class ServerInfo { public class ServerInfo {
private final int id; private int id;
private final UUID uuid; private final UUID uuid;
private String name; private String name;
private String webAddress; private String webAddress;
@ -48,4 +48,7 @@ public class ServerInfo {
this.webAddress = webAddress; this.webAddress = webAddress;
} }
public void setId(int id) {
this.id = id;
}
} }

View File

@ -5,6 +5,7 @@
package main.java.com.djrapitops.plan.data.server; package main.java.com.djrapitops.plan.data.server;
import com.djrapitops.plugin.config.BukkitConfig; import com.djrapitops.plugin.config.BukkitConfig;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan; import main.java.com.djrapitops.plan.Plan;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -14,6 +15,7 @@ import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
/** /**
@ -31,7 +33,6 @@ public class ServerInfoFile extends BukkitConfig<Plan> {
FileConfigurationOptions options = config.options(); FileConfigurationOptions options = config.options();
options.copyDefaults(true); options.copyDefaults(true);
options.header("IMPORTANT: Do not edit this file unless you want to lose your data!"); options.header("IMPORTANT: Do not edit this file unless you want to lose your data!");
config.addDefault("Server.ID", -1);
config.addDefault("Server.UUID", ""); config.addDefault("Server.UUID", "");
config.addDefault("Bungee.WebAddress", ""); config.addDefault("Bungee.WebAddress", "");
config.addDefault("Bungee.Fail", 0); config.addDefault("Bungee.Fail", 0);
@ -39,29 +40,30 @@ public class ServerInfoFile extends BukkitConfig<Plan> {
} }
public void saveInfo(ServerInfo thisServer, ServerInfo bungee) throws IOException { public void saveInfo(ServerInfo thisServer, ServerInfo bungee) throws IOException {
FileConfiguration config = getConfig();
Map<String, Serializable> serverMap = new HashMap<>(); Map<String, Serializable> serverMap = new HashMap<>();
Map<String, Serializable> bungeeMap = new HashMap<>(); Map<String, Serializable> bungeeMap = new HashMap<>();
serverMap.put("ID", thisServer.getId());
serverMap.put("UUID", thisServer.getUuid().toString()); serverMap.put("UUID", thisServer.getUuid().toString());
config.set("Server", serverMap);
bungeeMap.put("WebAddress", bungee.getWebAddress()); String oldAddress = config.getString("Bungee.WebAddress");
String newAddress = bungee.getWebAddress();
getConfig().set("Server", serverMap); if (!newAddress.equals(oldAddress)) {
getConfig().set("Bungee", bungeeMap); bungeeMap.put("Fail", 0);
bungeeMap.put("WebAddress", newAddress);
config.set("Bungee", bungeeMap);
}
save(); save();
} }
public int getID() { public Optional<UUID> getUUID() {
return getConfig().getInt("Server.ID");
}
public UUID getUUID() {
String uuidString = getConfig().getString("Server.UUID"); String uuidString = getConfig().getString("Server.UUID");
if (uuidString == null) { if (Verify.isEmpty(uuidString)) {
return null; return Optional.empty();
} }
return UUID.fromString(uuidString); return Optional.of(UUID.fromString(uuidString));
} }
public String getBungeeWebAddress() { public String getBungeeWebAddress() {

View File

@ -19,7 +19,9 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
/** /**
* //TODO Class Javadoc Comment * Manages the Server information required for Bungee-Bukkit WebAPI connection.
* <p>
* Also manages Server ID required for MySQL database independence.
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@ -33,9 +35,8 @@ public class ServerInfoManager {
public ServerInfoManager(Plan plugin) { public ServerInfoManager(Plan plugin) {
this.plugin = plugin; this.plugin = plugin;
Database db = plugin.getDB(); Database db = plugin.getDB();
if ("sqlite".equals(db.getConfigName())) { serverTable = db.getServerTable();
return;
}
try { try {
serverInfoFile = new ServerInfoFile(plugin); serverInfoFile = new ServerInfoFile(plugin);
} catch (IOException | InvalidConfigurationException e) { } catch (IOException | InvalidConfigurationException e) {
@ -44,39 +45,52 @@ public class ServerInfoManager {
plugin.disablePlugin(); plugin.disablePlugin();
} }
serverTable = db.getServerTable(); Optional<UUID> serverUUID = serverInfoFile.getUUID();
int serverID = serverInfoFile.getID();
try { try {
if (serverID == -1) { if (serverUUID.isPresent()) {
registerServer(); updateDbInfo(serverUUID.get());
} else { } else {
updateDbInfo(serverID); registerServer();
} }
} catch (SQLException e) { } catch (Exception e) {
Log.toLog(this.getClass().getName(), e); Log.toLog(this.getClass().getName(), e);
Log.error("Failed to register server info to database, disabling plugin.");
plugin.disablePlugin();
} }
} }
private void updateDbInfo(int serverID) throws SQLException { private void updateDbInfo(UUID serverUUID) throws SQLException {
UUID uuid = serverInfoFile.getUUID(); Optional<Integer> serverID = serverTable.getServerID(serverUUID);
if (!serverID.isPresent()) {
registerServer(serverUUID);
return;
}
String name = Settings.SERVER_NAME.toString(); String name = Settings.SERVER_NAME.toString();
String webAddress = plugin.getUiServer().getAccessAddress(); String webAddress = plugin.getUiServer().getAccessAddress();
if ("plan".equalsIgnoreCase(name)) { if ("plan".equalsIgnoreCase(name)) {
name = "Server" + Integer.toString(serverID); name = "Server" + serverID.get();
} }
serverInfo = new ServerInfo(serverID, uuid, name, webAddress); serverInfo = new ServerInfo(serverID.get(), serverUUID, name, webAddress);
serverTable.saveCurrentServerInfo(serverInfo); serverTable.saveCurrentServerInfo(serverInfo);
} }
private void registerServer() throws SQLException { private void registerServer() throws SQLException {
UUID serverUUID = generateNewUUID(plugin.getServer()); registerServer(generateNewUUID(plugin.getServer()));
}
private void registerServer(UUID serverUUID) throws SQLException {
String webAddress = plugin.getUiServer().getAccessAddress(); String webAddress = plugin.getUiServer().getAccessAddress();
String name = Settings.SERVER_NAME.toString(); String name = Settings.SERVER_NAME.toString();
serverInfo = new ServerInfo(-1, serverUUID, name, webAddress); serverInfo = new ServerInfo(-1, serverUUID, name, webAddress);
serverTable.saveCurrentServerInfo(serverInfo); serverTable.saveCurrentServerInfo(serverInfo);
Optional<Integer> serverID = serverTable.getServerID(serverUUID);
if (serverID.isPresent()) {
serverInfo.setId(serverID.get());
} else {
throw new IllegalStateException("Failed to Register Server (ID not found)");
}
} }
private UUID generateNewUUID(Server server) { private UUID generateNewUUID(Server server) {
@ -96,10 +110,18 @@ public class ServerInfoManager {
return Optional.empty(); return Optional.empty();
} }
public void saveBungeeConnectionAddress(String address) throws IOException {
serverInfoFile.saveInfo(serverInfo, new ServerInfo(-1, null, "Bungee", address));
}
public int getServerID() { public int getServerID() {
return serverInfo.getId(); return serverInfo.getId();
} }
public UUID getServerUUID() {
return serverInfo.getUuid();
}
public String getServerName() { public String getServerName() {
return serverInfo.getName(); return serverInfo.getName();
} }

View File

@ -56,6 +56,7 @@ public abstract class SQLDB extends Database {
securityTable = new SecurityTable(this, usingMySQL); securityTable = new SecurityTable(this, usingMySQL);
worldTable = new WorldTable(this, usingMySQL); worldTable = new WorldTable(this, usingMySQL);
worldTimesTable = new WorldTimesTable(this, usingMySQL); worldTimesTable = new WorldTimesTable(this, usingMySQL);
serverTable = new ServerTable(this, usingMySQL);
startConnectionPingTask(); startConnectionPingTask();
} }

View File

@ -19,7 +19,9 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
/** /**
* //TODO Class Javadoc Comment * Table representing plan_servers in the database.
* <p>
* Used for managing multiple server's data in the database.
* *
* @author Rsl1122 * @author Rsl1122
*/ */
@ -31,7 +33,7 @@ public class ServerTable extends Table {
private final String columnWebserverAddress; private final String columnWebserverAddress;
private final String columnInstalled; private final String columnInstalled;
public ServerTable(String name, SQLDB db, boolean usingMySQL) { public ServerTable(SQLDB db, boolean usingMySQL) {
super("plan_servers", db, usingMySQL); super("plan_servers", db, usingMySQL);
columnServerID = "id"; columnServerID = "id";
columnServerUUID = "uuid"; columnServerUUID = "uuid";

View File

@ -5,13 +5,6 @@
package main.java.com.djrapitops.plan.ui.theme; package main.java.com.djrapitops.plan.ui.theme;
import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;
/** /**
* Enum that contains available themes. * Enum that contains available themes.
@ -78,23 +71,6 @@ public enum Theme {
return replaced; return replaced;
} }
// TODO Remove
public static void test() throws IOException {
String serverHtml = HtmlUtils.getStringFromResource("server - Example.html");
String css = HtmlUtils.getStringFromResource("main.css");
File folder = new File(Plan.getInstance().getDataFolder(), "themes");
folder.mkdirs();
for (Theme t : Theme.values()) {
File themeFolder = new File(folder, t.name());
themeFolder.mkdirs();
File themeHtml = new File(themeFolder, "server.html");
File themeCss = new File(themeFolder, "main.css");
Files.write(themeHtml.toPath(), Collections.singletonList(t.replaceThemeColors(serverHtml)));
Files.write(themeCss.toPath(), Collections.singletonList(t.replaceThemeColors(css)));
}
}
public static String replaceColors(String resourceString) { public static String replaceColors(String resourceString) {
Theme def = Theme.DEFAULT; Theme def = Theme.DEFAULT;
String replaced = resourceString; String replaced = resourceString;