From 7f385df3b16d584cab0c0f9dbd14f1c94b806623 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 1 Jan 2019 03:26:43 +0100 Subject: [PATCH] Handle errors for the components --- .../buttondevteam/core/ComponentManager.java | 8 +- .../lib/architecture/Component.java | 83 +++++++++++-------- .../UnregisteredComponentException.java | 2 +- 3 files changed, 56 insertions(+), 37 deletions(-) diff --git a/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java b/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java index 4e7b37d..0c3c51b 100644 --- a/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java +++ b/ButtonCore/src/main/java/buttondevteam/core/ComponentManager.java @@ -1,7 +1,7 @@ package buttondevteam.core; +import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.architecture.Component; -import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException; import lombok.val; public final class ComponentManager { @@ -23,7 +23,8 @@ public final class ComponentManager { Component.getComponents().values().stream().filter(c -> c.shouldBeEnabled().get()).forEach(c -> { try { Component.setComponentEnabled(c, true); - } catch (UnregisteredComponentException ignored) { //This *should* never happen + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to enable one of the components: " + c.getClass().getSimpleName(), e); } }); componentsEnabled = true; @@ -36,7 +37,8 @@ public final class ComponentManager { Component.getComponents().values().stream().filter(Component::isEnabled).forEach(c -> { try { Component.setComponentEnabled(c, false); - } catch (UnregisteredComponentException ignored) { //This *should* never happen + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to disable one of the components: " + c.getClass().getSimpleName(), e); } }); componentsEnabled = false; diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java index 57a98d5..4380812 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java @@ -59,9 +59,10 @@ public abstract class Component { * The component will be enabled automatically, regardless of when it was registered. * * @param component The component to register + * @return Whether the component is registered successfully (it may have failed to enable) */ - public static void registerComponent(JavaPlugin plugin, Component component) { - registerUnregisterComponent(plugin, component, true); + public static boolean registerComponent(JavaPlugin plugin, Component component) { + return registerUnregisterComponent(plugin, component, true); } /** @@ -69,46 +70,62 @@ public abstract class Component { * Make sure to unregister the dependencies last. * * @param componentClass The component class to unregister + * @return Whether the component is unregistered successfully (it also got disabled) */ - public static void unregisterComponent(JavaPlugin plugin, Class componentClass) { + public static boolean unregisterComponent(JavaPlugin plugin, Class componentClass) { val component = components.get(componentClass); if (component == null) - return; //Failed to load - registerUnregisterComponent(plugin, component, false); + return false; //Failed to load + return registerUnregisterComponent(plugin, component, false); } - public static void registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) { - val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class); - if (metaAnn != null) { - Class[] dependencies = metaAnn.depends(); - for (val dep : dependencies) { - if (!components.containsKey(dep)) { - plugin.getLogger().warning("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + " as a required dependency is missing/disabled: " + dep.getSimpleName()); - return; + public static boolean registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) { + try { + val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class); + if (metaAnn != null) { + Class[] dependencies = metaAnn.depends(); + for (val dep : dependencies) { + if (!components.containsKey(dep)) { + plugin.getLogger().warning("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + " as a required dependency is missing/disabled: " + dep.getSimpleName()); + return false; + } } } - } - if (register) { - component.plugin = plugin; - var compconf = plugin.getConfig().getConfigurationSection("components"); - if (compconf == null) compconf = plugin.getConfig().createSection("components"); - component.config = compconf.getConfigurationSection(component.getClassName()); - if (component.config == null) component.config = compconf.createSection(component.getClassName()); - component.register(plugin); - components.put(component.getClass(), component); - if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) { - try { //Enable components registered after the previous ones getting enabled - setComponentEnabled(component, true); - } catch (UnregisteredComponentException ignored) { + if (register) { + component.plugin = plugin; + var compconf = plugin.getConfig().getConfigurationSection("components"); + if (compconf == null) compconf = plugin.getConfig().createSection("components"); + component.config = compconf.getConfigurationSection(component.getClassName()); + if (component.config == null) component.config = compconf.createSection(component.getClassName()); + component.register(plugin); + components.put(component.getClass(), component); + if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) { + try { //Enable components registered after the previous ones getting enabled + setComponentEnabled(component, true); + return true; + } catch (Exception | NoClassDefFoundError e) { + TBMCCoreAPI.SendException("Failed to enable component " + component.getClassName() + "!", e); + return true; + } } + return true; + } else { + if (component.enabled) { + try { + component.disable(); + component.enabled = false; + } catch (Exception | NoClassDefFoundError e) { + TBMCCoreAPI.SendException("Failed to disable component " + component.getClassName() + "!", e); + return false; //If failed to disable, won't unregister either + } + } + component.unregister(plugin); + components.remove(component.getClass()); + return true; } - } else { - if (component.enabled) { - component.disable(); - component.enabled = false; - } - component.unregister(plugin); - components.remove(component.getClass()); + } catch (Exception e) { + TBMCCoreAPI.SendException("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + "!", e); + return false; } } diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/exceptions/UnregisteredComponentException.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/exceptions/UnregisteredComponentException.java index e86c302..b230fad 100644 --- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/exceptions/UnregisteredComponentException.java +++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/exceptions/UnregisteredComponentException.java @@ -2,7 +2,7 @@ package buttondevteam.lib.architecture.exceptions; import buttondevteam.lib.architecture.Component; -public class UnregisteredComponentException extends Throwable { +public class UnregisteredComponentException extends Exception { public UnregisteredComponentException(Component component) { super("The component '" + component.getClass().getSimpleName() + "' isn't registered!");