Search in sources :

Example 1 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigurationMetadataTest method verifyMetaData.

private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, boolean securitySensitive, Map<String, Set<String>> attributeProperties) throws Exception {
    assertEquals(metadata.getConfigClass(), configClass);
    if (metadata.getConstructor() != null) {
        assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor());
    } else {
        try {
            configClass.getDeclaredConstructor();
            fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName()));
        } catch (NoSuchMethodException expected) {
        }
    }
    assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size());
    for (String name : attributeProperties.keySet()) {
        AttributeMetadata attribute = metadata.getAttributes().get(name);
        assertEquals(attribute.getConfigClass(), configClass);
        Set<String> namesToTest = new HashSet<>();
        namesToTest.add(attribute.getInjectionPoint().getProperty());
        for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) {
            namesToTest.add(legacyInjectionPoint.getProperty());
        }
        assertEquals(namesToTest, attributeProperties.get(name));
        assertEquals(attribute.getDescription(), description);
        assertEquals(attribute.isSecuritySensitive(), securitySensitive);
    }
}
Also used : AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) HashSet(java.util.HashSet)

Example 2 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigAssertions method assertPropertiesSupported.

private static void assertPropertiesSupported(ConfigurationMetadata<?> metadata, Set<String> propertyNames, boolean allowDeprecatedProperties) {
    Set<String> supportedProperties = new TreeSet<>();
    Set<String> nonDeprecatedProperties = new TreeSet<>();
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        if (attribute.getInjectionPoint().getProperty() != null) {
            nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty());
            supportedProperties.add(attribute.getInjectionPoint().getProperty());
        }
        for (ConfigurationMetadata.InjectionPointMetaData deprecated : attribute.getLegacyInjectionPoints()) {
            supportedProperties.add(deprecated.getProperty());
        }
    }
    if (!supportedProperties.containsAll(propertyNames)) {
        Set<String> unsupportedProperties = new TreeSet<>(propertyNames);
        unsupportedProperties.removeAll(supportedProperties);
        throw new AssertionError("Unsupported properties: " + unsupportedProperties);
    }
    // check for usage of deprecated properties
    if (!allowDeprecatedProperties && !nonDeprecatedProperties.containsAll(propertyNames)) {
        Set<String> deprecatedProperties = new TreeSet<>(propertyNames);
        deprecatedProperties.removeAll(nonDeprecatedProperties);
        throw new AssertionError("Deprecated properties: " + deprecatedProperties);
    }
}
Also used : TreeSet(java.util.TreeSet) AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) ConfigurationMetadata(io.airlift.configuration.ConfigurationMetadata)

Example 3 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigAssertions method assertDefaults.

public static <T> void assertDefaults(Map<String, Object> expectedAttributeValues, Class<T> configClass) {
    ConfigurationMetadata<?> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass);
    // verify all supplied attributes are supported
    if (!metadata.getAttributes().keySet().containsAll(expectedAttributeValues.keySet())) {
        Set<String> unsupportedAttributes = new TreeSet<>(expectedAttributeValues.keySet());
        unsupportedAttributes.removeAll(metadata.getAttributes().keySet());
        throw new AssertionError("Unsupported attributes: " + unsupportedAttributes);
    }
    // verify all supplied attributes are supported not deprecated
    Set<String> nonDeprecatedAttributes = new TreeSet<>();
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        if (attribute.getInjectionPoint().getProperty() != null) {
            nonDeprecatedAttributes.add(attribute.getName());
        }
    }
    if (!nonDeprecatedAttributes.containsAll(expectedAttributeValues.keySet())) {
        Set<String> unsupportedAttributes = new TreeSet<>(expectedAttributeValues.keySet());
        unsupportedAttributes.removeAll(nonDeprecatedAttributes);
        throw new AssertionError("Deprecated attributes: " + unsupportedAttributes);
    }
    // verify all attributes are tested
    if (!expectedAttributeValues.keySet().containsAll(nonDeprecatedAttributes)) {
        Set<String> untestedAttributes = new TreeSet<>(nonDeprecatedAttributes);
        untestedAttributes.removeAll(expectedAttributeValues.keySet());
        throw new AssertionError("Untested attributes: " + untestedAttributes);
    }
    // create an uninitialized default instance
    T actual = newDefaultInstance(configClass);
    // verify each attribute is either the supplied default value
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        Method getter = attribute.getGetter();
        if (getter == null) {
            continue;
        }
        Object actualAttributeValue = invoke(actual, getter);
        Object expectedAttributeValue = expectedAttributeValues.get(attribute.getName());
        if (!Objects.deepEquals(actualAttributeValue, expectedAttributeValue)) {
            throw new AssertionError(notEquals(attribute.getName(), actualAttributeValue, expectedAttributeValue));
        }
    }
}
Also used : TreeSet(java.util.TreeSet) AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) Method(java.lang.reflect.Method)

Example 4 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigAssertions method assertRecordedDefaults.

public static <T> void assertRecordedDefaults(T recordedConfig) {
    $$RecordedConfigData<T> recordedConfigData = getRecordedConfig(recordedConfig);
    Set<Method> invokedMethods = recordedConfigData.getInvokedMethods();
    T config = recordedConfigData.getInstance();
    Class<T> configClass = getClass(config);
    ConfigurationMetadata<?> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass);
    // collect information about the attributes that have been set
    Map<String, Object> attributeValues = new TreeMap<>();
    Set<String> setDeprecatedAttributes = new TreeSet<>();
    Set<Method> validSetterMethods = new HashSet<>();
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        if (attribute.getInjectionPoint().getProperty() != null) {
            validSetterMethods.add(attribute.getInjectionPoint().getSetter());
        }
        if (invokedMethods.contains(attribute.getInjectionPoint().getSetter())) {
            if (attribute.getInjectionPoint().getProperty() != null) {
                Object value = invoke(config, attribute.getGetter());
                attributeValues.put(attribute.getName(), value);
            } else {
                setDeprecatedAttributes.add(attribute.getName());
            }
        }
    }
    // verify no deprecated attribute setters have been called
    if (!setDeprecatedAttributes.isEmpty()) {
        throw new AssertionError("Invoked deprecated attribute setter methods: " + setDeprecatedAttributes);
    }
    // verify no other methods have been set
    if (!validSetterMethods.containsAll(invokedMethods)) {
        Set<Method> invalidInvocations = new HashSet<>(invokedMethods);
        invalidInvocations.removeAll(validSetterMethods);
        throw new AssertionError("Invoked non-attribute setter methods: " + invalidInvocations);
    }
    assertDefaults(attributeValues, configClass);
}
Also used : Method(java.lang.reflect.Method) TreeMap(java.util.TreeMap) TreeSet(java.util.TreeSet) AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) Sets.newConcurrentHashSet(com.google.common.collect.Sets.newConcurrentHashSet) HashSet(java.util.HashSet)

Example 5 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigurationFactory method build.

