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());
}
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;
}
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;
}
Aggregations