Move ShutdownHandler code into EngineInternals
This commit is contained in:
parent
f1fb21dbb1
commit
d06f8a3dc3
3 changed files with 116 additions and 11 deletions
|
@ -148,8 +148,8 @@ public final class Engine extends SubsystemClass {
|
||||||
* @see ShutdownHandler
|
* @see ShutdownHandler
|
||||||
* @since v1-alpha2
|
* @since v1-alpha2
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter(value = AccessLevel.MODULE)
|
||||||
@Setter
|
@Setter(value = AccessLevel.MODULE)
|
||||||
private @NotNull ShutdownHandler shutdownHandler = new Engine.JvmShutdownHandler();
|
private @NotNull ShutdownHandler shutdownHandler = new Engine.JvmShutdownHandler();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package de.staropensource.sosengine.base;
|
package de.staropensource.sosengine.base;
|
||||||
|
|
||||||
|
import de.staropensource.sosengine.base.classes.ShutdownHandler;
|
||||||
import de.staropensource.sosengine.base.exceptions.NoAccessException;
|
import de.staropensource.sosengine.base.exceptions.NoAccessException;
|
||||||
import de.staropensource.sosengine.base.logging.LoggerInstance;
|
import de.staropensource.sosengine.base.logging.LoggerInstance;
|
||||||
import de.staropensource.sosengine.base.types.InternalAccessArea;
|
import de.staropensource.sosengine.base.types.InternalAccessArea;
|
||||||
|
@ -26,6 +27,7 @@ import lombok.Getter;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
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
|
* @since v1-alpha4
|
||||||
*/
|
*/
|
||||||
public void restrictAccess(@NotNull InternalAccessArea area) {
|
public void restrictAccess(@NotNull InternalAccessArea area) {
|
||||||
if (area == InternalAccessArea.ALL) {
|
switch (area) {
|
||||||
|
case ALL -> {
|
||||||
List<@NotNull InternalAccessArea> areas = new ArrayList<>(List.of(InternalAccessArea.values()));
|
List<@NotNull InternalAccessArea> areas = new ArrayList<>(List.of(InternalAccessArea.values()));
|
||||||
areas.remove(InternalAccessArea.ALL);
|
areas.remove(InternalAccessArea.ALL);
|
||||||
|
areas.remove(InternalAccessArea.ALL_READ);
|
||||||
|
areas.remove(InternalAccessArea.ALL_WRITE);
|
||||||
restrictedAreas.addAll(areas);
|
restrictedAreas.addAll(areas);
|
||||||
}
|
}
|
||||||
|
case ALL_READ -> restrictedAreas.addAll(Arrays.stream(InternalAccessArea.valuesReadOnly()).toList());
|
||||||
restrictedAreas.add(area);
|
case ALL_WRITE -> restrictedAreas.addAll(Arrays.stream(InternalAccessArea.valuesWriteOnly()).toList());
|
||||||
|
default -> restrictedAreas.add(area);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,8 +124,7 @@ public final class EngineInternals {
|
||||||
* @since v1-alpha4
|
* @since v1-alpha4
|
||||||
*/
|
*/
|
||||||
public void installSafetyShutdownHook(boolean status) throws NoAccessException {
|
public void installSafetyShutdownHook(boolean status) throws NoAccessException {
|
||||||
if (restrictedAreas.contains(InternalAccessArea.SAFETY_SHUTDOWN_HOOK))
|
isRestricted(InternalAccessArea.SAFETY_SHUTDOWN_HOOK);
|
||||||
throw new NoAccessException("The internal access area SAFETY_SHUTDOWN_HOOK has been restricted");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (status)
|
if (status)
|
||||||
|
@ -114,4 +133,32 @@ public final class EngineInternals {
|
||||||
Runtime.getRuntime().removeShutdownHook(Engine.getInstance().getSafetyShutdownHook());
|
Runtime.getRuntime().removeShutdownHook(Engine.getInstance().getSafetyShutdownHook());
|
||||||
} catch (IllegalArgumentException | IllegalStateException ignored) {}
|
} 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
package de.staropensource.sosengine.base.types;
|
package de.staropensource.sosengine.base.types;
|
||||||
|
|
||||||
import de.staropensource.sosengine.base.EngineInternals;
|
import de.staropensource.sosengine.base.EngineInternals;
|
||||||
|
import de.staropensource.sosengine.base.classes.ShutdownHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies multiple areas of internal engine access.
|
* Specifies multiple areas of internal engine access.
|
||||||
|
@ -36,6 +37,20 @@ public enum InternalAccessArea {
|
||||||
*/
|
*/
|
||||||
ALL,
|
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
|
* Refers to the toggling of the JVM shutdown hook, which
|
||||||
* prevents JVM shutdowns without the engine first shutting down.
|
* prevents JVM shutdowns without the engine first shutting down.
|
||||||
|
@ -43,4 +58,47 @@ public enum InternalAccessArea {
|
||||||
* @since v1-alpha4
|
* @since v1-alpha4
|
||||||
*/
|
*/
|
||||||
SAFETY_SHUTDOWN_HOOK,
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue