use of org.apache.logging.log4j.plugins.PluginAliases in project logging-log4j2 by apache.
the class PluginRegistry method loadFromPackage.
/**
* Load plugin types from a package.
* @param pkg The package name.
* @return A Map of the lists of plugin types organized by category.
* @since 2.1
*/
public Map<String, List<PluginType<?>>> loadFromPackage(final String pkg) {
if (Strings.isBlank(pkg)) {
// happens when splitting an empty string
return Collections.emptyMap();
}
Map<String, List<PluginType<?>>> existing = pluginsByCategoryByPackage.get(pkg);
if (existing != null) {
// already loaded this package
return existing;
}
final long startTime = System.nanoTime();
final ResolverUtil resolver = new ResolverUtil();
final ClassLoader classLoader = LoaderUtil.getClassLoader();
if (classLoader != null) {
resolver.setClassLoader(classLoader);
}
resolver.findInPackage(new PluginTest(), pkg);
final Map<String, List<PluginType<?>>> newPluginsByCategory = new HashMap<>();
for (final Class<?> clazz : resolver.getClasses()) {
final Plugin plugin = clazz.getAnnotation(Plugin.class);
final String categoryLowerCase = plugin.category().toLowerCase();
List<PluginType<?>> list = newPluginsByCategory.computeIfAbsent(categoryLowerCase, k -> new ArrayList<>());
final PluginEntry mainEntry = new PluginEntry();
final String mainElementName = plugin.elementType().equals(Plugin.EMPTY) ? plugin.name() : plugin.elementType();
mainEntry.setKey(plugin.name().toLowerCase());
mainEntry.setName(plugin.name());
mainEntry.setCategory(plugin.category());
mainEntry.setClassName(clazz.getName());
mainEntry.setPrintable(plugin.printObject());
mainEntry.setDefer(plugin.deferChildren());
final PluginType<?> mainType = new PluginType<>(mainEntry, clazz, mainElementName);
list.add(mainType);
final PluginAliases pluginAliases = clazz.getAnnotation(PluginAliases.class);
if (pluginAliases != null) {
for (final String alias : pluginAliases.value()) {
final PluginEntry aliasEntry = new PluginEntry();
final String aliasElementName = plugin.elementType().equals(Plugin.EMPTY) ? alias.trim() : plugin.elementType();
aliasEntry.setKey(alias.trim().toLowerCase());
aliasEntry.setName(plugin.name());
aliasEntry.setCategory(plugin.category());
aliasEntry.setClassName(clazz.getName());
aliasEntry.setPrintable(plugin.printObject());
aliasEntry.setDefer(plugin.deferChildren());
final PluginType<?> aliasType = new PluginType<>(aliasEntry, clazz, aliasElementName);
list.add(aliasType);
}
}
}
LOGGER.debug(() -> {
final long endTime = System.nanoTime();
StringBuilder sb = new StringBuilder("Took ");
final DecimalFormat numFormat = new DecimalFormat("#0.000000");
sb.append(numFormat.format((endTime - startTime) * 1e-9));
sb.append(" seconds to load ").append(resolver.getClasses().size());
sb.append(" plugins from package ").append(pkg);
return sb.toString();
});
// Note multiple threads could be calling this method concurrently. Both will do the work,
// but only one will be allowed to store the result in the outer map.
// Return the inner map produced by whichever thread won the race, so all callers will get the same result.
existing = pluginsByCategoryByPackage.putIfAbsent(pkg, newPluginsByCategory);
if (existing != null) {
return existing;
}
return newPluginsByCategory;
}
use of org.apache.logging.log4j.plugins.PluginAliases in project logging-log4j2 by apache.
the class PluginProcessorTest method testFakePluginAliasesContainSameInformation.
@Test
public void testFakePluginAliasesContainSameInformation() throws Exception {
final PluginAliases aliases = FakePlugin.class.getAnnotation(PluginAliases.class);
for (final String alias : aliases.value()) {
final List<PluginType<?>> list = pluginService.getCategory(p.category());
assertNotNull(list);
final PluginEntry fake = getEntry(list, alias);
assertNotNull(fake);
verifyFakePluginEntry(alias, fake);
}
}
Aggregations