Search in sources :

Example 31 with ConfigurationData

use of org.apache.sling.caconfig.management.ConfigurationData in project sling by apache.

the class ConfigurationManagerImplTest method testPersistConfiguration_Nested.

@Test
public void testPersistConfiguration_Nested() throws Exception {
    underTest.persistConfiguration(contextResourceLevel2, getConfigResolvePath(getConfigResolvePath(CONFIG_NESTED_NAME) + "/propSub") + "/propSubLevel2", new ConfigurationPersistData(ImmutableMap.<String, Object>of("prop1", "value1_persist")));
    context.resourceResolver().commit();
    ConfigurationData configData = underTest.getConfiguration(contextResourceLevel2, CONFIG_NESTED_NAME);
    ConfigurationData subData = configData.getValues().get("propSub", ConfigurationData.class);
    ConfigurationData subDataLevel2 = subData.getValues().get("propSubLevel2", ConfigurationData.class);
    assertEquals("value1_persist", subDataLevel2.getValues().get("prop1", String.class));
    assertEquals("value1_persist", subDataLevel2.getEffectiveValues().get("prop1", String.class));
    assertFalse(subDataLevel2.getValues().get("prop4", false));
    assertFalse(subDataLevel2.getEffectiveValues().get("prop4", false));
}
Also used : ConfigurationPersistData(org.apache.sling.caconfig.spi.ConfigurationPersistData) ConfigurationData(org.apache.sling.caconfig.management.ConfigurationData) Test(org.junit.Test)

Example 32 with ConfigurationData

use of org.apache.sling.caconfig.management.ConfigurationData in project sling by apache.

the class ConfigurationManagerImplTest method testGetConfiguration_NoConfigMetadata.

@Test
public void testGetConfiguration_NoConfigMetadata() {
    when(configurationMetadataProvider.getConfigurationMetadata(CONFIG_NAME)).thenReturn(null);
    ConfigurationData configData = underTest.getConfiguration(contextResource, CONFIG_NAME);
    assertNotNull(configData);
    assertFalse(configData.isInherited());
    assertEquals(ImmutableSet.of("prop1", "prop4"), configData.getPropertyNames());
    assertEquals("value1", configData.getValues().get("prop1", String.class));
    assertEquals((Integer) 0, configData.getEffectiveValues().get("prop3", 0));
    assertFalse(configData.getValueInfo("prop1").isInherited());
    assertFalse(configData.getValueInfo("prop3").isInherited());
}
Also used : ConfigurationData(org.apache.sling.caconfig.management.ConfigurationData) Test(org.junit.Test)

Example 33 with ConfigurationData

use of org.apache.sling.caconfig.management.ConfigurationData in project sling by apache.

the class ConfigurationDataImpl method resolveNestedConfigs.

private void resolveNestedConfigs(Map<String, Object> props) {
    if (configMetadata == null) {
        return;
    }
    for (PropertyMetadata<?> propertyMetadata : configMetadata.getPropertyMetadata().values()) {
        if (propertyMetadata.isNestedConfiguration()) {
            ConfigurationMetadata nestedConfigMetadata = propertyMetadata.getConfigurationMetadata();
            String relatedConfigPath = resolvedConfigurationResource != null ? resolvedConfigurationResource.getPath() : null;
            String nestedConfigName;
            if (configResourceCollection) {
                String collectionItemName = StringUtils.defaultString(getCollectionItemName(), "newItem");
                nestedConfigName = configurationPersistenceStrategy.getCollectionParentConfigName(configName, relatedConfigPath) + "/" + configurationPersistenceStrategy.getCollectionItemConfigName(collectionItemName, relatedConfigPath) + "/" + nestedConfigMetadata.getName();
            } else {
                nestedConfigName = configurationPersistenceStrategy.getConfigName(configName, relatedConfigPath) + "/" + nestedConfigMetadata.getName();
            }
            if (propertyMetadata.getType().equals(ConfigurationMetadata.class)) {
                ConfigurationData configData = configurationManager.getConfiguration(contextResource, nestedConfigName);
                props.put(propertyMetadata.getName(), configData);
            } else if (propertyMetadata.getType().equals(ConfigurationMetadata[].class)) {
                Collection<ConfigurationData> configDatas = configurationManager.getConfigurationCollection(contextResource, nestedConfigName).getItems();
                props.put(propertyMetadata.getName(), configDatas.toArray(new ConfigurationData[configDatas.size()]));
            }
        }
    }
}
Also used : ConfigurationData(org.apache.sling.caconfig.management.ConfigurationData) Collection(java.util.Collection) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)

Example 34 with ConfigurationData

use of org.apache.sling.caconfig.management.ConfigurationData in project sling by apache.

the class ConfigurationManagerImpl method getConfigurationCollection.

