From 42464d503e34504cc0c6900b05f60fded11d8c49 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sun, 24 Feb 2019 12:41:07 +0200 Subject: [PATCH] Moved shutdown session save message to ServerShutdownSave --- .../djrapitops/plan/BukkitServerShutdownSave.java | 7 +++---- .../src/main/java/com/djrapitops/plan/Plan.java | 1 - .../com/djrapitops/plan/ServerShutdownSave.java | 14 ++++++++++++++ .../java/com/djrapitops/plan/ShutdownSaveTest.java | 4 +++- .../djrapitops/plan/SpongeServerShutdownSave.java | 11 +++++++++-- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java index cfad8abe7..c710c349d 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/BukkitServerShutdownSave.java @@ -17,6 +17,7 @@ package com.djrapitops.plan; import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plan.utilities.java.Reflection; import com.djrapitops.plugin.logging.console.PluginLogger; import com.djrapitops.plugin.logging.error.ErrorHandler; @@ -32,16 +33,14 @@ import javax.inject.Singleton; @Singleton public class BukkitServerShutdownSave extends ServerShutdownSave { - private final PluginLogger logger; - @Inject public BukkitServerShutdownSave( + Locale locale, DBSystem dbSystem, PluginLogger logger, ErrorHandler errorHandler ) { - super(dbSystem, errorHandler); - this.logger = logger; + super(locale, dbSystem, logger, errorHandler); } @Override diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java index db94f8b93..2ae0e2506 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/Plan.java @@ -91,7 +91,6 @@ public class Plan extends BukkitPlugin implements PlanPlugin { @Override public void onDisable() { if (serverShutdownSave != null) { - logger.info(locale != null ? locale.getString(PluginLang.DISABLED_UNSAVED_SESSIONS) : PluginLang.DISABLED_UNSAVED_SESSIONS.getDefault()); serverShutdownSave.performSave(); } if (system != null) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/ServerShutdownSave.java b/Plan/common/src/main/java/com/djrapitops/plan/ServerShutdownSave.java index 34cba3eda..847e54f4e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/ServerShutdownSave.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/ServerShutdownSave.java @@ -24,7 +24,10 @@ import com.djrapitops.plan.db.Database; import com.djrapitops.plan.db.access.transactions.events.ServerShutdownTransaction; import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.locale.Locale; +import com.djrapitops.plan.system.locale.lang.PluginLang; import com.djrapitops.plugin.logging.L; +import com.djrapitops.plugin.logging.console.PluginLogger; import com.djrapitops.plugin.logging.error.ErrorHandler; import java.util.Map; @@ -39,15 +42,21 @@ import java.util.concurrent.ExecutionException; */ public abstract class ServerShutdownSave { + protected final PluginLogger logger; private final DBSystem dbSystem; + private final Locale locale; private final ErrorHandler errorHandler; private boolean shuttingDown = false; public ServerShutdownSave( + Locale locale, DBSystem dbSystem, + PluginLogger logger, ErrorHandler errorHandler ) { + this.locale = locale; this.dbSystem = dbSystem; + this.logger = logger; this.errorHandler = errorHandler; } @@ -67,6 +76,11 @@ public abstract class ServerShutdownSave { return; } + // This check ensures that logging is not attempted on JVM shutdown. + // Underlying Logger might not be available leading to an exception. + if (!shuttingDown) { + logger.info(locale.getString(PluginLang.DISABLED_UNSAVED_SESSIONS)); + } attemptSave(activeSessions); SessionCache.clear(); diff --git a/Plan/common/src/test/java/com/djrapitops/plan/ShutdownSaveTest.java b/Plan/common/src/test/java/com/djrapitops/plan/ShutdownSaveTest.java index 5aba8a26f..b942f44a5 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/ShutdownSaveTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/ShutdownSaveTest.java @@ -27,6 +27,7 @@ import com.djrapitops.plan.db.access.transactions.events.WorldNameStoreTransacti import com.djrapitops.plan.system.cache.SessionCache; import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.info.server.Server; +import com.djrapitops.plan.system.locale.Locale; import com.djrapitops.plugin.logging.console.TestPluginLogger; import com.djrapitops.plugin.logging.error.ConsoleErrorLogger; import extension.PrintExtension; @@ -84,7 +85,8 @@ public class ShutdownSaveTest { DBSystem dbSystemMock = mock(DBSystem.class); when(dbSystemMock.getDatabase()).thenReturn(database); - underTest = new ServerShutdownSave(dbSystemMock, new ConsoleErrorLogger(new TestPluginLogger())) { + TestPluginLogger logger = new TestPluginLogger(); + underTest = new ServerShutdownSave(new Locale(), dbSystemMock, logger, new ConsoleErrorLogger(logger)) { @Override protected boolean checkServerShuttingDownStatus() { return shutdownStatus; diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/SpongeServerShutdownSave.java b/Plan/sponge/src/main/java/com/djrapitops/plan/SpongeServerShutdownSave.java index d74dafdb3..2260e341b 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/SpongeServerShutdownSave.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/SpongeServerShutdownSave.java @@ -17,6 +17,8 @@ package com.djrapitops.plan; import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.locale.Locale; +import com.djrapitops.plugin.logging.console.PluginLogger; import com.djrapitops.plugin.logging.error.ErrorHandler; import org.spongepowered.api.GameState; import org.spongepowered.api.event.Listener; @@ -37,8 +39,13 @@ public class SpongeServerShutdownSave extends ServerShutdownSave { private boolean shuttingDown = false; @Inject - public SpongeServerShutdownSave(DBSystem dbSystem, ErrorHandler errorHandler) { - super(dbSystem, errorHandler); + public SpongeServerShutdownSave( + Locale locale, + DBSystem dbSystem, + PluginLogger logger, + ErrorHandler errorHandler + ) { + super(locale, dbSystem, logger, errorHandler); } @Override