Search in sources :

Example 1 with ConstraintValidator

use of org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator 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;
}
Also used : Annotation(java.lang.annotation.Annotation) ConstraintValidator(org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator) PluginAliases(org.apache.logging.log4j.core.config.plugins.PluginAliases) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 2 with ConstraintValidator

use of org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator 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();
}
Also used : Annotation(java.lang.annotation.Annotation) Field(java.lang.reflect.Field) ConstraintValidator(org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator) PluginAliases(org.apache.logging.log4j.core.config.plugins.PluginAliases) ConfigurationException(org.apache.logging.log4j.core.config.ConfigurationException) AccessibleObject(java.lang.reflect.AccessibleObject)

Aggregations

Annotation (java.lang.annotation.Annotation)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 ConfigurationException (org.apache.logging.log4j.core.config.ConfigurationException)2 PluginAliases (org.apache.logging.log4j.core.config.plugins.PluginAliases)2 ConstraintValidator (org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator)2 Field (java.lang.reflect.Field)1