private <T> ConfigurationHolder<T> build(Class<T> configClass, Optional<String> configPrefix, ConfigDefaults<T> configDefaults) {
    if (configClass == null) {
        throw new NullPointerException("configClass is null");
    }
    String prefix = configPrefix.map(value -> value + ".").orElse("");
    ConfigurationMetadata<T> configurationMetadata = getMetadata(configClass);
    configurationMetadata.getProblems().throwIfHasErrors();
    T instance = newInstance(configurationMetadata);
    configDefaults.setDefaults(instance);
    Problems problems = new Problems(monitor);
    for (AttributeMetadata attribute : configurationMetadata.getAttributes().values()) {
        Problems attributeProblems = new Problems(monitor);
        try {
            setConfigProperty(instance, attribute, prefix, attributeProblems);
        } catch (InvalidConfigurationException e) {
            attributeProblems.addError(e.getCause(), e.getMessage());
        }
        problems.record(attributeProblems);
    }
    // Check that none of the defunct properties are still in use
    if (configClass.isAnnotationPresent(DefunctConfig.class)) {
        for (String value : configClass.getAnnotation(DefunctConfig.class).value()) {
            String name = prefix + value;
            if (!value.isEmpty() && properties.get(name) != null) {
                problems.addError("Defunct property '%s' (class [%s]) cannot be configured.", name, configClass.toString());
            }
        }
    }
    // if there already problems, don't run the bean validation as it typically reports duplicate errors
    problems.throwIfHasErrors();
    for (ConstraintViolation<?> violation : validate(instance)) {
        String propertyFieldName = violation.getPropertyPath().toString();
        // upper case first character to match config attribute name
        String attributeName = LOWER_CAMEL.to(UPPER_CAMEL, propertyFieldName);
        AttributeMetadata attribute = configurationMetadata.getAttributes().get(attributeName);
        if (attribute != null && attribute.getInjectionPoint() != null) {
            String propertyName = attribute.getInjectionPoint().getProperty();
            if (!prefix.isEmpty()) {
                propertyName = prefix + propertyName;
            }
            problems.addError("Invalid configuration property %s: %s (for class %s.%s)", propertyName, violation.getMessage(), configClass.getName(), violation.getPropertyPath());
        } else {
            problems.addError("Invalid configuration property with prefix '%s': %s (for class %s.%s)", prefix, violation.getMessage(), configClass.getName(), violation.getPropertyPath());
        }
    }
    problems.throwIfHasErrors();
    return new ConfigurationHolder<>(instance, problems);
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Module(com.google.inject.Module) LoadingCache(com.google.common.cache.LoadingCache) Provider(javax.inject.Provider) ListMultimap(com.google.common.collect.ListMultimap) Key(com.google.inject.Key) ProviderInstanceBinding(com.google.inject.spi.ProviderInstanceBinding) Message(com.google.inject.spi.Message) Map(java.util.Map) Splitter(com.google.common.base.Splitter) ConstraintViolation(javax.validation.ConstraintViolation) Method(java.lang.reflect.Method) ApacheValidationProvider(org.apache.bval.jsr.ApacheValidationProvider) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConfigurationException(com.google.inject.ConfigurationException) Validator(javax.validation.Validator) Set(java.util.Set) GuardedBy(javax.annotation.concurrent.GuardedBy) Binding(com.google.inject.Binding) String.format(java.lang.String.format) InvocationTargetException(java.lang.reflect.InvocationTargetException) CacheLoader(com.google.common.cache.CacheLoader) LOWER_CAMEL(com.google.common.base.CaseFormat.LOWER_CAMEL) List(java.util.List) ConfigurationMetadata.getConfigurationMetadata(io.airlift.configuration.ConfigurationMetadata.getConfigurationMetadata) Element(com.google.inject.spi.Element) Optional(java.util.Optional) InstanceBinding(com.google.inject.spi.InstanceBinding) CacheBuilder(com.google.common.cache.CacheBuilder) Problems.exceptionFor(io.airlift.configuration.Problems.exceptionFor) TypeToken(com.google.common.reflect.TypeToken) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) Multimaps(com.google.common.collect.Multimaps) DefaultElementVisitor(com.google.inject.spi.DefaultElementVisitor) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Validation(javax.validation.Validation) Nullable(javax.annotation.Nullable) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) Sets.newConcurrentHashSet(com.google.common.collect.Sets.newConcurrentHashSet) UPPER_CAMEL(com.google.common.base.CaseFormat.UPPER_CAMEL) Consumer(java.util.function.Consumer) AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Elements(com.google.inject.spi.Elements) AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata)

Aggregations

AttributeMetadata (io.airlift.configuration.ConfigurationMetadata.AttributeMetadata)10 Method (java.lang.reflect.Method)5 TreeSet (java.util.TreeSet)5 HashSet (java.util.HashSet)3 Sets.newConcurrentHashSet (com.google.common.collect.Sets.newConcurrentHashSet)2 ConfigurationMetadata (io.airlift.configuration.ConfigurationMetadata)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LOWER_CAMEL (com.google.common.base.CaseFormat.LOWER_CAMEL)1 UPPER_CAMEL (com.google.common.base.CaseFormat.UPPER_CAMEL)1 Splitter (com.google.common.base.Splitter)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 ListMultimap (com.google.common.collect.ListMultimap)1