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,46 +70,62 @@ 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) {
val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class); try {
if (metaAnn != null) { val metaAnn = component.getClass().getAnnotation(ComponentMetadata.class);
Class<? extends Component>[] dependencies = metaAnn.depends(); if (metaAnn != null) {
for (val dep : dependencies) { Class<? extends Component>[] dependencies = metaAnn.depends();
if (!components.containsKey(dep)) { for (val dep : dependencies) {
plugin.getLogger().warning("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + " as a required dependency is missing/disabled: " + dep.getSimpleName()); if (!components.containsKey(dep)) {
return; plugin.getLogger().warning("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + " as a required dependency is missing/disabled: " + dep.getSimpleName());
return false;
}
} }
} }
} if (register) {
if (register) { component.plugin = plugin;
component.plugin = plugin; var compconf = plugin.getConfig().getConfigurationSection("components");
var compconf = plugin.getConfig().getConfigurationSection("components"); if (compconf == null) compconf = plugin.getConfig().createSection("components");
if (compconf == null) compconf = plugin.getConfig().createSection("components"); component.config = compconf.getConfigurationSection(component.getClassName());
component.config = compconf.getConfigurationSection(component.getClassName()); if (component.config == null) component.config = compconf.createSection(component.getClassName());
if (component.config == null) component.config = compconf.createSection(component.getClassName()); component.register(plugin);
component.register(plugin); components.put(component.getClass(), component);
components.put(component.getClass(), 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); return true;
} catch (UnregisteredComponentException ignored) { } 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 { } catch (Exception e) {
if (component.enabled) { TBMCCoreAPI.SendException("Failed to " + (register ? "" : "un") + "register component " + component.getClassName() + "!", e);
component.disable(); return false;
component.enabled = false;
}
component.unregister(plugin);
components.remove(component.getClass());
} }
} }

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!");