Search in sources :

Example 1 with Plugin

use of org.apache.logging.log4j.core.config.plugins.Plugin in project logging-log4j2 by apache.

the class PluginProcessorTest method testNestedPlugin.

@Test
public void testNestedPlugin() throws Exception {
    final Plugin p = FakePlugin.Nested.class.getAnnotation(Plugin.class);
    final PluginEntry nested = pluginCache.getCategory(p.category()).get(p.name().toLowerCase());
    assertNotNull(nested);
    assertEquals(p.name().toLowerCase(), nested.getKey());
    assertEquals(FakePlugin.Nested.class.getName(), nested.getClassName());
    assertEquals(p.name(), nested.getName());
    assertEquals(Plugin.EMPTY, p.elementType());
    assertEquals(p.printObject(), nested.isPrintable());
    assertEquals(p.deferChildren(), nested.isDefer());
}
Also used : Plugin(org.apache.logging.log4j.core.config.plugins.Plugin) Test(org.junit.Test)

Example 2 with Plugin

use of org.apache.logging.log4j.core.config.plugins.Plugin in project logging-log4j2 by apache.

the class PluginProcessor method collectPlugins.

private void collectPlugins(final Iterable<? extends Element> elements) {
    final Elements elementUtils = processingEnv.getElementUtils();
    final ElementVisitor<PluginEntry, Plugin> pluginVisitor = new PluginElementVisitor(elementUtils);
    final ElementVisitor<Collection<PluginEntry>, Plugin> pluginAliasesVisitor = new PluginAliasesElementVisitor(elementUtils);
    for (final Element element : elements) {
        final Plugin plugin = element.getAnnotation(Plugin.class);
        if (plugin == null) {
            continue;
        }
        final PluginEntry entry = element.accept(pluginVisitor, plugin);
        final Map<String, PluginEntry> category = pluginCache.getCategory(entry.getCategory());
        category.put(entry.getKey(), entry);
        final Collection<PluginEntry> entries = element.accept(pluginAliasesVisitor, plugin);
        for (final PluginEntry pluginEntry : entries) {
            category.put(pluginEntry.getKey(), pluginEntry);
        }
    }
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) Collection(java.util.Collection) Elements(javax.lang.model.util.Elements) Plugin(org.apache.logging.log4j.core.config.plugins.Plugin)

Example 3 with Plugin

use of org.apache.logging.log4j.core.config.plugins.Plugin in project logging-log4j2 by apache.

the class PluginRegistry method loadFromPackage.

/**
     * @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 = Loader.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.get(categoryLowerCase);
        if (list == null) {
            newPluginsByCategory.put(categoryLowerCase, list = 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);
            }
        }
    }
    final long endTime = System.nanoTime();
    final DecimalFormat numFormat = new DecimalFormat("#0.000000");
    final double seconds = (endTime - startTime) * 1e-9;
    LOGGER.debug("Took {} seconds to load {} plugins from package {}", numFormat.format(seconds), resolver.getClasses().size(), pkg);
    // 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;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) PluginEntry(org.apache.logging.log4j.core.config.plugins.processor.PluginEntry) PluginAliases(org.apache.logging.log4j.core.config.plugins.PluginAliases) ArrayList(java.util.ArrayList) List(java.util.List) Plugin(org.apache.logging.log4j.core.config.plugins.Plugin)

Aggregations

Plugin (org.apache.logging.log4j.core.config.plugins.Plugin)3 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 Elements (javax.lang.model.util.Elements)1 PluginAliases (org.apache.logging.log4j.core.config.plugins.PluginAliases)1 PluginEntry (org.apache.logging.log4j.core.config.plugins.processor.PluginEntry)1 Test (org.junit.Test)1