Handle errors for the components

This commit is contained in:
Norbi Peti 2019-01-01 03:26:43 +01:00
parent ff246b8c8b
commit 7f385df3b1
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 56 additions and 37 deletions

View file

@ -1,7 +1,7 @@
package buttondevteam.core; package buttondevteam.core;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException;
import lombok.val; import lombok.val;
public final class ComponentManager { public final class ComponentManager {
@ -23,7 +23,8 @@ public final class ComponentManager {
Component.getComponents().values().stream().filter(c -> c.shouldBeEnabled().get()).forEach(c -> { Component.getComponents().values().stream().filter(c -> c.shouldBeEnabled().get()).forEach(c -> {
try { try {
Component.setComponentEnabled(c, true); 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; componentsEnabled = true;
@ -36,7 +37,8 @@ public final class ComponentManager {
Component.getComponents().values().stream().filter(Component::isEnabled).forEach(c -> { Component.getComponents().values().stream().filter(Component::isEnabled).forEach(c -> {
try { try {
Component.setComponentEnabled(c, false); 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; componentsEnabled = false;

View file

@ -59,9 +59,10 @@ public abstract class Component {
* The component will be enabled automatically, regardless of when it was registered. * The component will be enabled automatically, regardless of when it was registered.
* *
* @param component The component to register * @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) { public static boolean registerComponent(JavaPlugin plugin, Component component) {
registerUnregisterComponent(plugin, component, true); return registerUnregisterComponent(plugin, component, true);
} }
/** /**
@ -69,22 +70,24 @@ public abstract class Component {
* Make sure to unregister the dependencies last. * Make sure to unregister the dependencies last.
* *
* @param componentClass The component class to unregister * @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<? extends Component> componentClass) { public static boolean unregisterComponent(JavaPlugin plugin, Class<? extends Component> componentClass) {
val component = components.get(componentClass); val component = components.get(componentClass);
if (component == null) if (component == null)
return; //Failed to load return false; //Failed to load
registerUnregisterComponent(plugin, component, false); return registerUnregisterComponent(plugin, component, false);
} }
public static void registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) { public static boolean registerUnregisterComponent(JavaPlugin plugin, Component component, boolean register) {
try {
val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class); val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class);
if (metaAnn != null) { if (metaAnn != null) {
Class<? extends Component>[] dependencies = metaAnn.depends(); Class<? extends Component>[] dependencies = metaAnn.depends();
for (val dep : dependencies) { for (val dep : dependencies) {
if (!components.containsKey(dep)) { if (!components.containsKey(dep)) {
plugin.getLogger().warning("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + " as a required dependency is missing/disabled: " + dep.getSimpleName()); plugin.getLogger().warning("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + " as a required dependency is missing/disabled: " + dep.getSimpleName());
return; return false;
} }
} }
} }
@ -99,16 +102,30 @@ public abstract class Component {
if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) { if (ComponentManager.areComponentsEnabled() && component.shouldBeEnabled().get()) {
try { //Enable components registered after the previous ones getting enabled try { //Enable components registered after the previous ones getting enabled
setComponentEnabled(component, true); setComponentEnabled(component, true);
} catch (UnregisteredComponentException ignored) { return true;
} catch (Exception | NoClassDefFoundError e) {
TBMCCoreAPI.SendException("Failed to enable component " + component.getClassName() + "!", e);
return true;
} }
} }
return true;
} else { } else {
if (component.enabled) { if (component.enabled) {
try {
component.disable(); component.disable();
component.enabled = false; 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); component.unregister(plugin);
components.remove(component.getClass()); components.remove(component.getClass());
return true;
}
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + "!", e);
return false;
} }
} }

View file

@ -2,7 +2,7 @@ package buttondevteam.lib.architecture.exceptions;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
public class UnregisteredComponentException extends Throwable { public class UnregisteredComponentException extends Exception {
public UnregisteredComponentException(Component component) { public UnregisteredComponentException(Component component) {
super("The component '" + component.getClass().getSimpleName() + "' isn't registered!"); super("The component '" + component.getClass().getSimpleName() + "' isn't registered!");