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;
}
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;
}
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;
}
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));
}
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;
}
Aggregations