Search in sources :

Example 1 with PropertyDefinition

use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.

the class DefaultDiscoveryService method buildProperties.

private Map<String, Comparable> buildProperties(DiscoveryStrategyFactory factory, DiscoveryStrategyConfig config, String className) {
    Collection<PropertyDefinition> propertyDefinitions = factory.getConfigurationProperties();
    if (propertyDefinitions == null) {
        return Collections.emptyMap();
    }
    Map<String, Comparable> properties = config.getProperties();
    Map<String, Comparable> mappedProperties = new HashMap<String, Comparable>();
    for (PropertyDefinition propertyDefinition : propertyDefinitions) {
        String propertyKey = propertyDefinition.key();
        Comparable value = properties.get(propertyKey);
        if (value == null) {
            if (!propertyDefinition.optional()) {
                throw new HazelcastException("Missing property '" + propertyKey + "' on discovery strategy '" + className + "' configuration");
            }
            continue;
        }
        TypeConverter typeConverter = propertyDefinition.typeConverter();
        Comparable mappedValue = typeConverter.convert(value);
        ValueValidator validator = propertyDefinition.validator();
        if (validator != null) {
            validator.validate(mappedValue);
        }
        mappedProperties.put(propertyKey, mappedValue);
    }
    return mappedProperties;
}
Also used : TypeConverter(com.hazelcast.core.TypeConverter) HazelcastException(com.hazelcast.core.HazelcastException) HashMap(java.util.HashMap) ValueValidator(com.hazelcast.config.properties.ValueValidator) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition)

Example 2 with PropertyDefinition

use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.

the class DiscoverySpiTest method test_AbstractDiscoveryStrategy_getOrNull.

@Test
public void test_AbstractDiscoveryStrategy_getOrNull() throws Exception {
    PropertyDefinition first = new SimplePropertyDefinition("first", PropertyTypeConverter.STRING);
    PropertyDefinition second = new SimplePropertyDefinition("second", BOOLEAN);
    PropertyDefinition third = new SimplePropertyDefinition("third", PropertyTypeConverter.INTEGER);
    PropertyDefinition fourth = new SimplePropertyDefinition("fourth", true, PropertyTypeConverter.STRING);
    PropertyDefinition onlyInEnv = new SimplePropertyDefinition("only_in_env", true, PropertyTypeConverter.INTEGER);
    Map<String, Comparable> properties = new HashMap<>();
    properties.put("first", "value-first");
    properties.put("second", Boolean.FALSE);
    properties.put("third", 100);
    Map<String, String> environmentVariables = new HashMap<>();
    // system property > system environment > configuration
    // property 'first' => "value-first"
    // property 'second' => true
    environmentVariables.put("test.second", "true");
    // property 'third' => 300
    environmentVariables.put("test.third", "200");
    System.setProperty("test.third", "300");
    // property 'fourth' => null
    environmentVariables.put("test.only_in_env", "500");
    PropertyDiscoveryStrategy strategy = new PropertyDiscoveryStrategy(LOGGER, properties, environmentVariables::get);
    // without lookup of environment
    assertEquals("value-first", strategy.getOrNull(first));
    assertEquals(Boolean.FALSE, strategy.getOrNull(second));
    assertEquals(100, ((Integer) strategy.getOrNull(third)).intValue());
    assertNull(strategy.getOrNull(fourth));
    assertEquals("value-first", strategy.getOrNull("test", first));
    assertEquals(Boolean.TRUE, strategy.getOrNull("test", second));
    assertEquals(300, ((Integer) strategy.getOrNull("test", third)).intValue());
    assertNull(strategy.getOrNull("test", fourth));
    assertEquals(500, ((Integer) strategy.getOrNull("test", onlyInEnv)).intValue());
}
Also used : SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) HashMap(java.util.HashMap) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 3 with PropertyDefinition

use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.

the class DiscoverySpiTest method test_AbstractDiscoveryStrategy_getOrDefault.

@Test
public void test_AbstractDiscoveryStrategy_getOrDefault() throws Exception {
    PropertyDefinition value = new SimplePropertyDefinition("value", PropertyTypeConverter.INTEGER);
    Map<String, Comparable> properties = Collections.emptyMap();
    PropertyDiscoveryStrategy strategy = new PropertyDiscoveryStrategy(LOGGER, properties);
    assertEquals(1111, (long) strategy.getOrDefault(value, 1111));
    assertEquals(1111, (long) strategy.getOrDefault("test", value, 1111));
}
Also used : SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 4 with PropertyDefinition

use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.

the class DiscoveryServicePropertiesUtilTest method nullProperty.

@Test
public void nullProperty() {
    // given
    Map<String, Comparable> properties = new HashMap<>(singletonMap(PROPERTY_KEY_1, null));
    TypeConverter typeConverter = new TypeConverter() {

        @Override
        public Comparable convert(Comparable value) {
            return value == null ? "hazel" : "cast";
        }
    };
    Collection<PropertyDefinition> propertyDefinitions = singletonList((PropertyDefinition) new SimplePropertyDefinition(PROPERTY_KEY_1, true, typeConverter));
    // when
    Map<String, Comparable> result = prepareProperties(properties, propertyDefinitions);
    // then
    assertEquals("hazel", result.get(PROPERTY_KEY_1));
}
Also used : TypeConverter(com.hazelcast.core.TypeConverter) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) HashMap(java.util.HashMap) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with PropertyDefinition

use of com.hazelcast.config.properties.PropertyDefinition in project hazelcast by hazelcast.

the class DiscoveryServicePropertiesUtilTest method correctDashlessPropertyConversion.

@Test
public void correctDashlessPropertyConversion() {
    // given
    Map<String, Comparable> properties = new HashMap<>(singletonMap("customproperty", PROPERTY_VALUE_1));
    Collection<PropertyDefinition> propertyDefinitions = singletonList(new SimplePropertyDefinition("custom-property", STRING));
    // when
    Map<String, Comparable> result = prepareProperties(properties, propertyDefinitions);
    // then
    assertEquals(PROPERTY_VALUE_1, result.get("custom-property"));
}
Also used : SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) HashMap(java.util.HashMap) SimplePropertyDefinition(com.hazelcast.config.properties.SimplePropertyDefinition) PropertyDefinition(com.hazelcast.config.properties.PropertyDefinition) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

PropertyDefinition (com.hazelcast.config.properties.PropertyDefinition)7 SimplePropertyDefinition (com.hazelcast.config.properties.SimplePropertyDefinition)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 TypeConverter (com.hazelcast.core.TypeConverter)3 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)3 ValueValidator (com.hazelcast.config.properties.ValueValidator)2 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 ValidationException (com.hazelcast.config.properties.ValidationException)1 HazelcastException (com.hazelcast.core.HazelcastException)1