Java Code Examples for org.apache.deltaspike.core.api.config.ConfigResolver#TypedResolver

The following examples show how to use org.apache.deltaspike.core.api.config.ConfigResolver#TypedResolver . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseConfigPropertyProducer.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public <T> ConfigResolver.TypedResolver<T> asResolver(final String key, final String stringDefault,
                                                      final Type ipCls,
                                                      final Class<? extends ConfigResolver.Converter> converterType,
                                                      final String parameterizedBy,
                                                      final boolean projectStageAware, final boolean evaluate)
{
    final ConfigResolver.UntypedResolver<String> untypedResolver = ConfigResolver.resolve(key);
    final ConfigResolver.TypedResolver<T> resolver =
            (ConfigResolver.Converter.class == converterType ?
                    untypedResolver.as(Class.class.cast(ipCls)) :
                    untypedResolver.as(ipCls, BeanProvider.getContextualReference(converterType)))
                    .withCurrentProjectStage(projectStageAware);
    if (!ConfigProperty.NULL.equals(stringDefault))
    {
        resolver.withStringDefault(stringDefault);
    }
    if (!ConfigProperty.NULL.equals(parameterizedBy))
    {
        resolver.parameterizedBy(parameterizedBy);
    }
    return resolver.evaluateVariables(evaluate);
}
 
Example 2
Source File: ConfigResolverTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void testTypedResolver_NonExistingValue()
{
    final String key = "non.existing.key";

    ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve(key)
        .logChanges(true);

    Assert.assertNull(resolver.getValue());

    setTestConfigSourceValue(key, "somevalue");
    Assert.assertEquals("somevalue", resolver.getValue());

    setTestConfigSourceValue(key, null);
    Assert.assertNull(resolver.getValue());
}
 
Example 3
Source File: TypedResolverTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCacheTime() throws Exception
{
    ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve("dataSource")
        .withCurrentProjectStage(true)
        .parameterizedBy("dbvendor")
        .cacheFor(TimeUnit.MILLISECONDS, 5)
        .withDefault("TESTDEFAULT");

    Assert.assertEquals("TestDataSource", resolver.getValue());
    Assert.assertEquals("TestDataSource", resolver.getValue());
    Assert.assertEquals("dataSource", resolver.getKey());
    Assert.assertEquals("TESTDEFAULT", resolver.getDefaultValue());
    Assert.assertEquals("dataSource.mysql.UnitTest", resolver.getResolvedKey());

    // because the clock steps in certain OS is only 16ms
    Thread.sleep(35L);
    Assert.assertEquals("TestDataSource", resolver.getValue());
}
 
Example 4
Source File: TypedResolverTest.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Test
public void testGets()
{
    ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve("dataSource")
            .withCurrentProjectStage(true)
            .parameterizedBy("dbvendor")
            .withDefault("TESTDEFAULT");

    Assert.assertEquals("TestDataSource", resolver.getValue());
    Assert.assertEquals("dataSource", resolver.getKey());
    Assert.assertEquals("TESTDEFAULT", resolver.getDefaultValue());
    Assert.assertEquals("dataSource.mysql.UnitTest", resolver.getResolvedKey());


    ConfigResolver.TypedResolver<String> resolver2 = ConfigResolver.resolve("testkey2")
            .withCurrentProjectStage(true)
            .parameterizedBy("INVALIDPARAMETER");


    Assert.assertEquals("testvalue", resolver2.getValue());
    Assert.assertEquals("testkey2", resolver2.getResolvedKey());
}
 
Example 5
Source File: ConfigImpl.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public ConfigSnapshot snapshotFor(ConfigResolver.TypedResolver<?>... typedResolvers)
{
    // we implement kind of optimistic Locking
    // Means we try multiple time to resolve all the given values
    // until the config didn't change inbetween.
    for (int tries = 1; tries < 5; tries++)
    {
        Map<ConfigResolver.TypedResolver<?>, Object> configValues = new HashMap<>();
        long startReadLastChanged = lastChanged;
        for (ConfigResolver.TypedResolver<?> typedResolver : typedResolvers)
        {
            configValues.put(typedResolver, typedResolver.getValue());
        }

        if (startReadLastChanged == lastChanged)
        {
            return new ConfigSnapshotImpl(configValues);
        }
    }

    throw new IllegalStateException(
            "Could not resolve ConfigTransaction as underlying values are permanently changing!");
}
 
Example 6
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public ConfigResolver.TypedResolver<T> parameterizedBy(String propertyName)
{
    this.propertyParameter = propertyName;

    if (propertyParameter != null && !propertyParameter.isEmpty())
    {
        String parameterValue = ConfigResolver
                .resolve(propertyParameter)
                .withCurrentProjectStage(projectStageAware)
                .getValue();

        if (parameterValue != null && !parameterValue.isEmpty())
        {
            this.parameterValue = parameterValue;
        }
    }

    return this;
}
 
