package org.apache.logging.log4j.core.config.plugins.util;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAliases;
import org.apache.logging.log4j.core.config.plugins.processor.PluginCache;
import org.apache.logging.log4j.core.config.plugins.processor.PluginEntry;
import org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Strings;
public class PluginRegistry {
private static final Logger LOGGER = StatusLogger.getLogger();
private static volatile PluginRegistry INSTANCE;
private static final Object INSTANCE_LOCK = new Object();
private final AtomicReference<Map<String, List<PluginType<?>>>> pluginsByCategoryRef =
new AtomicReference<>();
private final ConcurrentMap<Long, Map<String, List<PluginType<?>>>> pluginsByCategoryByBundleId =
new ConcurrentHashMap<>();
private final ConcurrentMap<String, Map<String, List<PluginType<?>>>> pluginsByCategoryByPackage =
new ConcurrentHashMap<>();
private PluginRegistry() {
}
public static PluginRegistry getInstance() {
PluginRegistry result = INSTANCE;
if (result == null) {
synchronized (INSTANCE_LOCK) {
result = INSTANCE;
if (result == null) {
INSTANCE = result = new PluginRegistry();
}
}
}
return result;
}
public void clear() {
pluginsByCategoryRef.set(null);
pluginsByCategoryByPackage.clear();
pluginsByCategoryByBundleId.clear();
}
public Map<Long, Map<String, List<PluginType<?>>>> getPluginsByCategoryByBundleId() {
return pluginsByCategoryByBundleId;
}
public Map<String, List<PluginType<?>>> loadFromMainClassLoader() {
final Map<String, List<PluginType<?>>> existing = pluginsByCategoryRef.get();
if (existing != null) {
return existing;
}
final Map<String, List<PluginType<?>>> newPluginsByCategory = decodeCacheFiles(Loader.getClassLoader());
if (pluginsByCategoryRef.compareAndSet(null, newPluginsByCategory)) {
return newPluginsByCategory;
}
return pluginsByCategoryRef.get();
}
public void clearBundlePlugins(final long bundleId) {
pluginsByCategoryByBundleId.remove(bundleId);
}
public Map<String, List<PluginType<?>>> loadFromBundle(final long bundleId, final ClassLoader loader) {
Map<String, List<PluginType<?>>> existing = pluginsByCategoryByBundleId.get(bundleId);
if (existing != null) {
return existing;
}
final Map<String, List<PluginType<?>>> newPluginsByCategory = decodeCacheFiles(loader);
existing = pluginsByCategoryByBundleId.putIfAbsent(bundleId, newPluginsByCategory);
if (existing != null) {
return existing;
}
return newPluginsByCategory;
}
private Map<String, List<PluginType<?>>> decodeCacheFiles(final ClassLoader loader) {
final long startTime = System.nanoTime();
final PluginCache cache = new PluginCache();
try {
final Enumeration<URL> resources = loader.getResources(PluginProcessor.PLUGIN_CACHE_FILE);
if (resources == null) {
LOGGER.info("Plugin preloads not available from class loader {}", loader);
} else {
cache.loadCacheFiles(resources);
}
} catch (final IOException ioe) {
LOGGER.warn("Unable to preload plugins", ioe);
}
final Map<String, List<PluginType<?>>> newPluginsByCategory = new HashMap<>();
int pluginCount = 0;
for (final Map.Entry<String, Map<String, PluginEntry>> outer : cache.getAllCategories().entrySet()) {
final String categoryLowerCase = outer.getKey();
final List<PluginType<?>> types = new ArrayList<>(outer.getValue().size());
newPluginsByCategory.put(categoryLowerCase, types);
for (final Map.Entry<String, PluginEntry> inner : outer.getValue().entrySet()) {
final PluginEntry entry = inner.getValue();
final String className = entry.getClassName();
try {
final Class<?> clazz = loader.loadClass(className);
final PluginType<?> type = new PluginType<>(entry, clazz, entry.getName());
types.add(type);
++pluginCount;
} catch (final ClassNotFoundException e) {
LOGGER.info("Plugin [{}] could not be loaded due to missing classes.", className, e);
} catch (final LinkageError e) {
LOGGER.info("Plugin [{}] could not be loaded due to linkage error.", className, e);
}
}
}
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 {}",
numFormat.format(seconds), pluginCount, loader);
return newPluginsByCategory;
}
public Map<String, List<PluginType<?>>> loadFromPackage(final String pkg) {
if (Strings.isBlank(pkg)) {
return Collections.emptyMap();
}
Map<String, List<PluginType<?>>> existing = pluginsByCategoryByPackage.get(pkg);
if (existing != null) {
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);
existing = pluginsByCategoryByPackage.putIfAbsent(pkg, newPluginsByCategory);
if (existing != null) {
return existing;
}
return newPluginsByCategory;
}
public static class PluginTest implements ResolverUtil.Test {
@Override
public boolean matches(final Class<?> type) {
return type != null && type.isAnnotationPresent(Plugin.class);
}
@Override
public String toString() {
return "annotated with @" + Plugin.class.getSimpleName();
}
@Override
public boolean matches(final URI resource) {
throw new UnsupportedOperationException();
}
@Override
public boolean doesMatchClass() {
return true;
}
@Override
public boolean doesMatchResource() {
return false;
}
}
}