From bd8b7818dafe2801229b08d002ce84e90b21b0b8 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 10 Jun 2024 18:55:27 +0200 Subject: [PATCH] Add Engine#shutdown method and shutdown events --- .../staropensource/sosengine/base/Engine.java | 24 +++++++++++++ .../base/events/EngineShutdownEvent.java | 36 +++++++++++++++++++ .../internal/InternalEngineShutdownEvent.java | 36 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 base/src/main/java/de/staropensource/sosengine/base/events/EngineShutdownEvent.java create mode 100644 base/src/main/java/de/staropensource/sosengine/base/events/internal/InternalEngineShutdownEvent.java diff --git a/base/src/main/java/de/staropensource/sosengine/base/Engine.java b/base/src/main/java/de/staropensource/sosengine/base/Engine.java index d7ebf276..074c7b7a 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/Engine.java +++ b/base/src/main/java/de/staropensource/sosengine/base/Engine.java @@ -20,6 +20,8 @@ package de.staropensource.sosengine.base; import de.staropensource.sosengine.base.data.info.EngineInformation; +import de.staropensource.sosengine.base.events.EngineShutdownEvent; +import de.staropensource.sosengine.base.events.internal.InternalEngineShutdownEvent; import de.staropensource.sosengine.base.logging.CrashHandler; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.CodePart; @@ -27,6 +29,7 @@ import de.staropensource.sosengine.base.types.LogIssuer; import de.staropensource.sosengine.base.utility.*; import lombok.Getter; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Range; import java.util.LinkedHashMap; import java.util.Map; @@ -164,4 +167,25 @@ public final class Engine { CrashHandler.getCrashContent().put("Operating system", crashContentOS); CrashHandler.getCrashContent().put("Stacktrace", "%stacktrace%"); } + + /** + * Shuts the engine and JVM down. + * + * @param exitCode the code to exit with, from 0-255 + * @since 1-alpha0 + */ + public void shutdown(@Range(from = 0, to = 255) int exitCode) { + new EngineShutdownEvent().callEvent(); + new InternalEngineShutdownEvent().callEvent(); + Runtime.getRuntime().exit(exitCode); + } + + /** + * Shuts the engine and JVM down. + * + * @since 1-alpha0 + */ + public void shutdown() { + shutdown(0); + } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/events/EngineShutdownEvent.java b/base/src/main/java/de/staropensource/sosengine/base/events/EngineShutdownEvent.java new file mode 100644 index 00000000..c661cdaa --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/events/EngineShutdownEvent.java @@ -0,0 +1,36 @@ +/* + STAROPENSOURCE ENGINE SOURCE FILE + Copyright (c) 2024 The StarOpenSource Engine Contributors + Licensed under the GNU Affero General Public License v3 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.staropensource.sosengine.base.events; + +import de.staropensource.sosengine.base.classes.Event; +import de.staropensource.sosengine.base.classes.helpers.EventHelper; + +/** + * Called when the engine and JVM are about to shutdown. + * + * @since 1-alpha0 + */ +public class EngineShutdownEvent implements Event { + /** {@inheritDoc} */ + @Override + public void callEvent() { + EventHelper.invokeAnnotatedMethods(getClass()); + } +} diff --git a/base/src/main/java/de/staropensource/sosengine/base/events/internal/InternalEngineShutdownEvent.java b/base/src/main/java/de/staropensource/sosengine/base/events/internal/InternalEngineShutdownEvent.java new file mode 100644 index 00000000..199d217a --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/events/internal/InternalEngineShutdownEvent.java @@ -0,0 +1,36 @@ +/* + STAROPENSOURCE ENGINE SOURCE FILE + Copyright (c) 2024 The StarOpenSource Engine Contributors + Licensed under the GNU Affero General Public License v3 + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.staropensource.sosengine.base.events.internal; + +import de.staropensource.sosengine.base.classes.Event; +import de.staropensource.sosengine.base.classes.helpers.EventHelper; + +/** + * Called when the engine and JVM are about to shutdown, after {@link de.staropensource.sosengine.base.events.EngineShutdownEvent}. + * + * @since 1-alpha0 + */ +public class InternalEngineShutdownEvent implements Event { + /** {@inheritDoc} */ + @Override + public void callEvent() { + EventHelper.invokeAnnotatedMethods(getClass()); + } +}