Example 7
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
public ConfigResolver.TypedResolver<T> withStringDefault(String value)
{
    if (value == null || value.isEmpty())
    {
        throw new RuntimeException("Empty String or null supplied as string-default value for property "
                + keyOriginal);
    }

    if (isList)
    {
        defaultValue = splitAndConvertListValue(value);
    }
    else
    {
        defaultValue = convert(value);
    }
    withDefault = true;
    return this;
}
 
Example 8
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
public ConfigResolver.TypedResolver<T> withDefault(T value)
{
    defaultValue = value;
    withDefault = true;
    return this;
}
 
Example 9
Source File: ConfigResolverTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypedResolver_OnChange()
{
    final String key = "non.existing.key";

    final AtomicInteger valueChanged = new AtomicInteger(0);

    ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve(key)
        .logChanges(true)
        .onChange((k, oldValue, newValue) ->
        {
            Assert.assertEquals(key, k);
            valueChanged.incrementAndGet();
        });

    Assert.assertNull(resolver.getValue());

    setTestConfigSourceValue(key, "somevalue");
    Assert.assertEquals("somevalue", resolver.getValue());
    Assert.assertEquals(1, valueChanged.get());

    setTestConfigSourceValue(key, "newvalue");
    Assert.assertEquals("newvalue", resolver.getValue());
    Assert.assertEquals(2, valueChanged.get());

    // this time we do not change anything
    Assert.assertEquals("newvalue", resolver.getValue());
    Assert.assertEquals(2, valueChanged.get());

    // last change
    setTestConfigSourceValue(key, null);
    Assert.assertNull(resolver.getValue());
    Assert.assertEquals(3, valueChanged.get());
}
 
Example 10
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <N> ConfigResolver.TypedResolver<N> as(Type clazz, ConfigResolver.Converter<N> converter)
{
    configEntryType = clazz;
    this.converter = converter;

    return (ConfigResolver.TypedResolver<N>) this;
}
 
Example 11
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <N> ConfigResolver.TypedResolver<N> as(Class<N> clazz, ConfigResolver.Converter<N> converter)
{
    configEntryType = clazz;
    this.converter = converter;

    return (ConfigResolver.TypedResolver<N>) this;
}
 
Example 12
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ConfigResolver.TypedResolver<List<T>> asList()
{
    isList = true;
    ConfigResolver.TypedResolver<List<T>> listTypedResolver = (ConfigResolver.TypedResolver<List<T>>) this;

    if (defaultValue == null)
    {
        // the default for lists is an empty list instead of null
        return listTypedResolver.withDefault(Collections.<T>emptyList());
    }

    return listTypedResolver;
}
 
Example 13
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <N> ConfigResolver.TypedResolver<N> as(Class<N> clazz)
{
    configEntryType = clazz;
    return (ConfigResolver.TypedResolver<N>) this;
}
 
Example 14
Source File: ConfigResolverTest.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfigVariableRecursiveDeclaration()
{
    String url = ConfigResolver.getPropertyValue("deltaspike.test.recursive.variable1", "", true);
    Assert.assertEquals("pre-crazy-post/ohgosh/crazy", url);

    ConfigResolver.TypedResolver<String> tr = ConfigResolver.resolve("deltaspike.test.recursive.variable1")
        .evaluateVariables(true).logChanges(true);
    Assert.assertEquals("pre-crazy-post/ohgosh/crazy", tr.getValue());
}
 
Example 15
Source File: BaseConfigPropertyProducer.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
public <T> T readEntry(final String key, final String stringDefault, final Type ipCls,
                       final Class<? extends ConfigResolver.Converter> converterType,
                       final String parameterizedBy, final boolean projectStageAware, final boolean evaluate)
{
    final ConfigResolver.TypedResolver<T> resolver = asResolver(
            key, stringDefault, ipCls, converterType, parameterizedBy, projectStageAware, evaluate);
    return resolver.getValue();
}
 
Example 16
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public ConfigResolver.TypedResolver<T> withCurrentProjectStage(boolean with)
{
    this.projectStageAware = with;
    return this;
}
 
Example 17
Source File: TypedResolverImpl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public ConfigResolver.TypedResolver<T> onChange(ConfigResolver.ConfigChanged<T> valueChangedCallback)
{
    this.valueChangedCallback = valueChangedCallback;
    return this;
}
 
Example 18
Source File: ProxyConfigurationLifecycle.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
private DefaultSupplier(final ConfigResolver.TypedResolver<T> delegate)
{
    this.delegate = delegate;
}
 
Example 19
Source File: ConfigSnapshotImpl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
public ConfigSnapshotImpl(Map<ConfigResolver.TypedResolver<?>, Object> configValues)
{
    this.configValues = configValues;
}
 
Example 20
Source File: ConfigSnapshotImpl.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
public Map<ConfigResolver.TypedResolver<?>, Object> getConfigValues()
{
    return configValues;
}