use of uk.co.drnaylor.quickstart.annotations.ModuleData in project QuickStartModuleLoader by NucleusPowered.
the class ModuleContainer method startDiscover.
public final void startDiscover() throws QuickStartModuleDiscoveryException {
try {
Preconditions.checkState(currentPhase == ConstructionPhase.INITALISED);
currentPhase = ConstructionPhase.DISCOVERING;
Set<Class<? extends Module>> modules = discoverModules();
HashMap<String, ModuleSpec> discovered = Maps.newHashMap();
for (Class<? extends Module> s : modules) {
// If we have a module annotation, we are golden.
String id;
ModuleSpec ms;
if (s.isAnnotationPresent(ModuleData.class)) {
ModuleData md = s.getAnnotation(ModuleData.class);
id = md.id().toLowerCase();
ms = new ModuleSpec(s, md);
} else if (this.requireAnnotation) {
loggerProxy.warn(MessageFormat.format("The module class {0} does not have a ModuleData annotation associated with it. " + "It is not being loaded as the module container requires the annotation to be present.", s.getClass().getName()));
continue;
} else {
id = s.getClass().getName().toLowerCase();
loggerProxy.warn(MessageFormat.format("The module {0} does not have a ModuleData annotation associated with it. We're just assuming an ID of {0}.", id));
ms = new ModuleSpec(s, id, id, LoadingStatus.ENABLED, false);
}
if (discovered.containsKey(id)) {
throw new QuickStartModuleDiscoveryException("Duplicate module ID \"" + id + "\" was discovered - loading cannot continue.");
}
discovered.put(id, ms);
}
// Create the dependency map.
resolveDependencyOrder(discovered);
// Modules discovered. Create the Module Config adapter.
List<ModuleSpec> moduleSpecList = this.discoveredModules.entrySet().stream().filter(x -> !x.getValue().isMandatory()).map(Map.Entry::getValue).collect(Collectors.toList());
// Attaches config adapter and loads in the defaults.
config.attachModulesConfig(moduleSpecList, this.descriptionProcessor, this.moduleSection, this.moduleSectionHeader);
config.saveAdapterDefaults(false);
// Load what we have in config into our discovered modules.
try {
config.getConfigAdapter().getNode().forEach((k, v) -> {
try {
ModuleSpec ms = discoveredModules.get(k);
if (ms != null) {
ms.setStatus(v);
} else {
loggerProxy.warn(String.format("Ignoring module entry %s in the configuration file: module does not exist.", k));
}
} catch (IllegalStateException ex) {
loggerProxy.warn("A mandatory module can't have its status changed by config. Falling back to FORCELOAD for " + k);
}
});
} catch (ObjectMappingException e) {
loggerProxy.warn("Could not load modules config, falling back to defaults.");
e.printStackTrace();
}
// Modules have been discovered.
currentPhase = ConstructionPhase.DISCOVERED;
} catch (QuickStartModuleDiscoveryException ex) {
throw ex;
} catch (Exception e) {
throw new QuickStartModuleDiscoveryException("Unable to discover QuickStart modules", e);
}
}
Aggregations