Search in sources :

Example 1 with ConfigurationMetadata

use of org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata in project sling by apache.

the class ConfigurationManagerImpl method getNestedConfigurationMetadata.

private ConfigurationMetadata getNestedConfigurationMetadata(ConfigurationMetadata configMetadata, String configName, String partialConfigName) {
    if (StringUtils.startsWith(configName, partialConfigName + "/")) {
        // depending on different persistence strategies config names can be transformed differently - try all combinations here
        Set<String> prefixesToRemove = new LinkedHashSet<>();
        if (configMetadata.isCollection()) {
            String collectionItemName = StringUtils.substringBefore(StringUtils.substringAfter(configName, partialConfigName + "/"), "/");
            for (String collectionParentConfigName : configurationPersistenceStrategy.getAllCollectionParentConfigNames(partialConfigName)) {
                for (String collectionItemConfigName : configurationPersistenceStrategy.getAllCollectionItemConfigNames(collectionItemName)) {
                    prefixesToRemove.add(collectionParentConfigName + "/" + collectionItemConfigName + "/");
                }
            }
        } else {
            for (String configNameItem : configurationPersistenceStrategy.getAllConfigNames(partialConfigName)) {
                prefixesToRemove.add(configNameItem + "/");
            }
        }
        for (String prefixToRemove : prefixesToRemove) {
            String remainingConfigName = StringUtils.substringAfter(configName, prefixToRemove);
            // try direct match
            ConfigurationMetadata nestedConfigMetadata = getNestedConfigurationMetadataFromProperty(configMetadata, remainingConfigName);
            if (nestedConfigMetadata != null) {
                return nestedConfigMetadata;
            }
            // try to find partial match for deeper nestings
            for (String partialRemainingConfigName : ConfigNameUtil.getAllPartialConfigNameVariations(remainingConfigName)) {
                ConfigurationMetadata partialConfigMetadata = getNestedConfigurationMetadataFromProperty(configMetadata, partialRemainingConfigName);
                if (partialConfigMetadata != null) {
                    nestedConfigMetadata = getNestedConfigurationMetadata(partialConfigMetadata, remainingConfigName, partialRemainingConfigName);
                    if (nestedConfigMetadata != null) {
                        return nestedConfigMetadata;
                    }
                }
            }
        }
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)

Example 2 with ConfigurationMetadata

use of org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata in project sling by apache.

the class AnnotationClassParser method buildPropertyMetadata.

@SuppressWarnings("unchecked")
private static <T> PropertyMetadata<T> buildPropertyMetadata(Method propertyMethod, Class<T> type) {
    String propertyName = getPropertyName(propertyMethod.getName());
    PropertyMetadata<?> propertyMetadata;
    if (type.isArray() && type.getComponentType().isAnnotation()) {
        ConfigurationMetadata nestedConfigMetadata = buildConfigurationMetadata_Nested(type.getComponentType(), propertyName, true);
        propertyMetadata = new PropertyMetadata<>(propertyName, ConfigurationMetadata[].class).configurationMetadata(nestedConfigMetadata);
    } else if (type.isAnnotation()) {
        ConfigurationMetadata nestedConfigMetadata = buildConfigurationMetadata_Nested(type, propertyName, false);
        propertyMetadata = new PropertyMetadata<>(propertyName, ConfigurationMetadata.class).configurationMetadata(nestedConfigMetadata);
    } else {
        propertyMetadata = new PropertyMetadata<>(propertyName, type).defaultValue((T) propertyMethod.getDefaultValue());
    }
    Property propertyAnnotation = propertyMethod.getAnnotation(Property.class);
    if (propertyAnnotation != null) {
        propertyMetadata.label(emptyToNull(propertyAnnotation.label())).description(emptyToNull(propertyAnnotation.description())).properties(propsArrayToMap(propertyAnnotation.property())).order(propertyAnnotation.order());
    } else {
        Map<String, String> emptyMap = Collections.emptyMap();
        propertyMetadata.properties(emptyMap);
    }
    return (PropertyMetadata) propertyMetadata;
}
Also used : PropertyMetadata(org.apache.sling.caconfig.spi.metadata.PropertyMetadata) Property(org.apache.sling.caconfig.annotation.Property) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)

Example 3 with ConfigurationMetadata

use of org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata in project sling by apache.

the class AnnotationClassParser method buildConfigurationMetadata.

/**
     * Build configuration metadata by parsing the given annotation interface class and it's configuration annotations.
     * @param clazz Configuration annotation class
     * @return Configuration metadata
     */
public static ConfigurationMetadata buildConfigurationMetadata(Class<?> clazz) {
    Configuration configAnnotation = clazz.getAnnotation(Configuration.class);
    if (configAnnotation == null) {
        throw new IllegalArgumentException("Class has not @Configuration annotation: " + clazz.getName());
    }
    // configuration metadata and property metadata
    String configName = getConfigurationName(clazz, configAnnotation);
    ConfigurationMetadata configMetadata = new ConfigurationMetadata(configName, buildConfigurationMetadata_PropertyMetadata(clazz), configAnnotation.collection()).label(emptyToNull(configAnnotation.label())).description(emptyToNull(configAnnotation.description())).properties(propsArrayToMap(configAnnotation.property()));
    return configMetadata;
}
Also used : Configuration(org.apache.sling.caconfig.annotation.Configuration) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)

Example 4 with ConfigurationMetadata

use of org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata in project sling by apache.

the class ConfigurationBindingsValueProviderTest method setUp.

