Skip to content

Commit

Permalink
Introduced interface WithConfigPath and let KnownConfigValue exte…
Browse files Browse the repository at this point in the history
…nd this interface.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch-si.com>
  • Loading branch information
Juergen Fickel committed Feb 18, 2019
1 parent 3fa275c commit 2dbc420
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 72 deletions.
Expand Up @@ -36,7 +36,7 @@ private ClusterConfigValue(final String thePath, final Object theDefaultValue) {
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand Down Expand Up @@ -71,7 +71,7 @@ public static DefaultClusterConfig of(final Config config) {

@Override
public int getNumberOfShards() {
return config.getInt(ClusterConfigValue.NUMBER_OF_SHARDS.getPath());
return config.getInt(ClusterConfigValue.NUMBER_OF_SHARDS.getConfigPath());
}

@Override
Expand Down
Expand Up @@ -43,7 +43,7 @@ private HealthCheckConfigValue(final String thePath, final Object theDefaultValu
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand Down Expand Up @@ -78,22 +78,22 @@ public static DefaultHealthCheckConfig of(final Config config) {

@Override
public boolean isEnabled() {
return config.getBoolean(HealthCheckConfigValue.ENABLED.getPath());
return config.getBoolean(HealthCheckConfigValue.ENABLED.getConfigPath());
}

@Override
public Duration getInterval() {
return config.getDuration(HealthCheckConfigValue.INTERVAL.getPath());
return config.getDuration(HealthCheckConfigValue.INTERVAL.getConfigPath());
}

@Override
public boolean isPersistenceEnabled() {
return config.getBoolean(HealthCheckConfigValue.PERSISTENCE_ENABLED.getPath());
return config.getBoolean(HealthCheckConfigValue.PERSISTENCE_ENABLED.getConfigPath());
}

@Override
public Duration getPersistenceTimeout() {
return config.getDuration(HealthCheckConfigValue.PERSISTENCE_TIMEOUT.getPath());
return config.getDuration(HealthCheckConfigValue.PERSISTENCE_TIMEOUT.getConfigPath());
}

@Override
Expand Down
Expand Up @@ -41,7 +41,7 @@ private HttpConfigValue(final String thePath, final Object theDefaultValue) {
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand Down Expand Up @@ -75,12 +75,12 @@ public static DefaultHttpConfig of(final Config config) {

@Override
public String getHostname() {
return config.getString(HttpConfigValue.HOSTNAME.getPath());
return config.getString(HttpConfigValue.HOSTNAME.getConfigPath());
}

@Override
public int getPort() {
return config.getInt(HttpConfigValue.PORT.getPath());
return config.getInt(HttpConfigValue.PORT.getConfigPath());
}

@Override
Expand Down
Expand Up @@ -47,7 +47,7 @@ private LimitsConfigValue(final String thePath, final Object theDefaultValue) {
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand Down Expand Up @@ -89,27 +89,27 @@ public static DefaultLimitsConfig of(final Config config) {

@Override
public long getThingsMaxSize() {
return config.getBytes(LimitsConfigValue.THINGS_MAX_SIZE.getPath());
return config.getBytes(LimitsConfigValue.THINGS_MAX_SIZE.getConfigPath());
}

@Override
public long getPoliciesMaxSize() {
return config.getBytes(LimitsConfigValue.POLICIES_MAX_SIZE.getPath());
return config.getBytes(LimitsConfigValue.POLICIES_MAX_SIZE.getConfigPath());
}

@Override
public long getMessagesMaxSize() {
return config.getBytes(LimitsConfigValue.MESSAGES_MAX_SIZE.getPath());
return config.getBytes(LimitsConfigValue.MESSAGES_MAX_SIZE.getConfigPath());
}

@Override
public int getThingsSearchDefaultPageSize() {
return config.getInt(LimitsConfigValue.THINGS_SEARCH_DEFAULT_PAGE_SIZE.getPath());
return config.getInt(LimitsConfigValue.THINGS_SEARCH_DEFAULT_PAGE_SIZE.getConfigPath());
}

@Override
public int thingsSearchMaxPageSize() {
return config.getInt(LimitsConfigValue.THINGS_SEARCH_MAX_PAGE_SIZE.getPath());
return config.getInt(LimitsConfigValue.THINGS_SEARCH_MAX_PAGE_SIZE.getConfigPath());
}

@Override
Expand Down
Expand Up @@ -43,7 +43,7 @@ private MetricsConfigValue(final String thePath, final Object theDefaultValue) {
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand Down Expand Up @@ -78,22 +78,22 @@ public static DefaultMetricsConfig of(final Config config) {

@Override
public boolean isSystemMetricsEnabled() {
return config.getBoolean(MetricsConfigValue.SYSTEM_METRICS_ENABLED.getPath());
return config.getBoolean(MetricsConfigValue.SYSTEM_METRICS_ENABLED.getConfigPath());
}

@Override
public boolean isPrometheusEnabled() {
return config.getBoolean(MetricsConfigValue.PROMETHEUS_ENABLED.getPath());
return config.getBoolean(MetricsConfigValue.PROMETHEUS_ENABLED.getConfigPath());
}

@Override
public String getPrometheusHostname() {
return config.getString(MetricsConfigValue.PROMETHEUS_HOSTNAME.getPath());
return config.getString(MetricsConfigValue.PROMETHEUS_HOSTNAME.getConfigPath());
}

@Override
public int getPrometheusPort() {
return config.getInt(MetricsConfigValue.PROMETHEUS_PORT.getPath());
return config.getInt(MetricsConfigValue.PROMETHEUS_PORT.getConfigPath());
}

}
Expand Up @@ -15,13 +15,14 @@
import javax.annotation.concurrent.Immutable;

import org.eclipse.ditto.services.base.config.ServiceSpecificConfig;
import org.eclipse.ditto.services.utils.config.WithConfigPath;
import org.eclipse.ditto.services.utils.persistence.mongo.config.WithMongoDbConfig;

/**
* Provides the configuration settings of the Concierge service.
*/
@Immutable
public interface ConciergeConfig extends ServiceSpecificConfig, WithMongoDbConfig {
public interface ConciergeConfig extends ServiceSpecificConfig, WithConfigPath, WithMongoDbConfig {

/**
* Returns the config of Concierge's enforcement behaviour.
Expand Down
Expand Up @@ -41,7 +41,7 @@ private ConciergeCacheConfigValue(final String thePath, final Object theDefaultV
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand Down Expand Up @@ -75,12 +75,12 @@ public static DittoConciergeCacheConfig getInstance(final Config config, final S

@Override
public long getMaximumSize() {
return config.getLong(ConciergeCacheConfigValue.MAXIMUM_SIZE.getPath());
return config.getLong(ConciergeCacheConfigValue.MAXIMUM_SIZE.getConfigPath());
}

@Override
public Duration getExpireAfterWrite() {
return config.getDuration(ConciergeCacheConfigValue.EXPIRE_AFTER_WRITE.getPath());
return config.getDuration(ConciergeCacheConfigValue.EXPIRE_AFTER_WRITE.getConfigPath());
}

@Override
Expand Down
Expand Up @@ -177,6 +177,11 @@ public String toString() {
"]";
}

@Override
public String getConfigPath() {
return CONFIG_PATH;
}

/**
* This class implements {@link EnforcementConfig} for Ditto's Concierge service.
*/
Expand All @@ -196,7 +201,7 @@ private ConciergeEnforcementConfigValue(final String thePath, final Object theDe
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand All @@ -221,7 +226,7 @@ private DittoConciergeEnforcementConfig(final Config theConfig) {
* @param config is supposed to provide the settings of the enforcement config at {@value #CONFIG_PATH}.
* @return the instance.
* @throws NullPointerException if {@code config} is {@code null}.
* @throws com.typesafe.config.ConfigException.WrongType if {@code config} did not contain a nested
* @throws org.eclipse.ditto.services.utils.config.DittoConfigError if {@code config} did not contain a nested
* {@code Config} for {@value #CONFIG_PATH}.
*/
public static DittoConciergeEnforcementConfig of(final Config config) {
Expand All @@ -231,7 +236,7 @@ public static DittoConciergeEnforcementConfig of(final Config config) {

@Override
public Duration getAskTimeout() {
return config.getDuration(ConciergeEnforcementConfigValue.ASK_TIMEOUT.getPath());
return config.getDuration(ConciergeEnforcementConfigValue.ASK_TIMEOUT.getConfigPath());
}

@Override
Expand Down Expand Up @@ -279,7 +284,7 @@ private ConciergeCachesConfigValue(final String thePath, final Object theDefault
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand Down Expand Up @@ -308,7 +313,7 @@ private DittoConciergeCachesConfig(final Config theConfig) {
* @param config is supposed to provide the settings of the caches config at {@value #CONFIG_PATH}.
* @return the instance.
* @throws NullPointerException if {@code config} is {@code null}.
* @throws com.typesafe.config.ConfigException.WrongType if {@code config} did not contain a nested
* @throws org.eclipse.ditto.services.utils.config.DittoConfigError if {@code config} did not contain a nested
* {@code Config} for {@value #CONFIG_PATH}.
*/
public static DittoConciergeCachesConfig of(final Config config) {
Expand All @@ -318,7 +323,7 @@ public static DittoConciergeCachesConfig of(final Config config) {

@Override
public Duration getAskTimeout() {
return config.getDuration(ConciergeCachesConfigValue.ASK_TIMEOUT.getPath());
return config.getDuration(ConciergeCachesConfigValue.ASK_TIMEOUT.getConfigPath());
}

@Override
Expand Down Expand Up @@ -382,7 +387,7 @@ private ThingsAggregatorConfigValue(final String thePath, final Object theDefaul
}

@Override
public String getPath() {
public String getConfigPath() {
return path;
}

Expand All @@ -407,7 +412,7 @@ private DittoConciergeThingsAggregatorConfig(final Config theConfig) {
* @param config is supposed to provide the settings of the things aggregator config at {@value #CONFIG_PATH}.
* @return the instance.
* @throws NullPointerException if {@code config} is {@code null}.
* @throws com.typesafe.config.ConfigException.WrongType if {@code config} did not contain a nested
* @throws org.eclipse.ditto.services.utils.config.DittoConfigError if {@code config} did not contain a nested
* {@code Config} for {@value #CONFIG_PATH}.
*/
public static DittoConciergeThingsAggregatorConfig of(final Config config) {
Expand All @@ -417,12 +422,12 @@ public static DittoConciergeThingsAggregatorConfig of(final Config config) {

@Override
public Duration getSingleRetrieveThingTimeout() {
return config.getDuration(ThingsAggregatorConfigValue.SINGLE_RETRIEVE_THING_TIMEOUT.getPath());
return config.getDuration(ThingsAggregatorConfigValue.SINGLE_RETRIEVE_THING_TIMEOUT.getConfigPath());
}

@Override
public int getMaxParallelism() {
return config.getInt(ThingsAggregatorConfigValue.MAX_PARALLELISM.getPath());
return config.getInt(ThingsAggregatorConfigValue.MAX_PARALLELISM.getConfigPath());
}

@Override
Expand Down
Expand Up @@ -59,8 +59,7 @@ private ConfigWithFallback(final Config theBaseConfig) {
* {@code originalConfig} at {@code configPath}.
* @return the instance.
* @throws DittoConfigError if any argument is {@code null} of if the value of {@code originalConfig} at
* {@code configPath} is not of type
* {@link com.typesafe.config.ConfigValueType#OBJECT}.
* {@code configPath} is not of type {@link com.typesafe.config.ConfigValueType#OBJECT}.
*/
public static ConfigWithFallback newInstance(final Config originalConfig, final String configPath,
final KnownConfigValue[] fallBackValues) {
Expand Down Expand Up @@ -102,7 +101,7 @@ private static Config tryToGetAsConfig(final Config originalConfig, final String
private static Config arrayToConfig(final KnownConfigValue[] knownConfigValues) {
final Map<String, Object> fallbackValues = new HashMap<>(knownConfigValues.length);
for (final KnownConfigValue knownConfigValue : knownConfigValues) {
fallbackValues.put(knownConfigValue.getPath(), knownConfigValue.getDefaultValue());
fallbackValues.put(knownConfigValue.getConfigPath(), knownConfigValue.getDefaultValue());
}
return ConfigFactory.parseMap(fallbackValues);
}
Expand Down
Expand Up @@ -19,14 +19,7 @@
* </ul>
* A path expression is a dot-separated expression such as {@code foo.bar.baz}.
*/
public interface KnownConfigValue {

/**
* Returns the path of this config value.
*
* @return the path expression.
*/
String getPath();
public interface KnownConfigValue extends WithConfigPath {

/**
* Returns the default value to fall back to if no value was set in configuration for the path.
Expand Down
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.utils.config;

/**
* Implementations of this interface provide access to their config path.
*/
public interface WithConfigPath {

/**
* Returns the relative config path.
*
* @return the relative config path.
*/
String getConfigPath();

}
Expand Up @@ -79,14 +79,14 @@ public void useFallBackValuesIfConfigHasNoNestedConfigAtExpectedPath() {
final Config config = ConfigFactory.parseMap(Collections.singletonMap("foo", "bar"));

final KnownConfigValue barFallbackValue = Mockito.mock(KnownConfigValue.class);
Mockito.when(barFallbackValue.getPath()).thenReturn("bar");
Mockito.when(barFallbackValue.getConfigPath()).thenReturn("bar");
Mockito.when(barFallbackValue.getDefaultValue()).thenReturn(1);

final ConfigWithFallback underTest =
ConfigWithFallback.newInstance(config, KNOWN_CONFIG_PATH, new KnownConfigValue[]{barFallbackValue});

assertThat(underTest.root()).satisfies(configObject -> assertThat(configObject).hasSize(1));
assertThat(underTest.getInt(barFallbackValue.getPath())).isEqualTo(barFallbackValue.getDefaultValue());
assertThat(underTest.getInt(barFallbackValue.getConfigPath())).isEqualTo(barFallbackValue.getDefaultValue());
}

@Test
Expand Down Expand Up @@ -114,7 +114,7 @@ public void useFallBackValuesIfNotSetInOriginalConfig() {
final Config config = ConfigFactory.parseMap(Collections.singletonMap(KNOWN_CONFIG_PATH, configValueMap));

final KnownConfigValue barFallbackValue = Mockito.mock(KnownConfigValue.class);
Mockito.when(barFallbackValue.getPath()).thenReturn("bar");
Mockito.when(barFallbackValue.getConfigPath()).thenReturn("bar");
Mockito.when(barFallbackValue.getDefaultValue()).thenReturn(1);

final ConfigWithFallback underTest =
Expand Down

0 comments on commit 2dbc420

Please sign in to comment.