Search in sources :

Example 1 with PropertyMetadata

use of org.apache.sling.caconfig.spi.metadata.PropertyMetadata 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 2 with PropertyMetadata

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

the class AnnotationClassParser method buildConfigurationMetadata_PropertyMetadata.

private static Collection<PropertyMetadata<?>> buildConfigurationMetadata_PropertyMetadata(Class<?> clazz) {
    // sort properties by order number, or alternatively by label, name
    SortedSet<PropertyMetadata<?>> propertyMetadataSet = new TreeSet<>(new Comparator<PropertyMetadata<?>>() {

        @Override
        public int compare(PropertyMetadata<?> o1, PropertyMetadata<?> o2) {
            int compare = Integer.compare(o1.getOrder(), o2.getOrder());
            if (compare == 0) {
                String sort1 = StringUtils.defaultString(o1.getLabel(), o1.getName());
                String sort2 = StringUtils.defaultString(o2.getLabel(), o2.getName());
                compare = sort1.compareTo(sort2);
            }
            return compare;
        }
    });
    Method[] propertyMethods = clazz.getDeclaredMethods();
    for (Method propertyMethod : propertyMethods) {
        PropertyMetadata<?> propertyMetadata = buildPropertyMetadata(propertyMethod, propertyMethod.getReturnType());
        propertyMetadataSet.add(propertyMetadata);
    }
    return propertyMetadataSet;
}
Also used : TreeSet(java.util.TreeSet) PropertyMetadata(org.apache.sling.caconfig.spi.metadata.PropertyMetadata) Method(java.lang.reflect.Method)

Example 3 with PropertyMetadata

use of org.apache.sling.caconfig.spi.metadata.PropertyMetadata 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 4 with PropertyMetadata

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

the class AnnotationClassParserTest method testBuildConfigurationMetadata_Nested.

@Test
public void testBuildConfigurationMetadata_Nested() {
    ConfigurationMetadata metadata = buildConfigurationMetadata(NestedConfig.class);
    assertEquals(NestedConfig.class.getName(), metadata.getName());
    Collection<PropertyMetadata<?>> propertyMetadataList = metadata.getPropertyMetadata().values();
    assertEquals(4, propertyMetadataList.size());
    for (PropertyMetadata<?> propertyMetadata : propertyMetadataList) {
        if (StringUtils.equals(propertyMetadata.getName(), "stringParam")) {
            assertEquals(String.class, propertyMetadata.getType());
        } else if (StringUtils.equals(propertyMetadata.getName(), "subConfig")) {
            assertEquals(ConfigurationMetadata.class, propertyMetadata.getType());
            ConfigurationMetadata subConfigMetadata = propertyMetadata.getConfigurationMetadata();
            assertEquals("subConfig", subConfigMetadata.getName());
            assertEquals(3, subConfigMetadata.getPropertyMetadata().size());
        } else if (StringUtils.equals(propertyMetadata.getName(), "subListConfig")) {
            assertEquals(ConfigurationMetadata[].class, propertyMetadata.getType());
            ConfigurationMetadata subListConfigMetadata = propertyMetadata.getConfigurationMetadata();
            assertEquals("subListConfig", subListConfigMetadata.getName());
            assertEquals(2, subListConfigMetadata.getPropertyMetadata().size());
        } else if (StringUtils.equals(propertyMetadata.getName(), "subConfigWithoutAnnotation")) {
            assertEquals(ConfigurationMetadata.class, propertyMetadata.getType());
            ConfigurationMetadata subConfigWithoutAnnotationMetadata = propertyMetadata.getConfigurationMetadata();
            assertEquals("subConfigWithoutAnnotation", subConfigWithoutAnnotationMetadata.getName());
            assertEquals(1, subConfigWithoutAnnotationMetadata.getPropertyMetadata().size());
        } else {
            fail("Unexpected property name: " + propertyMetadata.getName());
        }
    }
}
Also used : PropertyMetadata(org.apache.sling.caconfig.spi.metadata.PropertyMetadata) NestedConfig(org.apache.sling.caconfig.example.NestedConfig) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata) AnnotationClassParser.buildConfigurationMetadata(org.apache.sling.caconfig.impl.metadata.AnnotationClassParser.buildConfigurationMetadata) Test(org.junit.Test)

Example 5 with PropertyMetadata

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

the class AnnotationClassParserTest method testBuildConfigurationMetadata_Simple.

@Test
public void testBuildConfigurationMetadata_Simple() {
    ConfigurationMetadata metadata = buildConfigurationMetadata(MetadataSimpleConfig.class);
    assertEquals("simpleConfig", metadata.getName());
    assertEquals("Simple configuration", metadata.getLabel());
    assertEquals("This is a configuration example with additional metadata.", metadata.getDescription());
    assertEquals(ImmutableMap.of("param1", "value1", "param2", "123"), metadata.getProperties());
    assertFalse(metadata.isCollection());
    List<PropertyMetadata<?>> propertyMetadataList = ImmutableList.copyOf(metadata.getPropertyMetadata().values());
    assertEquals(3, propertyMetadataList.size());
    PropertyMetadata<?> stringParam = propertyMetadataList.get(0);
    assertEquals("String Param", stringParam.getLabel());
    assertEquals("Enter strings here.", stringParam.getDescription());
    assertEquals(ImmutableMap.of("p1", "v1"), stringParam.getProperties());
    assertNull(stringParam.getDefaultValue());
    PropertyMetadata<?> intParam = propertyMetadataList.get(1);
    assertEquals("Integer Param", intParam.getLabel());
    assertNull(intParam.getDescription());
    assertTrue(intParam.getProperties().isEmpty());
    assertEquals(5, intParam.getDefaultValue());
    PropertyMetadata<?> boolParam = propertyMetadataList.get(2);
    assertNull(boolParam.getLabel());
    assertNull(boolParam.getDescription());
    assertTrue(boolParam.getProperties().isEmpty());
    assertNull(boolParam.getDefaultValue());
}
Also used : PropertyMetadata(org.apache.sling.caconfig.spi.metadata.PropertyMetadata) ConfigurationMetadata(org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata) AnnotationClassParser.buildConfigurationMetadata(org.apache.sling.caconfig.impl.metadata.AnnotationClassParser.buildConfigurationMetadata) Test(org.junit.Test)

Aggregations

PropertyMetadata (org.apache.sling.caconfig.spi.metadata.PropertyMetadata)10 ConfigurationMetadata (org.apache.sling.caconfig.spi.metadata.ConfigurationMetadata)9 Before (org.junit.Before)5 AnnotationClassParser.buildConfigurationMetadata (org.apache.sling.caconfig.impl.metadata.AnnotationClassParser.buildConfigurationMetadata)2 Test (org.junit.Test)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 Property (org.apache.sling.caconfig.annotation.Property)1 NestedConfig (org.apache.sling.caconfig.example.NestedConfig)1 DefaultConfigurationPersistenceStrategy (org.apache.sling.caconfig.impl.def.DefaultConfigurationPersistenceStrategy)1 ConfigurationMetadataProviderMultiplexerImpl (org.apache.sling.caconfig.impl.metadata.ConfigurationMetadataProviderMultiplexerImpl)1 ConfigurationMetadataProvider (org.apache.sling.caconfig.spi.ConfigurationMetadataProvider)1