com.sportradar.unifiedodds.sdk.SDKConfigurationReader Maven / Gradle / Ivy
/*
* Copyright (C) Sportradar AG. See LICENSE for full license governing this code
*/
package com.sportradar.unifiedodds.sdk;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created on 10/04/2018.
* // TODO @eti: Javadoc
*/
public abstract class SDKConfigurationReader {
private final Map sdkProperties;
SDKConfigurationReader() {
this.sdkProperties = readConfiguration();
}
public Optional readAccessToken() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.accessToken"));
}
public Optional readMaxInactivitySeconds() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.maxInactivitySeconds")).map(value -> {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The provided uf.sdk.maxInactivitySeconds is not a valid number, value: " + value);
}
});
}
public Optional readDefaultLocale() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.defaultLocale")).map(Locale::forLanguageTag);
}
public List readDesiredLocales() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.desiredLocales")).map(locales ->
Stream.of(locales.split(",")).map(Locale::forLanguageTag).collect(Collectors.toList())
).orElse(Collections.emptyList());
}
public Optional readApiHost() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.apiHost"));
}
public Optional readMessagingHost() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.messagingHost"));
}
public Optional readMessagingPort() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.messagingPort")).map(value -> {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The provided uf.sdk.messagingPort is not a valid number, value: " + value);
}
});
}
public Optional readMessagingUsername() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.messagingUsername"));
}
public Optional readMessagingPassword() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.messagingPassword"));
}
public Optional readMessagingVirtualHost() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.messagingVirtualHost"));
}
public Optional readUseApiSsl() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.useApiSsl"))
.map(value -> value.equals("true"));
}
public Optional readUseMessagingSsl() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.useMessagingSsl"))
.map(value -> value.equals("true"));
}
public List readDisabledProducers() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.disabledProducers")).map(locales ->
Stream.of(locales.split(",")).map(value -> {
try {
return Integer.valueOf(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The provided uf.sdk.disabledProducers contains an invalid number, invalid value: " + value);
}
}).collect(Collectors.toList())
).orElse(Collections.emptyList());
}
public Optional readMaxRecoveryTime() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.maxRecoveryTime")).map(value -> {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The provided uf.sdk.maxRecoveryTime is not a valid number, value: " + value);
}
});
}
public Optional readUseIntegration() {
Optional useIntegration = Optional.ofNullable(sdkProperties.get("uf.sdk.useIntegration"))
.map(value -> value.equals("true"));
if (!useIntegration.isPresent())
useIntegration = Optional.ofNullable(sdkProperties.get("uf.sdk.useStaging"))
.map(value -> value.equals("true"));
return useIntegration;
}
public Optional readSdkNodeId() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.nodeId"))
.map(value -> {
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The provided uf.sdk.nodeId is not a valid number, value: " + value);
}
});
}
public Optional readExceptionHandlingStrategy() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.exceptionHandlingStrategy"))
.map(value -> value.equalsIgnoreCase("throw") ? ExceptionHandlingStrategy.Throw : ExceptionHandlingStrategy.Catch);
}
public Optional readCleanTrafficLogEntries() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.cleanTrafficLogEntries"))
.map(value -> value.equals("true"));
}
public Optional readHttpClientTimeout() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.httpClientTimeout"))
.map(value -> {
int timeout = Integer.parseInt(value);
if (timeout < 0) {
throw new IllegalArgumentException("The provided uf.sdk.httpClientTimeout is not a valid timeout value, value: " + value);
}
return timeout;
});
}
public Optional readSimpleVariantCaching() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.simpleVariantCaching"))
.map(value -> value.equals("true"));
}
public Set readSchedulerTasksToSkip() {
return Optional.ofNullable(sdkProperties.get("uf.sdk.schedulerTasksToSkip"))
.map(locales -> Stream.of(locales.split(",")).collect(Collectors.toSet()))
.orElse(Collections.emptySet());
}
abstract Map readConfiguration();
}