From c0f1dfc8c5f6b37cf41cb656195eb4fe4faa310e Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Wed, 29 Aug 2018 10:13:37 +0300 Subject: [PATCH] Removed all getInstance method calls related to InfoSystem. --- .../java/com/djrapitops/plan/PlanBungee.java | 6 +- .../java/com/djrapitops/plan/PlanSponge.java | 5 +- .../plan/command/commands/InspectCommand.java | 25 ++++++++- .../djrapitops/plan/system/DebugChannels.java | 11 ++++ .../plan/system/info/BungeeInfoSystem.java | 16 ++++-- .../plan/system/info/InfoSystem.java | 36 ++++++------ .../plan/system/info/ServerInfoSystem.java | 4 +- .../connection/BungeeConnectionSystem.java | 9 ++- .../system/info/connection/ConnectionLog.java | 32 ++--------- .../info/connection/ConnectionSystem.java | 8 --- .../connection/InfoRequestPageHandler.java | 2 +- .../connection/ServerConnectionSystem.java | 7 ++- .../info/connection/WebExceptionLogger.java | 40 +++++++++----- .../info/InspectCacheRequestProcessor.java | 39 +++++-------- .../info/NetworkPageUpdateProcessor.java | 4 +- .../info/PlayerPageUpdateProcessor.java | 9 ++- .../system/settings/PlanErrorManager.java | 55 ------------------- .../tasks/bungee/EnableConnectionTask.java | 9 ++- .../system/tasks/server/BootAnalysisTask.java | 9 ++- .../tasks/server/NetworkPageRefreshTask.java | 11 +++- .../tasks/server/PeriodicAnalysisTask.java | 9 ++- .../plan/system/webserver/WebServer.java | 8 --- .../system/webserver/WebServerSystem.java | 14 +---- .../webserver/pages/ServerPageHandler.java | 2 +- .../response/pages/AnalysisPageResponse.java | 4 +- .../plan/utilities/metrics/BStatsBungee.java | 13 +++-- .../plan/utilities/metrics/BStatsSponge.java | 12 ++-- 27 files changed, 191 insertions(+), 208 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/system/DebugChannels.java delete mode 100644 Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java diff --git a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java index 0a48885a6..da38230af 100644 --- a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java +++ b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java @@ -97,7 +97,11 @@ public class PlanBungee extends BungeePlugin implements PlanPlugin { locale = system.getLocaleSystem().getLocale(); system.enable(); - new BStatsBungee(this).registerMetrics(); + new BStatsBungee( + this, + system.getDatabaseSystem().getActiveDatabase(), + system.getInfoSystem().getConnectionSystem() + ).registerMetrics(); logger.info(locale.getString(PluginLang.ENABLED)); } catch (AbstractMethodError e) { diff --git a/Plan/src/main/java/com/djrapitops/plan/PlanSponge.java b/Plan/src/main/java/com/djrapitops/plan/PlanSponge.java index 60fc04412..20a88ef6b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/PlanSponge.java +++ b/Plan/src/main/java/com/djrapitops/plan/PlanSponge.java @@ -120,7 +120,10 @@ public class PlanSponge extends SpongePlugin implements PlanPlugin { locale = system.getLocaleSystem().getLocale(); system.enable(); - new BStatsSponge(metrics).registerMetrics(); + new BStatsSponge( + metrics, + system.getDatabaseSystem().getActiveDatabase() + ).registerMetrics(); logger.info(locale.getString(PluginLang.ENABLED)); } catch (AbstractMethodError e) { diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java index 5e47bd2a1..16f048bad 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/InspectCommand.java @@ -2,6 +2,7 @@ package com.djrapitops.plan.command.commands; import com.djrapitops.plan.api.exceptions.database.DBOpException; import com.djrapitops.plan.system.database.databases.Database; +import com.djrapitops.plan.system.info.connection.ConnectionSystem; import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.system.locale.lang.CmdHelpLang; import com.djrapitops.plan.system.locale.lang.CommandLang; @@ -33,7 +34,8 @@ public class InspectCommand extends CommandNode { private final Locale locale; private final Database database; private final WebServer webServer; - private UUIDUtility uuidUtility; + private final ConnectionSystem connectionSystem; + private final UUIDUtility uuidUtility; private final ErrorHandler errorHandler; @Inject @@ -41,10 +43,12 @@ public class InspectCommand extends CommandNode { Locale locale, Database database, WebServer webServer, + ConnectionSystem connectionSystem, UUIDUtility uuidUtility, ErrorHandler errorHandler ) { super("inspect", Permissions.INSPECT.getPermission(), CommandType.PLAYER_OR_ARGS); + this.connectionSystem = connectionSystem; setArguments(""); this.locale = locale; @@ -83,7 +87,7 @@ public class InspectCommand extends CommandNode { } checkWebUserAndNotify(sender); - Processing.submit(new InspectCacheRequestProcessor(uuid, sender, playerName, locale)); + Processing.submit(new InspectCacheRequestProcessor(uuid, sender, playerName, this::sendInspectMsg)); } catch (DBOpException e) { sender.sendMessage("§eDatabase exception occurred: " + e.getMessage()); errorHandler.log(L.ERROR, this.getClass(), e); @@ -100,4 +104,21 @@ public class InspectCommand extends CommandNode { } } } + + private void sendInspectMsg(ISender sender, String playerName) { + sender.sendMessage(locale.getString(CommandLang.HEADER_INSPECT, playerName)); + + String url = connectionSystem.getMainAddress() + "/player/" + playerName; + String linkPrefix = locale.getString(CommandLang.LINK_PREFIX); + + boolean console = !CommandUtils.isPlayer(sender); + if (console) { + sender.sendMessage(linkPrefix + url); + } else { + sender.sendMessage(linkPrefix); + sender.sendLink(" ", locale.getString(CommandLang.LINK_CLICK_ME), url); + } + + sender.sendMessage(">"); + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/DebugChannels.java b/Plan/src/main/java/com/djrapitops/plan/system/DebugChannels.java new file mode 100644 index 000000000..7b060d3cf --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/system/DebugChannels.java @@ -0,0 +1,11 @@ +package com.djrapitops.plan.system; + +/** + * Identifiers for different Debug channels. + * + * @author Rsl1122 + */ +public class DebugChannels { + public static final String INFO_REQUESTS = "InfoRequests"; + public static final String CONNECTIONS = "Connections"; +} diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/BungeeInfoSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/info/BungeeInfoSystem.java index bb5bf1a40..38cb7a4c1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/BungeeInfoSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/BungeeInfoSystem.java @@ -13,10 +13,12 @@ import com.djrapitops.plan.system.info.request.GenerateInspectPageRequest; import com.djrapitops.plan.system.info.request.InfoRequest; import com.djrapitops.plan.system.info.request.InfoRequestFactory; import com.djrapitops.plan.system.info.server.ServerInfo; +import com.djrapitops.plan.system.webserver.WebServer; import com.djrapitops.plan.system.webserver.cache.PageId; import com.djrapitops.plan.system.webserver.cache.ResponseCache; import com.djrapitops.plan.system.webserver.response.errors.InternalErrorResponse; import com.djrapitops.plan.system.webserver.response.pages.NetworkPageResponse; +import com.djrapitops.plugin.logging.console.PluginLogger; import javax.inject.Inject; import javax.inject.Singleton; @@ -32,16 +34,22 @@ public class BungeeInfoSystem extends InfoSystem { private final ServerInfo serverInfo; @Inject - public BungeeInfoSystem(InfoRequestFactory infoRequestFactory, ConnectionSystem connectionSystem, ServerInfo serverInfo) { - super(infoRequestFactory, connectionSystem); + public BungeeInfoSystem( + InfoRequestFactory infoRequestFactory, + ConnectionSystem connectionSystem, + ServerInfo serverInfo, + WebServer webServer, + PluginLogger logger + ) { + super(infoRequestFactory, connectionSystem, serverInfo, webServer, logger); this.serverInfo = serverInfo; } @Override public void runLocally(InfoRequest infoRequest) throws WebException { - if (infoRequest instanceof CacheRequest || - infoRequest instanceof GenerateInspectPageRequest) { + if (infoRequest instanceof CacheRequest + || infoRequest instanceof GenerateInspectPageRequest) { infoRequest.runLocally(); } else { // runLocally is called when ConnectionSystem has no servers. diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/InfoSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/info/InfoSystem.java index 65bc492be..c79aa392b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/InfoSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/InfoSystem.java @@ -9,7 +9,7 @@ import com.djrapitops.plan.api.exceptions.connection.BadRequestException; import com.djrapitops.plan.api.exceptions.connection.ConnectionFailException; import com.djrapitops.plan.api.exceptions.connection.NoServersException; import com.djrapitops.plan.api.exceptions.connection.WebException; -import com.djrapitops.plan.system.PlanSystem; +import com.djrapitops.plan.system.DebugChannels; import com.djrapitops.plan.system.SubSystem; import com.djrapitops.plan.system.info.connection.ConnectionSystem; import com.djrapitops.plan.system.info.request.GenerateRequest; @@ -17,10 +17,9 @@ import com.djrapitops.plan.system.info.request.InfoRequest; import com.djrapitops.plan.system.info.request.InfoRequestFactory; import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.ServerInfo; -import com.djrapitops.plan.system.webserver.WebServerSystem; +import com.djrapitops.plan.system.webserver.WebServer; import com.djrapitops.plugin.api.Check; -import com.djrapitops.plugin.api.utility.log.Log; -import com.djrapitops.plugin.utilities.Verify; +import com.djrapitops.plugin.logging.console.PluginLogger; import java.util.UUID; @@ -37,17 +36,22 @@ public abstract class InfoSystem implements SubSystem { protected final InfoRequestFactory infoRequestFactory; protected final ConnectionSystem connectionSystem; + protected final ServerInfo serverInfo; + protected final WebServer webServer; + protected final PluginLogger logger; - protected InfoSystem(InfoRequestFactory infoRequestFactory, ConnectionSystem connectionSystem) { + protected InfoSystem( + InfoRequestFactory infoRequestFactory, + ConnectionSystem connectionSystem, + ServerInfo serverInfo, + WebServer webServer, + PluginLogger logger + ) { this.infoRequestFactory = infoRequestFactory; this.connectionSystem = connectionSystem; - } - - @Deprecated - public static InfoSystem getInstance() { - InfoSystem infoSystem = PlanSystem.getInstance().getInfoSystem(); - Verify.nullCheck(infoSystem, () -> new IllegalStateException("Info System was not initialized.")); - return infoSystem; + this.serverInfo = serverInfo; + this.webServer = webServer; + this.logger = logger; } /** @@ -77,7 +81,7 @@ public abstract class InfoSystem implements SubSystem { */ public void generateAnalysisPage(UUID serverUUID) throws WebException { GenerateRequest request = infoRequestFactory.generateAnalysisPageRequest(serverUUID); - if (ServerInfo.getServerUUID_Old().equals(serverUUID)) { + if (serverInfo.getServerUUID().equals(serverUUID)) { runLocally(request); } else { sendRequest(request); @@ -95,14 +99,14 @@ public abstract class InfoSystem implements SubSystem { public void sendRequest(InfoRequest infoRequest) throws WebException { try { if (!connectionSystem.isServerAvailable()) { - Log.debug("Main server unavailable, running locally."); + logger.debug(DebugChannels.INFO_REQUESTS, "Main server unavailable, running locally."); runLocally(infoRequest); return; } connectionSystem.sendInfoRequest(infoRequest); } catch (WebException original) { try { - Log.debug("Exception during request: " + original.toString() + ", running locally."); + logger.debug(DebugChannels.INFO_REQUESTS, "Exception during request: " + original.toString() + ", running locally."); runLocally(infoRequest); } catch (NoServersException e2) { throw original; @@ -156,7 +160,7 @@ public abstract class InfoSystem implements SubSystem { throw new BadRequestException("Method not available on Bungee."); } Server bungee = new Server(-1, null, "Bungee", addressToRequestServer, -1); - String addressOfThisServer = WebServerSystem.getInstance().getWebServer().getAccessAddress(); + String addressOfThisServer = webServer.getAccessAddress(); connectionSystem.setSetupAllowed(true); connectionSystem.sendInfoRequest(infoRequestFactory.sendDBSettingsRequest(addressOfThisServer), bungee); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/ServerInfoSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/info/ServerInfoSystem.java index 0d6974b42..802c35757 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/ServerInfoSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/ServerInfoSystem.java @@ -11,6 +11,7 @@ import com.djrapitops.plan.system.info.request.InfoRequest; import com.djrapitops.plan.system.info.request.InfoRequestFactory; import com.djrapitops.plan.system.info.request.SetupRequest; import com.djrapitops.plan.system.info.server.ServerInfo; +import com.djrapitops.plan.system.webserver.WebServer; import com.djrapitops.plan.utilities.html.HtmlStructure; import com.djrapitops.plugin.logging.console.PluginLogger; @@ -33,9 +34,10 @@ public class ServerInfoSystem extends InfoSystem { ConnectionSystem connectionSystem, ServerInfo serverInfo, InfoRequestFactory infoRequestFactory, + WebServer webServer, PluginLogger logger ) { - super(infoRequestFactory, connectionSystem); + super(infoRequestFactory, connectionSystem, serverInfo, webServer, logger); this.serverInfo = serverInfo; this.logger = logger; } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/BungeeConnectionSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/BungeeConnectionSystem.java index 8283898b5..c2e07c41c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/BungeeConnectionSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/BungeeConnectionSystem.java @@ -31,7 +31,8 @@ public class BungeeConnectionSystem extends ConnectionSystem { private final Database database; private final WebServer webServer; - private ErrorHandler errorHandler; + private final ErrorHandler errorHandler; + private final WebExceptionLogger webExceptionLogger; private long latestServerMapRefresh; @@ -43,12 +44,14 @@ public class BungeeConnectionSystem extends ConnectionSystem { InfoRequests infoRequests, Lazy infoSystem, ServerInfo serverInfo, - ErrorHandler errorHandler + ErrorHandler errorHandler, + WebExceptionLogger webExceptionLogger ) { super(connectionLog, infoRequests, infoSystem, serverInfo); this.database = database; this.webServer = webServer; this.errorHandler = errorHandler; + this.webExceptionLogger = webExceptionLogger; latestServerMapRefresh = 0; } @@ -88,7 +91,7 @@ public class BungeeConnectionSystem extends ConnectionSystem { throw new NoServersException("No Servers available to make wide-request: " + infoRequest.getClass().getSimpleName()); } for (Server server : bukkitServers.values()) { - WebExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server)); + webExceptionLogger.logIfOccurs(this.getClass(), () -> sendInfoRequest(infoRequest, server)); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java index 7a789f236..a7007cf2d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionLog.java @@ -5,11 +5,13 @@ package com.djrapitops.plan.system.info.connection; import com.djrapitops.plan.data.store.objects.DateHolder; +import com.djrapitops.plan.system.DebugChannels; import com.djrapitops.plan.system.info.request.InfoRequest; import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plugin.logging.debug.DebugLogger; import javax.inject.Inject; +import javax.inject.Singleton; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -19,6 +21,7 @@ import java.util.Objects; * * @author Rsl1122 */ +@Singleton public class ConnectionLog { private final DebugLogger debugLogger; @@ -31,41 +34,16 @@ public class ConnectionLog { log = new HashMap<>(); } - /** - * Get a map sorted by Addresses, then Requests and then Log entries. - * - * @return {@code Map<"In: "/"Out: "+Address, Map>} - */ - @Deprecated - public static Map> getLogEntries_Old() { - return getInstance().getLogEntries(); - } - - @Deprecated - public static void logConnectionTo_Old(Server server, InfoRequest request, int responseCode) { - getInstance().logConnectionTo(server, request, responseCode); - } - - @Deprecated - public static void logConnectionFrom_Old(String server, String requestTarget, int responseCode) { - getInstance().logConnectionFrom(server, requestTarget, responseCode); - } - - @Deprecated - private static ConnectionLog getInstance() { - return ConnectionSystem.getInstance().getConnectionLog(); - } - public void logConnectionTo(Server server, InfoRequest request, int responseCode) { String requestName = request.getClass().getSimpleName(); String address = server.getWebAddress(); logConnection(address, "Out: " + requestName, responseCode); - debugLogger.logOn("Connections", "ConnectionOut: " + requestName + " to " + address); + debugLogger.logOn(DebugChannels.CONNECTIONS, "ConnectionOut: " + requestName + " to " + address); } public void logConnectionFrom(String server, String requestTarget, int responseCode) { logConnection(server, "In: " + requestTarget, responseCode); - debugLogger.logOn("Connections", "ConnectionIn: " + requestTarget + " from " + server); + debugLogger.logOn(DebugChannels.CONNECTIONS, "ConnectionIn: " + requestTarget + " from " + server); } private void logConnection(String address, String infoRequestName, int responseCode) { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionSystem.java index f7ae7f7c2..3bc3ab9b5 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ConnectionSystem.java @@ -12,7 +12,6 @@ import com.djrapitops.plan.system.info.request.InfoRequest; import com.djrapitops.plan.system.info.request.WideRequest; import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.ServerInfo; -import com.djrapitops.plugin.utilities.Verify; import dagger.Lazy; import java.util.*; @@ -48,13 +47,6 @@ public abstract class ConnectionSystem implements SubSystem { this.dataRequests = dataRequests; } - @Deprecated - public static ConnectionSystem getInstance() { - ConnectionSystem connectionSystem = InfoSystem.getInstance().getConnectionSystem(); - Verify.nullCheck(connectionSystem, () -> new IllegalStateException("Connection System was not initialized")); - return connectionSystem; - } - public InfoRequest getInfoRequest(String name) { return dataRequests.get(name.toLowerCase()); } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/InfoRequestPageHandler.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/InfoRequestPageHandler.java index 8e3cec049..45bde5dd8 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/InfoRequestPageHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/InfoRequestPageHandler.java @@ -72,7 +72,7 @@ public class InfoRequestPageHandler implements PageHandler { responseCode = getResponseCodeFor(e); throw e; } finally { - ConnectionLog.logConnectionFrom_Old(request.getRemoteAddress(), request.getTarget(), responseCode); + connectionSystem.getConnectionLog().logConnectionFrom(request.getRemoteAddress(), request.getTarget(), responseCode); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ServerConnectionSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ServerConnectionSystem.java index b487d7b99..a3b3b511b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ServerConnectionSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/ServerConnectionSystem.java @@ -40,6 +40,7 @@ public class ServerConnectionSystem extends ConnectionSystem { private final Database database; private final WebServer webServer; private final PluginLogger pluginLogger; + private final WebExceptionLogger webExceptionLogger; private long latestServerMapRefresh; @@ -55,7 +56,8 @@ public class ServerConnectionSystem extends ConnectionSystem { InfoRequests infoRequests, Lazy infoSystem, ServerInfo serverInfo, - PluginLogger pluginLogger + PluginLogger pluginLogger, + WebExceptionLogger webExceptionLogger ) { super(connectionLog, infoRequests, infoSystem, serverInfo); this.locale = locale; @@ -63,6 +65,7 @@ public class ServerConnectionSystem extends ConnectionSystem { this.database = database; this.webServer = webServer; this.pluginLogger = pluginLogger; + this.webExceptionLogger = webExceptionLogger; latestServerMapRefresh = 0; } @@ -105,7 +108,7 @@ public class ServerConnectionSystem extends ConnectionSystem { throw new NoServersException("No Servers available to make wide-request: " + infoRequest.getClass().getSimpleName()); } for (Server server : bukkitServers.values()) { - WebExceptionLogger.logIfOccurs(this.getClass(), () -> { + webExceptionLogger.logIfOccurs(this.getClass(), () -> { try { sendInfoRequest(infoRequest, server); } catch (ConnectionFailException ignored) { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java index f595e50f5..731f8c6be 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/info/connection/WebExceptionLogger.java @@ -6,8 +6,12 @@ package com.djrapitops.plan.system.info.connection; import com.djrapitops.plan.api.exceptions.connection.*; import com.djrapitops.plan.utilities.java.ThrowingVoidFunction; -import com.djrapitops.plugin.api.utility.log.Log; +import com.djrapitops.plugin.logging.L; +import com.djrapitops.plugin.logging.console.PluginLogger; +import com.djrapitops.plugin.logging.error.ErrorHandler; +import javax.inject.Inject; +import javax.inject.Singleton; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -18,31 +22,45 @@ import java.util.Map; * * @author Rsl1122 */ +@Singleton public class WebExceptionLogger { - @Deprecated - public static void logIfOccurs(Class c, ThrowingVoidFunction function) { + private final ConnectionLog connectionLog; + private final PluginLogger logger; + private final ErrorHandler errorHandler; + + @Inject + public WebExceptionLogger( + ConnectionLog connectionLog, + PluginLogger logger, + ErrorHandler errorHandler + ) { + this.connectionLog = connectionLog; + this.logger = logger; + this.errorHandler = errorHandler; + } + + public void logIfOccurs(Class definingClass, ThrowingVoidFunction function) { try { function.apply(); } catch (ConnectionFailException e) { if (shouldLog(e)) { - Log.debug(e.getMessage()); + logger.debug(e.getMessage()); } } catch (UnsupportedTransferDatabaseException | UnauthorizedServerException | NotFoundException | NoServersException e) { - Log.debug(e.getMessage()); + logger.debug(e.getMessage()); } catch (WebException e) { - Log.toLog(c, e); + errorHandler.log(L.WARN, definingClass, e); } } - @Deprecated - private static boolean shouldLog(ConnectionFailException e) { + private boolean shouldLog(ConnectionFailException e) { String address = getAddress(e); if (address == null) { return true; } - Map> logEntries = ConnectionLog.getLogEntries_Old(); + Map> logEntries = connectionLog.getLogEntries(); Map entries = logEntries.get("Out: " + address); if (entries != null) { List connections = new ArrayList<>(entries.values()); @@ -64,8 +82,4 @@ public class WebExceptionLogger { } return null; } - - private WebExceptionLogger() { - // Static method class. - } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/InspectCacheRequestProcessor.java b/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/InspectCacheRequestProcessor.java index 5e593a688..0791d7236 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/InspectCacheRequestProcessor.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/InspectCacheRequestProcessor.java @@ -7,14 +7,11 @@ package com.djrapitops.plan.system.processing.processors.info; import com.djrapitops.plan.api.exceptions.connection.*; import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.info.InfoSystem; -import com.djrapitops.plan.system.info.connection.ConnectionSystem; -import com.djrapitops.plan.system.locale.Locale; -import com.djrapitops.plan.system.locale.lang.CommandLang; import com.djrapitops.plugin.api.utility.log.Log; -import com.djrapitops.plugin.command.CommandUtils; import com.djrapitops.plugin.command.ISender; import java.util.UUID; +import java.util.function.BiConsumer; /** * Sends a request to cache players inspect page to the ResponseCache on the appropriate WebServer. @@ -26,21 +23,28 @@ public class InspectCacheRequestProcessor implements Runnable { private final UUID uuid; private final ISender sender; private final String playerName; - private final Locale locale; + private final BiConsumer msgSender; - public InspectCacheRequestProcessor(UUID uuid, ISender sender, String playerName, Locale locale) { + private InfoSystem infoSystem; + + public InspectCacheRequestProcessor( + UUID uuid, + ISender sender, + String playerName, + BiConsumer msgSender + ) { this.uuid = uuid; this.sender = sender; this.playerName = playerName; - this.locale = locale; + this.msgSender = msgSender; } @Override public void run() { SessionCache.refreshActiveSessionsState(); try { - InfoSystem.getInstance().generateAndCachePlayerPage(uuid); - sendInspectMsg(sender, playerName); + infoSystem.generateAndCachePlayerPage(uuid); + msgSender.accept(sender, playerName); } catch (ConnectionFailException | UnsupportedTransferDatabaseException | UnauthorizedServerException | NotFoundException | NoServersException e) { sender.sendMessage("§c" + e.getMessage()); @@ -48,21 +52,4 @@ public class InspectCacheRequestProcessor implements Runnable { Log.toLog(this.getClass(), e); } } - - // TODO Move to InspectCommand somehow. - private void sendInspectMsg(ISender sender, String playerName) { - sender.sendMessage(locale.getString(CommandLang.HEADER_INSPECT, playerName)); - // Link - String url = ConnectionSystem.getInstance().getMainAddress() + "/player/" + playerName; - String linkPrefix = locale.getString(CommandLang.LINK_PREFIX); - boolean console = !CommandUtils.isPlayer(sender); - if (console) { - sender.sendMessage(linkPrefix + url); - } else { - sender.sendMessage(linkPrefix); - sender.sendLink(" ", locale.getString(CommandLang.LINK_CLICK_ME), url); - } - - sender.sendMessage(">"); - } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/NetworkPageUpdateProcessor.java b/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/NetworkPageUpdateProcessor.java index 244f70f3d..c3b83e064 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/NetworkPageUpdateProcessor.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/NetworkPageUpdateProcessor.java @@ -15,10 +15,12 @@ import com.djrapitops.plugin.api.utility.log.Log; */ public class NetworkPageUpdateProcessor implements Runnable { + private InfoSystem infoSystem; + @Override public void run() { try { - InfoSystem.getInstance().updateNetworkPage(); + infoSystem.updateNetworkPage(); } catch (WebException e) { Log.toLog(this.getClass(), e); } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/PlayerPageUpdateProcessor.java b/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/PlayerPageUpdateProcessor.java index 98d0cfaa7..1ec9d78a2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/PlayerPageUpdateProcessor.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/processing/processors/info/PlayerPageUpdateProcessor.java @@ -13,20 +13,23 @@ public class PlayerPageUpdateProcessor implements Runnable { private final UUID uuid; + private InfoSystem infoSystem; + private WebExceptionLogger webExceptionLogger; + public PlayerPageUpdateProcessor(UUID uuid) { this.uuid = uuid; } @Override public void run() { - if (!InfoSystem.getInstance().getConnectionSystem().isServerAvailable() || Check.isBungeeAvailable()) { + if (!infoSystem.getConnectionSystem().isServerAvailable() || Check.isBungeeAvailable()) { RunnableFactory.createNew("Generate Inspect page: " + uuid, new AbsRunnable() { @Override public void run() { try { - WebExceptionLogger.logIfOccurs(PlayerPageUpdateProcessor.class, - () -> InfoSystem.getInstance().generateAndCachePlayerPage(uuid) + webExceptionLogger.logIfOccurs(PlayerPageUpdateProcessor.class, + () -> infoSystem.generateAndCachePlayerPage(uuid) ); } finally { cancel(); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java b/Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java deleted file mode 100644 index aa7d14141..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/system/settings/PlanErrorManager.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * License is provided in the jar as LICENSE also here: - * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/LICENSE - */ -package com.djrapitops.plan.system.settings; - -import com.djrapitops.plan.system.webserver.WebServer; -import com.djrapitops.plan.system.webserver.WebServerSystem; -import com.djrapitops.plugin.api.Check; -import com.djrapitops.plugin.api.utility.log.ErrorLogger; -import com.djrapitops.plugin.api.utility.log.Log; -import com.djrapitops.plugin.api.utility.log.errormanager.ErrorManager; - -import java.io.File; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * Abstract Plugin Framework ErrorManager for logging Errors properly instead of a vague message. - * - * @author Rsl1122 - */ -public class PlanErrorManager implements ErrorManager { - - @Override - public void toLog(String source, Throwable e, Class callingPlugin) { - try { - File logsFolder = Log.getLogsFolder(callingPlugin); - Log.warn(source + " Caught: " + e, callingPlugin); - if (WebServerSystem.isWebServerEnabled()) { - Log.warn("Exception can be viewed at " + WebServer.getInstance().getAccessAddress() + "/debug"); - } else { - Log.warn("It has been logged to ErrorLog.txt"); - } - logGlobally(source, e); - ErrorLogger.logThrowable(e, logsFolder); - } catch (Exception exception) { - System.out.println("Failed to log error to file because of " + exception); - System.out.println("Error:"); - // Fallback - System.out.println("Fail Reason:"); - Logger.getGlobal().log(Level.WARNING, source, e); - } - } - - private void logGlobally(String source, Throwable e) { - try { - if ((Check.isBukkitAvailable() && Check.isBungeeAvailable()) || Settings.DEV_MODE.isTrue()) { - Logger.getGlobal().log(Level.WARNING, source, e); - } - } catch (IllegalStateException ignored) { - /* Config system not initialized */ - } - } -} diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/EnableConnectionTask.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/EnableConnectionTask.java index 39476c5b2..3b93d1efb 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/EnableConnectionTask.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/bungee/EnableConnectionTask.java @@ -6,24 +6,29 @@ import com.djrapitops.plan.system.info.request.InfoRequestFactory; import com.djrapitops.plugin.task.AbsRunnable; import javax.inject.Inject; +import javax.inject.Singleton; +@Singleton public class EnableConnectionTask extends AbsRunnable { private ConnectionSystem connectionSystem; private final InfoRequestFactory infoRequestFactory; + private final WebExceptionLogger webExceptionLogger; @Inject public EnableConnectionTask( ConnectionSystem connectionSystem, - InfoRequestFactory infoRequestFactory + InfoRequestFactory infoRequestFactory, + WebExceptionLogger webExceptionLogger ) { this.connectionSystem = connectionSystem; this.infoRequestFactory = infoRequestFactory; + this.webExceptionLogger = webExceptionLogger; } @Override public void run() { - WebExceptionLogger.logIfOccurs(this.getClass(), + webExceptionLogger.logIfOccurs(this.getClass(), () -> connectionSystem.sendWideInfoRequest(infoRequestFactory.generateNetworkPageContentRequest()) ); cancel(); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BootAnalysisTask.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BootAnalysisTask.java index 439f6a8e4..9b67bf264 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BootAnalysisTask.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/BootAnalysisTask.java @@ -7,28 +7,33 @@ import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plugin.task.AbsRunnable; import javax.inject.Inject; +import javax.inject.Singleton; +@Singleton public class BootAnalysisTask extends AbsRunnable { private final InfoSystem infoSystem; private final InfoRequestFactory infoRequestFactory; private final ServerInfo serverInfo; + private final WebExceptionLogger webExceptionLogger; @Inject public BootAnalysisTask( InfoSystem infoSystem, InfoRequestFactory infoRequestFactory, - ServerInfo serverInfo + ServerInfo serverInfo, + WebExceptionLogger webExceptionLogger ) { this.infoSystem = infoSystem; this.infoRequestFactory = infoRequestFactory; this.serverInfo = serverInfo; + this.webExceptionLogger = webExceptionLogger; } @Override public void run() { try { - WebExceptionLogger.logIfOccurs(this.getClass(), () -> + webExceptionLogger.logIfOccurs(this.getClass(), () -> infoSystem.sendRequest(infoRequestFactory.generateAnalysisPageRequest(serverInfo.getServerUUID())) ); } catch (IllegalStateException ignore) { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/NetworkPageRefreshTask.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/NetworkPageRefreshTask.java index b5643cc30..6c03b31cd 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/NetworkPageRefreshTask.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/NetworkPageRefreshTask.java @@ -5,18 +5,25 @@ import com.djrapitops.plan.system.info.connection.WebExceptionLogger; import com.djrapitops.plugin.task.AbsRunnable; import javax.inject.Inject; +import javax.inject.Singleton; +@Singleton public class NetworkPageRefreshTask extends AbsRunnable { private InfoSystem infoSystem; + private final WebExceptionLogger webExceptionLogger; @Inject - public NetworkPageRefreshTask(InfoSystem infoSystem) { + public NetworkPageRefreshTask( + InfoSystem infoSystem, + WebExceptionLogger webExceptionLogger + ) { this.infoSystem = infoSystem; + this.webExceptionLogger = webExceptionLogger; } @Override public void run() { - WebExceptionLogger.logIfOccurs(this.getClass(), () -> infoSystem.updateNetworkPage()); + webExceptionLogger.logIfOccurs(this.getClass(), () -> infoSystem.updateNetworkPage()); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/PeriodicAnalysisTask.java b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/PeriodicAnalysisTask.java index 24e55729a..00248ce22 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/PeriodicAnalysisTask.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/tasks/server/PeriodicAnalysisTask.java @@ -10,7 +10,9 @@ import com.djrapitops.plugin.logging.error.ErrorHandler; import com.djrapitops.plugin.task.AbsRunnable; import javax.inject.Inject; +import javax.inject.Singleton; +@Singleton public class PeriodicAnalysisTask extends AbsRunnable { private final InfoSystem infoSystem; @@ -18,25 +20,28 @@ public class PeriodicAnalysisTask extends AbsRunnable { private final ServerInfo serverInfo; private final PluginLogger logger; private final ErrorHandler errorHandler; + private final WebExceptionLogger webExceptionLogger; @Inject public PeriodicAnalysisTask( InfoSystem infoSystem, InfoRequestFactory infoRequestFactory, ServerInfo serverInfo, PluginLogger logger, - ErrorHandler errorHandler + ErrorHandler errorHandler, + WebExceptionLogger webExceptionLogger ) { this.infoSystem = infoSystem; this.infoRequestFactory = infoRequestFactory; this.serverInfo = serverInfo; this.logger = logger; this.errorHandler = errorHandler; + this.webExceptionLogger = webExceptionLogger; } @Override public void run() { try { - WebExceptionLogger.logIfOccurs(this.getClass(), () -> + webExceptionLogger.logIfOccurs(this.getClass(), () -> infoSystem.sendRequest(infoRequestFactory.generateAnalysisPageRequest(serverInfo.getServerUUID())) ); } catch (IllegalStateException ignore) { diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java index 7e451f089..d192d807c 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServer.java @@ -12,7 +12,6 @@ import com.djrapitops.plugin.api.Check; import com.djrapitops.plugin.logging.L; import com.djrapitops.plugin.logging.console.PluginLogger; import com.djrapitops.plugin.logging.error.ErrorHandler; -import com.djrapitops.plugin.utilities.Verify; import com.sun.net.httpserver.HttpServer; import com.sun.net.httpserver.HttpsConfigurator; import com.sun.net.httpserver.HttpsParameters; @@ -75,13 +74,6 @@ public class WebServer implements SubSystem { this.errorHandler = errorHandler; } - @Deprecated - public static WebServer getInstance() { - WebServer webServer = WebServerSystem.getInstance().getWebServer(); - Verify.nullCheck(webServer, () -> new IllegalStateException("WebServer was not initialized.")); - return webServer; - } - @Override public void enable() throws EnableException { this.port = config.getNumber(Settings.WEBSERVER_PORT); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServerSystem.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServerSystem.java index 13235dee5..e1679dfa8 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServerSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/WebServerSystem.java @@ -5,18 +5,19 @@ package com.djrapitops.plan.system.webserver; import com.djrapitops.plan.api.exceptions.EnableException; -import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.SubSystem; import com.djrapitops.plan.system.webserver.cache.ResponseCache; import com.djrapitops.plugin.benchmarking.Timings; import javax.inject.Inject; +import javax.inject.Singleton; /** * WebServer subsystem for managing WebServer initialization. * * @author Rsl1122 */ +@Singleton public class WebServerSystem implements SubSystem { private final WebServer webServer; @@ -28,17 +29,6 @@ public class WebServerSystem implements SubSystem { this.timings = timings; } - @Deprecated - public static WebServerSystem getInstance() { - return PlanSystem.getInstance().getWebServerSystem(); - } - - @Deprecated - public static boolean isWebServerEnabled() { - WebServer webServer = getInstance().webServer; - return webServer != null && webServer.isEnabled(); - } - @Override public void enable() throws EnableException { timings.start("WebServer Initialization"); diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java index 2f897a06a..481541482 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/pages/ServerPageHandler.java @@ -72,7 +72,7 @@ public class ServerPageHandler implements PageHandler { } return ResponseCache.loadResponse(PageId.SERVER.of(serverUUID)); } - return AnalysisPageResponse.refreshNow(serverUUID); + return AnalysisPageResponse.refreshNow(serverUUID, infoSystem); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/AnalysisPageResponse.java b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/AnalysisPageResponse.java index 5a927f0ae..aeb93102b 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/AnalysisPageResponse.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/webserver/response/pages/AnalysisPageResponse.java @@ -21,10 +21,10 @@ import java.util.UUID; public class AnalysisPageResponse extends Response { @Deprecated - public static AnalysisPageResponse refreshNow(UUID serverUUID) { + public static AnalysisPageResponse refreshNow(UUID serverUUID, InfoSystem infoSystem) { Processing.submitNonCritical(() -> { try { - InfoSystem.getInstance().generateAnalysisPage(serverUUID); + infoSystem.generateAnalysisPage(serverUUID); } catch (NoServersException | ConnectionFailException e) { ResponseCache.cacheResponse(PageId.SERVER.of(serverUUID), () -> new NotFoundResponse(e.getMessage())); } catch (WebException e) { diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsBungee.java b/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsBungee.java index 42a4af38b..21c7fbdb9 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsBungee.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsBungee.java @@ -3,7 +3,6 @@ package com.djrapitops.plan.utilities.metrics; import com.djrapitops.plan.PlanBungee; import com.djrapitops.plan.system.database.databases.Database; import com.djrapitops.plan.system.info.connection.ConnectionSystem; -import com.djrapitops.plugin.api.utility.log.Log; import org.bstats.bungeecord.Metrics; import java.io.Serializable; @@ -11,14 +10,18 @@ import java.io.Serializable; public class BStatsBungee { private final PlanBungee plugin; + private final Database database; + private final ConnectionSystem connectionSystem; + private Metrics metrics; - public BStatsBungee(PlanBungee plugin) { + public BStatsBungee(PlanBungee plugin, Database database, ConnectionSystem connectionSystem) { this.plugin = plugin; + this.database = database; + this.connectionSystem = connectionSystem; } public void registerMetrics() { - Log.logDebug("Enable", "Enabling bStats Metrics."); if (metrics == null) { metrics = new Metrics(plugin); } @@ -27,11 +30,11 @@ public class BStatsBungee { private void registerConfigSettingGraphs() { String serverType = plugin.getProxy().getName(); - String databaseType = Database.getActive().getName(); + String databaseType = database.getName(); addStringSettingPie("server_type", serverType); addStringSettingPie("database_type", databaseType); - addStringSettingPie("network_servers", ConnectionSystem.getInstance().getBukkitServers().size()); + addStringSettingPie("network_servers", connectionSystem.getBukkitServers().size()); } protected void addStringSettingPie(String id, Serializable setting) { diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsSponge.java b/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsSponge.java index 15c2cd6c3..bae02213d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsSponge.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/metrics/BStatsSponge.java @@ -1,8 +1,6 @@ package com.djrapitops.plan.utilities.metrics; import com.djrapitops.plan.system.database.databases.Database; -import com.djrapitops.plan.system.info.connection.ConnectionSystem; -import com.djrapitops.plugin.api.utility.log.Log; import org.bstats.sponge.Metrics; import java.io.Serializable; @@ -10,27 +8,25 @@ import java.io.Serializable; public class BStatsSponge { private final Metrics metrics; + private final Database database; - public BStatsSponge(Metrics metrics) { + public BStatsSponge(Metrics metrics, Database database) { this.metrics = metrics; + this.database = database; } public void registerMetrics() { - Log.logDebug("Enable", "Enabling bStats Metrics."); if (metrics != null) { registerConfigSettingGraphs(); - } else { - Log.debug("Metrics not injected properly."); } } private void registerConfigSettingGraphs() { String serverType = "Sponge"; - String databaseType = Database.getActive().getName(); + String databaseType = database.getName(); addStringSettingPie("server_type", serverType); addStringSettingPie("database_type", databaseType); - addStringSettingPie("network_servers", ConnectionSystem.getInstance().getBukkitServers().size()); } protected void addStringSettingPie(String id, Serializable setting) {