use of org.apache.logging.log4j.core.config.plugins.PluginAliases in project logging-log4j2 by apache.
the class PluginBuilder method generateParameters.
private Object[] generateParameters(final Method factory) {
final StringBuilder log = new StringBuilder();
final Class<?>[] types = factory.getParameterTypes();
final Annotation[][] annotations = factory.getParameterAnnotations();
final Object[] args = new Object[annotations.length];
boolean invalid = false;
for (int i = 0; i < annotations.length; i++) {
log.append(log.length() == 0 ? factory.getName() + "(" : ", ");
final String[] aliases = extractPluginAliases(annotations[i]);
for (final Annotation a : annotations[i]) {
if (a instanceof PluginAliases) {
// already processed
continue;
}
final PluginVisitor<? extends Annotation> visitor = PluginVisitors.findVisitor(a.annotationType());
if (visitor != null) {
final Object value = visitor.setAliases(aliases).setAnnotation(a).setConversionType(types[i]).setStrSubstitutor(configuration.getStrSubstitutor()).setMember(factory).visit(configuration, node, event, log);
// don't overwrite existing values if the visitor gives us no value to inject
if (value != null) {
args[i] = value;
}
}
}
final Collection<ConstraintValidator<?>> validators = ConstraintValidators.findValidators(annotations[i]);
final Object value = args[i];
final String argName = "arg[" + i + "](" + simpleName(value) + ")";
for (final ConstraintValidator<?> validator : validators) {
if (!validator.isValid(argName, value)) {
invalid = true;
}
}
}
log.append(log.length() == 0 ? factory.getName() + "()" : ")");
checkForRemainingAttributes();
verifyNodeChildrenUsed();
LOGGER.debug(log.toString());
if (invalid) {
throw new ConfigurationException("Arguments given for element " + node.getName() + " are invalid");
}
return args;
}
use of org.apache.logging.log4j.core.config.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 PluginEntry fake = pluginCache.getCategory(p.category()).get(alias.toLowerCase());
verifyFakePluginEntry(alias, fake);
}
}
use of org.apache.logging.log4j.core.config.plugins.PluginAliases in project logging-log4j2 by apache.
the class PluginBuilder method injectFields.
private void injectFields(final Builder<?> builder) throws IllegalAccessException {
final List<Field> fields = TypeUtil.getAllDeclaredFields(builder.getClass());
AccessibleObject.setAccessible(fields.toArray(new Field[] {}), true);
final StringBuilder log = new StringBuilder();
boolean invalid = false;
for (final Field field : fields) {
log.append(log.length() == 0 ? simpleName(builder) + "(" : ", ");
final Annotation[] annotations = field.getDeclaredAnnotations();
final String[] aliases = extractPluginAliases(annotations);
for (final Annotation a : annotations) {
if (a instanceof PluginAliases) {
// already processed
continue;
}
final PluginVisitor<? extends Annotation> visitor = PluginVisitors.findVisitor(a.annotationType());
if (visitor != null) {
final Object value = visitor.setAliases(aliases).setAnnotation(a).setConversionType(field.getType()).setStrSubstitutor(configuration.getStrSubstitutor()).setMember(field).visit(configuration, node, event, log);
// don't overwrite default values if the visitor gives us no value to inject
if (value != null) {
field.set(builder, value);
}
}
}
final Collection<ConstraintValidator<?>> validators = ConstraintValidators.findValidators(annotations);
final Object value = field.get(builder);
for (final ConstraintValidator<?> validator : validators) {
if (!validator.isValid(field.getName(), value)) {
invalid = true;
}
}
}
log.append(log.length() == 0 ? builder.getClass().getSimpleName() + "()" : ")");
LOGGER.debug(log.toString());
if (invalid) {
throw new ConfigurationException("Arguments given for element " + node.getName() + " are invalid");
}
checkForRemainingAttributes();
verifyNodeChildrenUsed();
}
use of org.apache.logging.log4j.core.config.plugins.PluginAliases 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;
}
Aggregations