Search in sources :

Example 1 with ListIteratorWrapper

use of org.apache.commons.collections.iterators.ListIteratorWrapper 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)

Example 2 with ListIteratorWrapper

use of org.apache.commons.collections.iterators.ListIteratorWrapper 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

ResettableIterator (org.apache.commons.collections.ResettableIterator)2 ListIteratorWrapper (org.apache.commons.collections.iterators.ListIteratorWrapper)2 Resource (org.apache.sling.api.resource.Resource)2 ConfigurationMetadata (org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 ConfigurationData (org.apache.sling.caconfig.management.ConfigurationData)1