From b9ee7dfc41a9b7a31e1aeb41e5b45e7b506f4596 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Mon, 27 Apr 2020 00:59:35 +0200 Subject: [PATCH] Add more information on failed database connection attempts --- Minepacks/pom.xml | 2 +- .../Minepacks/Bukkit/Database/Database.java | 55 ++++++----- .../Minepacks/Bukkit/Database/SQL.java | 3 +- .../Minepacks/Bukkit/Minepacks.java | 6 ++ .../SpecialInfoWorker/NoDatabaseWorker.java | 94 +++++++++++++++++++ .../SpecialInfoWorker/SpecialInfoBase.java | 53 +++++++++++ pom.xml | 2 +- 7 files changed, 189 insertions(+), 26 deletions(-) create mode 100644 Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/NoDatabaseWorker.java create mode 100644 Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/SpecialInfoBase.java diff --git a/Minepacks/pom.xml b/Minepacks/pom.xml index 4add09c..66a8e26 100644 --- a/Minepacks/pom.xml +++ b/Minepacks/pom.xml @@ -36,7 +36,7 @@ at.pcgamingfreaks PluginLib - 1.0.22-SNAPSHOT + 1.0.23-SNAPSHOT diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java index 3787497..e43100e 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Database.java @@ -84,39 +84,48 @@ public void close() unCacheStrategie.close(); } - public static Database getDatabase(Minepacks plugin) + public static @Nullable Database getDatabase(Minepacks plugin) { - String dbType = plugin.getConfiguration().getDatabaseType().toLowerCase(Locale.ROOT); - ConnectionProvider connectionProvider = null; - if(dbType.equals("shared") || dbType.equals("external") || dbType.equals("global")) + try { + String dbType = plugin.getConfiguration().getDatabaseType().toLowerCase(Locale.ROOT); + ConnectionProvider connectionProvider = null; + if(dbType.equals("shared") || dbType.equals("external") || dbType.equals("global")) + { /*if[STANDALONE] plugin.getLogger().warning(ConsoleColor.RED + "The shared database connection option is not available in standalone mode!" + ConsoleColor.RESET); return null; else[STANDALONE]*/ - at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool pool = at.pcgamingfreaks.PluginLib.Bukkit.PluginLib.getInstance().getDatabaseConnectionPool(); - if(pool == null) - { - plugin.getLogger().warning(ConsoleColor.RED + "The shared connection pool is not initialized correctly!" + ConsoleColor.RESET); - return null; + at.pcgamingfreaks.PluginLib.Database.DatabaseConnectionPool pool = at.pcgamingfreaks.PluginLib.Bukkit.PluginLib.getInstance().getDatabaseConnectionPool(); + if(pool == null) + { + plugin.getLogger().warning(ConsoleColor.RED + "The shared connection pool is not initialized correctly!" + ConsoleColor.RESET); + return null; + } + dbType = pool.getDatabaseType().toLowerCase(Locale.ROOT); + connectionProvider = pool.getConnectionProvider(); + /*end[STANDALONE]*/ } - dbType = pool.getDatabaseType().toLowerCase(Locale.ROOT); - connectionProvider = pool.getConnectionProvider(); - /*end[STANDALONE]*/ + Database database; + switch(dbType) + { + case "mysql": database = new MySQL(plugin, connectionProvider); break; + case "sqlite": database = new SQLite(plugin, connectionProvider); break; + case "flat": + case "file": + case "files": + database = new Files(plugin); break; + default: plugin.getLogger().warning(String.format(MESSAGE_UNKNOWN_DB_TYPE, plugin.getConfiguration().getDatabaseType())); return null; + } + database.init(); + return database; } - Database database; - switch(dbType) + catch(IllegalStateException ignored) {} + catch(Exception e) { - case "mysql": database = new MySQL(plugin, connectionProvider); break; - case "sqlite": database = new SQLite(plugin, connectionProvider); break; - case "flat": - case "file": - case "files": - database = new Files(plugin); break; - default: plugin.getLogger().warning(String.format(MESSAGE_UNKNOWN_DB_TYPE, plugin.getConfiguration().getDatabaseType())); return null; + e.printStackTrace(); } - database.init(); - return database; + return null; } public void backup(@NotNull Backpack backpack) diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java index 95f8e86..985127a 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/SQL.java @@ -37,7 +37,7 @@ public abstract class SQL extends Database { - private ConnectionProvider dataSource; + private final ConnectionProvider dataSource; protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names protected String fieldPlayerName, fieldPlayerID, fieldPlayerUUID, fieldBpOwner, fieldBpIts, fieldBpVersion, fieldBpLastUpdate, fieldCdPlayer, fieldCdTime; // Table Fields @@ -50,6 +50,7 @@ public SQL(@NotNull Minepacks plugin, @NotNull ConnectionProvider connectionProv super(plugin); dataSource = connectionProvider; + if(!dataSource.isAvailable()) throw new IllegalStateException("Failed to initialize database connection!"); loadSettings(); buildQuerys(); diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java index 94d2b98..f58ed7d 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java @@ -33,6 +33,7 @@ import at.pcgamingfreaks.Minepacks.Bukkit.Database.Helper.WorldBlacklistMode; import at.pcgamingfreaks.Minepacks.Bukkit.Database.Language; import at.pcgamingfreaks.Minepacks.Bukkit.Listener.*; +import at.pcgamingfreaks.Minepacks.Bukkit.SpecialInfoWorker.NoDatabaseWorker; import at.pcgamingfreaks.StringUtils; import at.pcgamingfreaks.Updater.UpdateProviders.BukkitUpdateProvider; import at.pcgamingfreaks.Updater.UpdateProviders.JenkinsUpdateProvider; @@ -177,6 +178,11 @@ private void load() { lang.load(config); database = Database.getDatabase(this); + if(database == null) + { + new NoDatabaseWorker(this); + return; + } maxSize = config.getBackpackMaxSize(); at.pcgamingfreaks.Minepacks.Bukkit.Backpack.setShrinkApproach(config.getShrinkApproach()); at.pcgamingfreaks.Minepacks.Bukkit.Backpack.setTitle(config.getBPTitle(), config.getBPTitleOther()); diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/NoDatabaseWorker.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/NoDatabaseWorker.java new file mode 100644 index 0000000..ee2a93e --- /dev/null +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/NoDatabaseWorker.java @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2020 GeorgH93 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package at.pcgamingfreaks.Minepacks.Bukkit.SpecialInfoWorker; + +import at.pcgamingfreaks.Bukkit.Message.Message; +import at.pcgamingfreaks.Bukkit.Message.MessageBuilder; +import at.pcgamingfreaks.Bukkit.RegisterablePluginCommand; +import at.pcgamingfreaks.Message.MessageColor; +import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; +import at.pcgamingfreaks.Minepacks.Bukkit.Permissions; +import at.pcgamingfreaks.Reflection; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +/** + * This worker will inform the admin that the plugin failed to connect to the database, he hopefully is able to solve the problem. + * It registers a new command that only allows to reload the plugin to prevent unnecessary downtime for the server cause of restarts. + */ +public class NoDatabaseWorker extends SpecialInfoBase implements CommandExecutor +{ + private final Minepacks plugin; + private final RegisterablePluginCommand command; + private final Message messageDBProblem; + + public NoDatabaseWorker(final @NotNull Minepacks plugin) + { + super(plugin, Permissions.RELOAD); + this.plugin = plugin; + Bukkit.getPluginManager().registerEvents(this, plugin); + command = new RegisterablePluginCommand(plugin, "backpack"); + command.registerCommand(); + command.setExecutor(this); + messageDBProblem = new MessageBuilder("Minepacks", MessageColor.GOLD).append(" failed to connect to its database!", MessageColor.RED).appendNewLine() + .append("Please check your configuration and reload the plugin (", MessageColor.RED).append("/backpack reload", MessageColor.BLUE).command("/backpack reload").append(")!", MessageColor.RED).getMessage(); + } + + @Override + protected void sendMessage(Player player) + { + messageDBProblem.send(player); + } + + @Override + public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command cmd, @NotNull String s, String[] strings) + { + if(strings.length != 1 || !strings[0].equalsIgnoreCase("reload")) + { + commandSender.sendMessage(MessageColor.RED + "Only \"/backpack reload\" is available at the moment!"); + } + else + { + if(commandSender.hasPermission(Permissions.RELOAD)) + { + command.unregisterCommand(); + HandlerList.unregisterAll(this); + try + { + plugin.getConfiguration().reload(); + Reflection.getMethod(plugin.getClass(), "load").invoke(plugin); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + else + { + commandSender.sendMessage(MessageColor.RED + "You don't have the permission to do that!"); + } + } + return true; + } +} \ No newline at end of file diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/SpecialInfoBase.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/SpecialInfoBase.java new file mode 100644 index 0000000..57a29b0 --- /dev/null +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/SpecialInfoWorker/SpecialInfoBase.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2020 GeorgH93 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package at.pcgamingfreaks.Minepacks.Bukkit.SpecialInfoWorker; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.plugin.java.JavaPlugin; + +public abstract class SpecialInfoBase implements Listener +{ + private final JavaPlugin plugin; + private final String permission; + + protected SpecialInfoBase(final JavaPlugin plugin, final String permission) + { + this.plugin = plugin; + this.permission = permission; + } + + @EventHandler + public void onJoin(final PlayerJoinEvent event) + { + if(event.getPlayer().hasPermission(permission)) + { + Bukkit.getScheduler().runTaskLater(plugin, () -> { + if(event.getPlayer().isOnline()) + { + sendMessage(event.getPlayer()); + } + }, 3 * 20L); // Run with a 3 seconds delay + } + } + + protected abstract void sendMessage(final Player player); +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 79eda93..01fa16a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ pom - 2.3.5 + 2.3.6-RC1 UTF-8 UTF-8