Search in sources :

Example 1 with Activation

use of com.devonfw.cobigen.api.annotation.Activation in project cobigen by devonfw.

the class PluginRegistry method registerTriggerInterpreter.

/**
 * Registers the given {@link TriggerInterpreter}
 *
 * @param triggerInterpreter to be registered
 * @param plugin the plugin the trigger interpreter is located in
 */
public static void registerTriggerInterpreter(TriggerInterpreter triggerInterpreter, GeneratorPluginActivator plugin) {
    if (triggerInterpreter == null || StringUtils.isEmpty(triggerInterpreter.getType())) {
        throw new IllegalArgumentException("You cannot register a new TriggerInterpreter with triggerInterpreter==null or type==null or empty!");
    }
    registeredTriggerInterpreter.put(triggerInterpreter.getType(), triggerInterpreter);
    Activation annotation = plugin.getClass().getAnnotation(Activation.class);
    if (annotation != null) {
        for (String ext : annotation.byFileExtension()) {
            registeredTriggerInterpreterByFileExtension.put(ext, triggerInterpreter);
        }
        if (annotation.byFolder()) {
            registeredTriggerInterpreterByFileExtension.put(FOLDER, triggerInterpreter);
        }
    }
    LOG.debug("TriggerInterpreter for type '{}' registered ({}).", triggerInterpreter.getType(), triggerInterpreter.getClass().getCanonicalName());
}
Also used : Activation(com.devonfw.cobigen.api.annotation.Activation)

Example 2 with Activation

use of com.devonfw.cobigen.api.annotation.Activation 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)

Example 3 with Activation

use of com.devonfw.cobigen.api.annotation.Activation in project cobigen by devonfw.

the class PluginRegistry method getMerger.

/**
 * Returns the {@link Merger} for the given merge strategy
 *
 * @param mergeStrategy the {@link Merger} should be able to interpret
 * @return the {@link Merger} for the given mergerType or <code>null</code> if there is no {@link Merger} for this
 *         mergerType
 */
public static Merger getMerger(String mergeStrategy) {
    if (mergeStrategy == null) {
        return null;
    }
    Merger merger = registeredMerger.get(mergeStrategy);
    if (merger == null) {
        LOG.debug("Trying to find merger for type '{}' in {} registered plugins.", mergeStrategy, ClassServiceLoader.getGeneratorPluginActivatorClasses().size());
        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[] byMergeStrategy = activation.byMergeStrategy();
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Plug-in will be activated by merge strategies '{}'.", Arrays.stream(byMergeStrategy).collect(Collectors.joining(",")));
                }
                Arrays.sort(byMergeStrategy);
                if (Arrays.binarySearch(byMergeStrategy, mergeStrategy) >= 0) {
                    loadPlugin(activatorClass);
                    break;
                } else {
                    LOG.debug("Merge strategy not found. Skipping.");
                }
            } else {
                LOG.debug("Activator annotation not present. Skipping.");
            }
        }
        merger = registeredMerger.get(mergeStrategy);
    }
    if (merger != null) {
        merger = ProxyFactory.getProxy(merger);
    }
    return merger;
}
Also used : Merger(com.devonfw.cobigen.api.extension.Merger) Activation(com.devonfw.cobigen.api.annotation.Activation)

Aggregations

Activation (com.devonfw.cobigen.api.annotation.Activation)3 Merger (com.devonfw.cobigen.api.extension.Merger)2 ReaderPriority (com.devonfw.cobigen.api.annotation.ReaderPriority)1 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)1 GeneratorPluginActivator (com.devonfw.cobigen.api.extension.GeneratorPluginActivator)1 Priority (com.devonfw.cobigen.api.extension.Priority)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