From 5b61851bb1713b74710cede42e85e8fbb0f8fead Mon Sep 17 00:00:00 2001 From: Decebal Suiu Date: Sat, 17 Aug 2024 19:44:51 +0300 Subject: [PATCH] Add helper methods in PluginState --- .../org/pf4j/AbstractExtensionFinder.java | 16 +++--- .../java/org/pf4j/AbstractPluginManager.java | 22 ++++---- pf4j/src/main/java/org/pf4j/PluginState.java | 51 ++++++++++++++++++- .../org/pf4j/SingletonExtensionFactory.java | 2 +- .../org/pf4j/DefaultPluginManagerTest.java | 6 +-- 5 files changed, 73 insertions(+), 24 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java index 998a29d..980594c 100644 --- a/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java +++ b/pf4j/src/main/java/org/pf4j/AbstractExtensionFinder.java @@ -88,7 +88,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin if (pluginId != null) { PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId); - if (PluginState.STARTED != pluginWrapper.getPluginState()) { + if (!pluginWrapper.getPluginState().isStarted()) { return result; } @@ -121,7 +121,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin List missingPluginIds = new ArrayList<>(); for (String requiredPluginId : extensionInfo.getPlugins()) { PluginWrapper requiredPlugin = pluginManager.getPlugin(requiredPluginId); - if (requiredPlugin == null || !PluginState.STARTED.equals(requiredPlugin.getPluginState())) { + if (requiredPlugin == null || !requiredPlugin.getPluginState().isStarted()) { missingPluginIds.add(requiredPluginId); } } @@ -179,7 +179,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin if (pluginId != null) { PluginWrapper pluginWrapper = pluginManager.getPlugin(pluginId); - if (PluginState.STARTED != pluginWrapper.getPluginState()) { + if (!pluginWrapper.getPluginState().isStarted()) { return result; } @@ -231,13 +231,13 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin // clear cache entries = null; - // By default we're assuming, that no checks for extension dependencies are necessary. + // By default, we're assuming, that no checks for extension dependencies are necessary. // // A plugin, that has an optional dependency to other plugins, might lead to unloadable // Java classes (NoClassDefFoundError) at application runtime due to possibly missing - // dependencies. Therefore we're enabling the check for optional extensions, if the + // dependencies. Therefore, we're enabling the check for optional extensions, if the // started plugin contains at least one optional plugin dependency. - if (checkForExtensionDependencies == null && PluginState.STARTED.equals(event.getPluginState())) { + if (checkForExtensionDependencies == null && event.getPluginState().isStarted()) { for (PluginDependency dependency : event.getPlugin().getDescriptor().getDependencies()) { if (dependency.isOptional()) { log.debug("Enable check for extension dependencies via ASM."); @@ -255,7 +255,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin *

* This feature is enabled by default, if at least one available plugin makes use of * optional plugin dependencies. Those optional plugins might not be available at runtime. - * Therefore any extension is checked by default against available plugins before its + * Therefore, any extension is checked by default against available plugins before its * instantiation. *

* Notice: This feature requires the optional ASM library @@ -274,7 +274,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin *

* This feature is enabled by default, if at least one available plugin makes use of * optional plugin dependencies. Those optional plugins might not be available at runtime. - * Therefore any extension is checked by default against available plugins before its + * Therefore, any extension is checked by default against available plugins before its * instantiation. *

* Notice: This feature requires the optional ASM library diff --git a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java b/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java index b9ba5c5..f576210 100644 --- a/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java +++ b/pf4j/src/main/java/org/pf4j/AbstractPluginManager.java @@ -312,7 +312,7 @@ public abstract class AbstractPluginManager implements PluginManager { PluginState pluginState; try { pluginState = stopPlugin(pluginId, false); - if (PluginState.STARTED == pluginState) { + if (pluginState.isStarted()) { return false; } @@ -358,7 +358,7 @@ public abstract class AbstractPluginManager implements PluginManager { PluginWrapper pluginWrapper = getPlugin(pluginId); // stop the plugin if it's started PluginState pluginState = stopPlugin(pluginId); - if (PluginState.STARTED == pluginState) { + if (pluginState.isStarted()) { log.error("Failed to stop plugin '{}' on delete", pluginId); return false; } @@ -385,7 +385,7 @@ public abstract class AbstractPluginManager implements PluginManager { public void startPlugins() { for (PluginWrapper pluginWrapper : resolvedPlugins) { PluginState pluginState = pluginWrapper.getPluginState(); - if ((PluginState.DISABLED != pluginState) && (PluginState.STARTED != pluginState)) { + if (!pluginState.isDisabled() && !pluginState.isStarted()) { try { log.info("Start plugin '{}'", getPluginLabel(pluginWrapper.getDescriptor())); pluginWrapper.getPlugin().start(); @@ -413,7 +413,7 @@ public abstract class AbstractPluginManager implements PluginManager { PluginWrapper pluginWrapper = getPlugin(pluginId); PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); - if (PluginState.STARTED == pluginState) { + if (pluginState.isStarted()) { log.debug("Already started plugin '{}'", getPluginLabel(pluginDescriptor)); return PluginState.STARTED; } @@ -423,7 +423,7 @@ public abstract class AbstractPluginManager implements PluginManager { return pluginState; } - if (PluginState.DISABLED == pluginState) { + if (pluginState.isDisabled()) { // automatically enable plugin on manual plugin start if (!enablePlugin(pluginId)) { return pluginState; @@ -458,7 +458,7 @@ public abstract class AbstractPluginManager implements PluginManager { while (itr.hasNext()) { PluginWrapper pluginWrapper = itr.next(); PluginState pluginState = pluginWrapper.getPluginState(); - if (PluginState.STARTED == pluginState) { + if (pluginState.isStarted()) { try { log.info("Stop plugin '{}'", getPluginLabel(pluginWrapper.getDescriptor())); pluginWrapper.getPlugin().stop(); @@ -550,11 +550,11 @@ public abstract class AbstractPluginManager implements PluginManager { PluginWrapper pluginWrapper = getPlugin(pluginId); PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); - if (PluginState.DISABLED == pluginState) { + if (pluginState.isDisabled()) { log.debug("Already disabled plugin '{}'", getPluginLabel(pluginDescriptor)); return true; - } else if (PluginState.STARTED == pluginState) { - if (PluginState.STOPPED != stopPlugin(pluginId)) { + } else if (pluginState.isStarted()) { + if (!stopPlugin(pluginId).isStopped()) { log.error("Failed to stop plugin '{}' on disable", getPluginLabel(pluginDescriptor)); return false; } @@ -582,7 +582,7 @@ public abstract class AbstractPluginManager implements PluginManager { PluginDescriptor pluginDescriptor = pluginWrapper.getDescriptor(); PluginState pluginState = pluginWrapper.getPluginState(); - if (PluginState.DISABLED != pluginState) { + if (!pluginState.isDisabled()) { log.debug("Plugin '{}' is not disabled", getPluginLabel(pluginDescriptor)); return true; } @@ -842,7 +842,7 @@ public abstract class AbstractPluginManager implements PluginManager { PluginWrapper pluginWrapper = plugins.get(pluginId); if (unresolvedPlugins.remove(pluginWrapper)) { PluginState pluginState = pluginWrapper.getPluginState(); - if (pluginState != PluginState.DISABLED) { + if (!pluginState.isDisabled()) { pluginWrapper.setPluginState(PluginState.RESOLVED); } diff --git a/pf4j/src/main/java/org/pf4j/PluginState.java b/pf4j/src/main/java/org/pf4j/PluginState.java index 6a6c1f9..c6ed044 100644 --- a/pf4j/src/main/java/org/pf4j/PluginState.java +++ b/pf4j/src/main/java/org/pf4j/PluginState.java @@ -72,8 +72,57 @@ public enum PluginState { this.status = status; } + /** + * Returns {@code true} if the value is {@link #CREATED}. + */ + public boolean isCreated() { + return this == CREATED; + } + + /** + * Returns {@code true} if the value is {@link #DISABLED}. + */ + public boolean isDisabled() { + return this == DISABLED; + } + + /** + * Returns {@code true} if the value is {@link #RESOLVED}. + */ + public boolean isResolved() { + return this == RESOLVED; + } + + /** + * Returns {@code true} if the value is {@link #STARTED}. + */ + public boolean isStarted() { + return this == STARTED; + } + + /** + * Returns {@code true} if the value is {@link #STOPPED}. + */ + public boolean isStopped() { + return this == STOPPED; + } + + /** + * Returns {@code true} if the value is {@link #FAILED}. + */ + public boolean isFailed() { + return this == FAILED; + } + + /** + * Returns {@code true} if the value is {@link #UNLOADED}. + */ + public boolean isUnloaded() { + return this == UNLOADED; + } + public boolean equals(String status) { - return (this.status.equalsIgnoreCase(status)); + return this.status.equalsIgnoreCase(status); } @Override diff --git a/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java b/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java index 0eef139..a7b8903 100644 --- a/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java +++ b/pf4j/src/main/java/org/pf4j/SingletonExtensionFactory.java @@ -39,7 +39,7 @@ public class SingletonExtensionFactory extends DefaultExtensionFactory { cache = new HashMap<>(); pluginManager.addPluginStateListener(event -> { - if (event.getPluginState() != PluginState.STARTED) { + if (!event.getPluginState().isStarted()) { cache.remove(event.getPlugin().getPluginClassLoader()); } }); diff --git a/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java b/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java index 2562260..bc31b84 100644 --- a/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java +++ b/pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java @@ -203,7 +203,7 @@ class DefaultPluginManagerTest { assertFalse(pluginZip.file().exists()); Optional unloadedEvent = receivedEvents.stream() - .filter(event -> event.getPluginState() == PluginState.UNLOADED) + .filter(event -> event.getPluginState().isUnloaded()) .findFirst(); assertTrue(unloadedEvent.isPresent()); @@ -226,7 +226,7 @@ class DefaultPluginManagerTest { assertFalse(pluginJar.file().exists()); Optional unloadedEvent = receivedEvents.stream() - .filter(event -> event.getPluginState() == PluginState.UNLOADED) + .filter(event -> event.getPluginState().isUnloaded()) .findFirst(); assertTrue(unloadedEvent.isPresent()); @@ -368,7 +368,7 @@ class DefaultPluginManagerTest { assertFalse(pluginZip.file().exists()); Optional unloadedEvent = receivedEvents.stream() - .filter(event -> event.getPluginState() == PluginState.UNLOADED) + .filter(event -> event.getPluginState().isUnloaded()) .findFirst(); assertTrue(unloadedEvent.isPresent()); -- 2.39.5