diff options
author | Decebal Suiu <decebal.suiu@gmail.com> | 2024-08-17 19:44:51 +0300 |
---|---|---|
committer | Decebal Suiu <decebal.suiu@gmail.com> | 2024-08-17 19:44:51 +0300 |
commit | 5b61851bb1713b74710cede42e85e8fbb0f8fead (patch) | |
tree | 127193eb60ca69c8109ff748bb06398a0f00ca2d /pf4j/src/main | |
parent | c6502786275ee53fdacee33ac35fc4d08faa7026 (diff) | |
download | pf4j-5b61851bb1713b74710cede42e85e8fbb0f8fead.tar.gz pf4j-5b61851bb1713b74710cede42e85e8fbb0f8fead.zip |
Add helper methods in PluginState
Diffstat (limited to 'pf4j/src/main')
4 files changed, 70 insertions, 21 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<String> 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 * <p> * 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. * <p> * Notice: This feature requires the optional <a href="https://asm.ow2.io/">ASM library</a> @@ -274,7 +274,7 @@ public abstract class AbstractExtensionFinder implements ExtensionFinder, Plugin * <p> * 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. * <p> * Notice: This feature requires the optional <a href="https://asm.ow2.io/">ASM library</a> 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()); } }); |