use of com.devonfw.cobigen.api.extension.Priority in project cobigen by devonfw.
the class PluginRegistry method getPriority.
/**
* Extracts the {@link ReaderPriority} of a trigger interpreter
*
* @param clazz class to get the {@link ReaderPriority} annotation from (commonly the TriggerInterpreter classes)
* @return the priority of the input reader
*/
private static Priority getPriority(Class<? extends TriggerInterpreter> clazz) {
Priority priority;
if (clazz.getClass().isAnnotationPresent(ReaderPriority.class)) {
ReaderPriority[] annotation = clazz.getClass().getAnnotationsByType(ReaderPriority.class);
priority = annotation[0].value();
} else {
try {
priority = (Priority) ReaderPriority.class.getMethod("value").getDefaultValue();
} catch (NoSuchMethodException | SecurityException e) {
LOG.error("Could not find value() method of ReaderPriority. This should be an invalid case. Setting priority to hardcoded LOW to proceed. Please anyhow report a bug please.");
priority = Priority.LOW;
}
}
return priority;
}
use of com.devonfw.cobigen.api.extension.Priority in project cobigen by devonfw.
the class PluginRegistry method getTriggerInterpreters.
/**
* Returns a {@link Map} of all {@link TriggerInterpreter} keys.
*
* @param inputPath the path of the input to be read to just return the valid {@link TriggerInterpreter}s in order
* sorted by {@link Priority}
*
* @return all {@link TriggerInterpreter} keys as a set of strings.
*/
public static List<TriggerInterpreter> getTriggerInterpreters(Path inputPath) {
String extension;
if (inputPath.toFile().isFile()) {
extension = FilenameUtils.getExtension(inputPath.getFileName().toString());
LOG.debug("Trying to find trigger interpreter by file extension '{}'", extension);
for (Class<? extends GeneratorPluginActivator> activatorClass : ClassServiceLoader.getGeneratorPluginActivatorClasses()) {
LOG.debug("Checking found plug-in activator '{}'", activatorClass);
if (activatorClass.isAnnotationPresent(Activation.class)) {
Activation activation = activatorClass.getAnnotation(Activation.class);
String[] byFileExtension = activation.byFileExtension();
if (LOG.isDebugEnabled()) {
LOG.debug("Plug-in will be activated by file extensions '{}'.", Arrays.stream(byFileExtension).collect(Collectors.joining(",")));
}
Arrays.sort(byFileExtension);
if (Arrays.binarySearch(byFileExtension, extension) >= 0 && !loadedPlugins.containsKey(activatorClass)) {
loadPlugin(activatorClass);
} else {
LOG.debug("File extension not found. Skipping.");
}
} else {
LOG.debug("Activator annotation not present. Skipping.");
}
}
} else {
// directory
extension = FOLDER;
LOG.debug("Trying to find trigger interpreter by for folder inputs");
for (Class<? extends GeneratorPluginActivator> activatorClass : ClassServiceLoader.getGeneratorPluginActivatorClasses()) {
LOG.debug("Checking found plug-in activator '{}'", activatorClass);
if (activatorClass.isAnnotationPresent(Activation.class)) {
Activation activation = activatorClass.getAnnotation(Activation.class);
if (activation.byFolder() && !loadedPlugins.containsKey(activatorClass)) {
loadPlugin(activatorClass);
}
} else {
LOG.debug("Activator annotation not present. Skipping.");
}
}
}
List<TriggerInterpreter> sortedPlugins = registeredTriggerInterpreterByFileExtension.get(extension).stream().sorted((a, b) -> {
Priority priorityA = getPriority(a.getClass());
Priority priorityB = getPriority(b.getClass());
return SignedBytes.compare(priorityA.getRank(), priorityB.getRank());
}).collect(Collectors.toList());
return sortedPlugins;
}
Aggregations