Search in sources :

Example 1 with Priority

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;
}
Also used : ReaderPriority(com.devonfw.cobigen.api.annotation.ReaderPriority) ReaderPriority(com.devonfw.cobigen.api.annotation.ReaderPriority) Priority(com.devonfw.cobigen.api.extension.Priority)

Example 2 with 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;
}
Also used : TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) LoggerFactory(org.slf4j.LoggerFactory) SignedBytes(com.google.common.primitives.SignedBytes) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) Maps(com.google.common.collect.Maps) Activation(com.devonfw.cobigen.api.annotation.Activation) Merger(com.devonfw.cobigen.api.extension.Merger) ReaderPriority(com.devonfw.cobigen.api.annotation.ReaderPriority) List(java.util.List) HashMultimap(com.google.common.collect.HashMultimap) GeneratorPluginActivator(com.devonfw.cobigen.api.extension.GeneratorPluginActivator) Map(java.util.Map) Priority(com.devonfw.cobigen.api.extension.Priority) TriggerInterpreter(com.devonfw.cobigen.api.extension.TriggerInterpreter) Path(java.nio.file.Path) FilenameUtils(org.apache.commons.io.FilenameUtils) ProxyFactory(com.devonfw.cobigen.impl.aop.ProxyFactory) ReaderPriority(com.devonfw.cobigen.api.annotation.ReaderPriority) Priority(com.devonfw.cobigen.api.extension.Priority) Activation(com.devonfw.cobigen.api.annotation.Activation)

Aggregations

ReaderPriority (com.devonfw.cobigen.api.annotation.ReaderPriority)2 Priority (com.devonfw.cobigen.api.extension.Priority)2 Activation (com.devonfw.cobigen.api.annotation.Activation)1 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)1 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)1 Merger (com.devonfw.cobigen.api.extension.Merger)1 TriggerInterpreter (com.devonfw.cobigen.api.extension.TriggerInterpreter)1 ProxyFactory (com.devonfw.cobigen.impl.aop.ProxyFactory)1 HashMultimap (com.google.common.collect.HashMultimap)1 Maps (com.google.common.collect.Maps)1 Multimap (com.google.common.collect.Multimap)1 SignedBytes (com.google.common.primitives.SignedBytes)1 Path (java.nio.file.Path)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 FilenameUtils (org.apache.commons.io.FilenameUtils)1 StringUtils (org.apache.commons.lang3.StringUtils)1