@SuppressWarnings("unchecked")
@Override
public ConfigurationCollectionData getConfigurationCollection(Resource resource, String configName) {
    ConfigNameUtil.ensureValidConfigName(configName);
    if (log.isDebugEnabled()) {
        log.debug("Get configuration collection for context path {}, name '{}'", resource.getPath(), configName);
    }
    ConfigurationMetadata configMetadata = getConfigurationMetadata(configName);
    List<ConfigurationData> configData = new ArrayList<>();
    // get all possible colection parent config names
    Collection<String> collectionParentConfigNames = configurationPersistenceStrategy.getAllCollectionParentConfigNames(configName);
    // get configuration resource items
    List<Iterator<Resource>> configResourceInheritanceChains = new ArrayList<>();
    for (String collectionParentConfigName : collectionParentConfigNames) {
        Collection<Iterator<Resource>> result = configurationResourceResolvingStrategy.getResourceCollectionInheritanceChain(resource, configurationResourceResolverConfig.configBucketNames(), collectionParentConfigName);
        if (result != null) {
            configResourceInheritanceChains.addAll(result);
        }
    }
    String writebackConfigResourceCollectionParentPath = null;
    if (configResourceInheritanceChains != null) {
        for (Iterator<Resource> configResourceInheritanceChain : configResourceInheritanceChains) {
            ResettableIterator resettableConfigResourceInheritanceChain = new ListIteratorWrapper(configResourceInheritanceChain);
            Resource configResource = applyPersistenceAndInheritance(resource.getPath(), configName, resettableConfigResourceInheritanceChain, true);
            resettableConfigResourceInheritanceChain.reset();
            Resource untransformedConfigResource = (Resource) resettableConfigResourceInheritanceChain.next();
            if (configResource != null) {
                // get writeback resource for "reverse inheritance detection"
                Resource writebackConfigResource = null;
                String writebackConfigResourcePath = null;
                for (String configBucketName : configurationResourceResolverConfig.configBucketNames()) {
                    writebackConfigResourceCollectionParentPath = configurationResourceResolvingStrategy.getResourceCollectionParentPath(resource, configBucketName, configName);
                    if (writebackConfigResourceCollectionParentPath != null) {
                        writebackConfigResourceCollectionParentPath = configurationPersistenceStrategy.getCollectionParentResourcePath(writebackConfigResourceCollectionParentPath);
                        writebackConfigResourcePath = writebackConfigResourceCollectionParentPath + "/" + untransformedConfigResource.getName();
                        writebackConfigResource = configResource.getResourceResolver().getResource(writebackConfigResourcePath);
                        if (writebackConfigResource != null) {
                            writebackConfigResource = configurationPersistenceStrategy.getCollectionItemResource(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();
                configData.add(new ConfigurationDataImpl(configMetadata, configResource, writebackConfigResource, applyPersistence(resettableConfigResourceInheritanceChain, true), resource, configName, this, configurationManagementSettings, configurationOverrideMultiplexer, configurationPersistenceStrategy, true, untransformedConfigResource.getName()));
            }
        }
    }
    // fallback for writeback path detection when no configuration resources does exist yet
    if (writebackConfigResourceCollectionParentPath == null) {
        for (String configBucketName : configurationResourceResolverConfig.configBucketNames()) {
            writebackConfigResourceCollectionParentPath = configurationResourceResolvingStrategy.getResourceCollectionParentPath(resource, configBucketName, configName);
            if (writebackConfigResourceCollectionParentPath != null) {
                break;
            }
        }
    }
    // get properties of parent resource of the current level
    Map<String, Object> resourceCollectionParentProps = null;
    if (writebackConfigResourceCollectionParentPath != null) {
        Resource writebackConfigResourceCollectionParent = resource.getResourceResolver().getResource(writebackConfigResourceCollectionParentPath);
        if (writebackConfigResourceCollectionParent != null) {
            resourceCollectionParentProps = writebackConfigResourceCollectionParent.getValueMap();
        }
    }
    return new ConfigurationCollectionDataImpl(configName, configData, writebackConfigResourceCollectionParentPath, resourceCollectionParentProps, configurationManagementSettings);
}
Also used : ListIteratorWrapper(org.apache.commons.collections.iterators.ListIteratorWrapper) ArrayList(java.util.ArrayList) ConfigurationData(org.apache.sling.caconfig.management.ConfigurationData) Resource(org.apache.sling.api.resource.Resource) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata) ResettableIterator(org.apache.commons.collections.ResettableIterator) ResettableIterator(org.apache.commons.collections.ResettableIterator) Iterator(java.util.Iterator)

Aggregations

ConfigurationData (org.apache.sling.caconfig.management.ConfigurationData)34 Test (org.junit.Test)31 ValueMap (org.apache.sling.api.resource.ValueMap)5 ConfigurationCollectionData (org.apache.sling.caconfig.management.ConfigurationCollectionData)4 ConfigurationMetadata (org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)4 Resource (org.apache.sling.api.resource.Resource)3 DummyConfigurationOverrideProvider (org.apache.sling.caconfig.impl.override.DummyConfigurationOverrideProvider)2 ConfigurationPersistData (org.apache.sling.caconfig.spi.ConfigurationPersistData)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 ResettableIterator (org.apache.commons.collections.ResettableIterator)1 ListIteratorWrapper (org.apache.commons.collections.iterators.ListIteratorWrapper)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 ContextResource (org.apache.sling.caconfig.resource.spi.ContextResource)1 ConfigurationCollectionPersistData (org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData)1