From d06f8a3dc3e510acf6ed3d3b74347a5b42755d5d Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Wed, 21 Aug 2024 13:51:21 +0200 Subject: [PATCH] Move ShutdownHandler code into EngineInternals --- .../staropensource/sosengine/base/Engine.java | 4 +- .../sosengine/base/EngineInternals.java | 65 ++++++++++++++++--- .../base/types/InternalAccessArea.java | 58 +++++++++++++++++ 3 files changed, 116 insertions(+), 11 deletions(-) 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 2a269e0..ba2396f 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/Engine.java +++ b/base/src/main/java/de/staropensource/sosengine/base/Engine.java @@ -148,8 +148,8 @@ public final class Engine extends SubsystemClass { * @see ShutdownHandler * @since v1-alpha2 */ - @Getter - @Setter + @Getter(value = AccessLevel.MODULE) + @Setter(value = AccessLevel.MODULE) private @NotNull ShutdownHandler shutdownHandler = new Engine.JvmShutdownHandler(); /** diff --git a/base/src/main/java/de/staropensource/sosengine/base/EngineInternals.java b/base/src/main/java/de/staropensource/sosengine/base/EngineInternals.java index 552ae80..cbd807e 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/EngineInternals.java +++ b/base/src/main/java/de/staropensource/sosengine/base/EngineInternals.java @@ -19,6 +19,7 @@ package de.staropensource.sosengine.base; +import de.staropensource.sosengine.base.classes.ShutdownHandler; import de.staropensource.sosengine.base.exceptions.NoAccessException; import de.staropensource.sosengine.base.logging.LoggerInstance; import de.staropensource.sosengine.base.types.InternalAccessArea; @@ -26,6 +27,7 @@ import lombok.Getter; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -79,18 +81,36 @@ public final class EngineInternals { } /** - * Allows disabling internal engine access. + * Determines whether access to the specified area is allowed. * + * @param area internal access area to check + * @throws NoAccessException when restricted + * @since v1-alpha4 + */ + private void isRestricted(@NotNull InternalAccessArea area) throws NoAccessException { + if (restrictedAreas.contains(area)) + throw new NoAccessException("The internal access area " + area.name() + " has been restricted"); + } + + /** + * Restricts access to certain areas of this class. + * + * @param area area to restrict * @since v1-alpha4 */ public void restrictAccess(@NotNull InternalAccessArea area) { - if (area == InternalAccessArea.ALL) { - List<@NotNull InternalAccessArea> areas = new ArrayList<>(List.of(InternalAccessArea.values())); - areas.remove(InternalAccessArea.ALL); - restrictedAreas.addAll(areas); + switch (area) { + case ALL -> { + List<@NotNull InternalAccessArea> areas = new ArrayList<>(List.of(InternalAccessArea.values())); + areas.remove(InternalAccessArea.ALL); + areas.remove(InternalAccessArea.ALL_READ); + areas.remove(InternalAccessArea.ALL_WRITE); + restrictedAreas.addAll(areas); + } + case ALL_READ -> restrictedAreas.addAll(Arrays.stream(InternalAccessArea.valuesReadOnly()).toList()); + case ALL_WRITE -> restrictedAreas.addAll(Arrays.stream(InternalAccessArea.valuesWriteOnly()).toList()); + default -> restrictedAreas.add(area); } - - restrictedAreas.add(area); } /** @@ -104,8 +124,7 @@ public final class EngineInternals { * @since v1-alpha4 */ public void installSafetyShutdownHook(boolean status) throws NoAccessException { - if (restrictedAreas.contains(InternalAccessArea.SAFETY_SHUTDOWN_HOOK)) - throw new NoAccessException("The internal access area SAFETY_SHUTDOWN_HOOK has been restricted"); + isRestricted(InternalAccessArea.SAFETY_SHUTDOWN_HOOK); try { if (status) @@ -114,4 +133,32 @@ public final class EngineInternals { Runtime.getRuntime().removeShutdownHook(Engine.getInstance().getSafetyShutdownHook()); } catch (IllegalArgumentException | IllegalStateException ignored) {} } + + /** + * Sets the engine's shutdown handler. + * The shutdown handler is responsible for + * shutting down the JVM safely. + * + * @param shutdownHandler new shutdown handler + * @throws NoAccessException when restricted + * @since v1-alpha4 + */ + public void setShutdownHandler(@NotNull ShutdownHandler shutdownHandler) throws NoAccessException { + isRestricted(InternalAccessArea.SHUTDOWN_HANDLER_UPDATE); + Engine.getInstance().setShutdownHandler(shutdownHandler); + } + + /** + * Gets the engine's shutdown handler. + * The shutdown handler is responsible for + * shutting down the JVM safely. + * + * @return shutdown handler + * @throws NoAccessException when restricted + * @since v1-alpha4 + */ + public @NotNull ShutdownHandler getShutdownHandler() throws NoAccessException { + isRestricted(InternalAccessArea.SHUTDOWN_HANDLER_GET); + return Engine.getInstance().getShutdownHandler(); + } } diff --git a/base/src/main/java/de/staropensource/sosengine/base/types/InternalAccessArea.java b/base/src/main/java/de/staropensource/sosengine/base/types/InternalAccessArea.java index 56726ba..81309c1 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/types/InternalAccessArea.java +++ b/base/src/main/java/de/staropensource/sosengine/base/types/InternalAccessArea.java @@ -20,6 +20,7 @@ package de.staropensource.sosengine.base.types; import de.staropensource.sosengine.base.EngineInternals; +import de.staropensource.sosengine.base.classes.ShutdownHandler; /** * Specifies multiple areas of internal engine access. @@ -36,6 +37,20 @@ public enum InternalAccessArea { */ ALL, + /** + * Refers to all write-only areas. + * + * @since v1-alpha4 + */ + ALL_WRITE, + + /** + * Refers to all read-only areas. + * + * @since v1-alpha4 + */ + ALL_READ, + /** * Refers to the toggling of the JVM shutdown hook, which * prevents JVM shutdowns without the engine first shutting down. @@ -43,4 +58,47 @@ public enum InternalAccessArea { * @since v1-alpha4 */ SAFETY_SHUTDOWN_HOOK, + + /** + * Refers to the getting of the engine's shutdown handler. + * The {@link ShutdownHandler} handles the shutdown sequence + * after the engine has been shut down. + * + * @since v1-alpha4 + */ + SHUTDOWN_HANDLER_GET, + + /** + * Refers to the updating of the engine's shutdown handler. + * The {@link ShutdownHandler} handles the shutdown sequence + * after the engine has been shut down. + * + * @since v1-alpha4 + */ + SHUTDOWN_HANDLER_UPDATE; + + /** + * Returns all read-only areas. + * + * @return array containing all read-only areas + * @since v1-alpha4 + */ + public static InternalAccessArea[] valuesReadOnly() { + return new InternalAccessArea[]{ + SHUTDOWN_HANDLER_GET, + }; + } + + /** + * Returns all write-only areas. + * + * @return array containing all write-only areas + * @since v1-alpha4 + */ + public static InternalAccessArea[] valuesWriteOnly() { + return new InternalAccessArea[]{ + SAFETY_SHUTDOWN_HOOK, + SHUTDOWN_HANDLER_UPDATE, + }; + } }