@Before
public void setUp() {
    context.registerInjectActivateService(new ConfigurationMetadataProviderMultiplexerImpl());
    context.registerService(ConfigurationMetadataProvider.class, configMetadataProvider);
    when(configMetadataProvider.getConfigurationNames()).thenReturn(CONFIG_NAMES);
    when(bindings.containsKey(SlingBindings.REQUEST)).thenReturn(true);
    when(bindings.get(SlingBindings.REQUEST)).thenReturn(request);
    when(request.getResource()).thenReturn(resource);
    when(resource.adaptTo(ConfigurationBuilder.class)).thenReturn(configBuilder);
    when(configBuilder.name(anyString())).thenReturn(configBuilder);
    when(configBuilder.asValueMap()).thenReturn(VALUEMAP);
    when(configBuilder.asValueMapCollection()).thenReturn(ImmutableList.of(VALUEMAP));
    when(configMetadataProvider.getConfigurationMetadata("name1")).thenReturn(new ConfigurationMetadata("name1", ImmutableList.<PropertyMetadata<?>>of(), false));
    when(configMetadataProvider.getConfigurationMetadata("name.2")).thenReturn(new ConfigurationMetadata("name.2", ImmutableList.<PropertyMetadata<?>>of(), true));
}
Also used : ConfigurationMetadataProviderMultiplexerImpl(org.apache.sling.caconfig.impl.metadata.ConfigurationMetadataProviderMultiplexerImpl) PropertyMetadata(org.apache.sling.caconfig.spi.metadata.PropertyMetadata) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata) Before(org.junit.Before)

Example 5 with ConfigurationMetadata

use of org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata in project sling by apache.

the class ConfigurationManagerImpl method getConfiguration.

@SuppressWarnings("unchecked")
@Override
public ConfigurationData getConfiguration(Resource resource, String configName) {
    ConfigNameUtil.ensureValidConfigName(configName);
    if (log.isDebugEnabled()) {
        log.debug("Get configuration for context path {}, name '{}'", resource.getPath(), configName);
    }
    ConfigurationMetadata configMetadata = getConfigurationMetadata(configName);
    Resource configResource = null;
    Iterator<Resource> configResourceInheritanceChain = configurationResourceResolvingStrategy.getResourceInheritanceChain(resource, configurationResourceResolverConfig.configBucketNames(), configName);
    if (configResourceInheritanceChain != null) {
        ResettableIterator resettableConfigResourceInheritanceChain = new ListIteratorWrapper(configResourceInheritanceChain);
        configResource = applyPersistenceAndInheritance(resource.getPath(), configName, resettableConfigResourceInheritanceChain, false);
        if (configResource != null) {
            // get writeback resource for "reverse inheritance detection"
            Resource writebackConfigResource = null;
            String writebackConfigResourcePath = null;
            for (String configBucketName : configurationResourceResolverConfig.configBucketNames()) {
                writebackConfigResourcePath = configurationResourceResolvingStrategy.getResourcePath(resource, configBucketName, configName);
                if (writebackConfigResourcePath != null) {
                    writebackConfigResource = resource.getResourceResolver().getResource(writebackConfigResourcePath);
                    if (writebackConfigResource != null) {
                        writebackConfigResource = configurationPersistenceStrategy.getResource(writebackConfigResource);
                        break;
                    }
                }
            }
            if (log.isTraceEnabled() && configResource != null) {
                log.trace("+ Found config resource for context path " + resource.getPath() + ": " + configResource.getPath() + " " + MapUtil.traceOutput(configResource.getValueMap()) + ", " + "writeback config resource: " + writebackConfigResourcePath);
            }
            resettableConfigResourceInheritanceChain.reset();
            return new ConfigurationDataImpl(configMetadata, configResource, writebackConfigResource, applyPersistence(resettableConfigResourceInheritanceChain, false), resource, configName, this, configurationManagementSettings, configurationOverrideMultiplexer, configurationPersistenceStrategy, false, null);
        }
    }
    if (configMetadata != null) {
        // if no config resource found but config metadata exist return empty config data with default values
        return new ConfigurationDataImpl(configMetadata, resource, configName, this, configurationManagementSettings, configurationOverrideMultiplexer, configurationPersistenceStrategy, false);
    }
    return null;
}
Also used : ListIteratorWrapper(org.apache.commons.collections.iterators.ListIteratorWrapper) Resource(org.apache.sling.api.resource.Resource) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata) ResettableIterator(org.apache.commons.collections.ResettableIterator)

Aggregations

ConfigurationMetadata (org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)33 Test (org.junit.Test)17 PropertyMetadata (org.apache.sling.caconfig.spi.metadata.PropertyMetadata)9 Before (org.junit.Before)5 AnnotationClassParser.buildConfigurationMetadata (org.apache.sling.caconfig.impl.metadata.AnnotationClassParser.buildConfigurationMetadata)4 ConfigurationData (org.apache.sling.caconfig.management.ConfigurationData)4 ConfigurationMetadataProvider (org.apache.sling.caconfig.spi.ConfigurationMetadataProvider)3 HashMap (java.util.HashMap)2 ResettableIterator (org.apache.commons.collections.ResettableIterator)2 ListIteratorWrapper (org.apache.commons.collections.iterators.ListIteratorWrapper)2 Resource (org.apache.sling.api.resource.Resource)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1 SortedSet (java.util.SortedSet)1 Configuration (org.apache.sling.caconfig.annotation.Configuration)1 Property (org.apache.sling.caconfig.annotation.Property)1 AllTypesConfig (org.apache.sling.caconfig.example.AllTypesConfig)1