291 lines
9.2 KiB
Java
291 lines
9.2 KiB
Java
package com.navi.medici.config;
|
|
|
|
import com.navi.medici.annotation.Nullable;
|
|
import com.navi.medici.bootstrap.LitmusExperimentBootstrapProvider;
|
|
import com.navi.medici.provider.LitmusContextProvider;
|
|
import com.navi.medici.scheduler.LitmusScheduledExecutor;
|
|
import com.navi.medici.scheduler.LitmusScheduledExecutorImpl;
|
|
import com.navi.medici.strategy.Strategy;
|
|
import com.navi.medici.strategy.UnknownStrategy;
|
|
import com.navi.medici.util.LitmusURLs;
|
|
import java.io.File;
|
|
import java.net.InetAddress;
|
|
import java.net.URI;
|
|
import java.net.UnknownHostException;
|
|
import java.util.Objects;
|
|
import java.util.Optional;
|
|
|
|
public class LitmusConfig {
|
|
|
|
private final URI litmusAPI;
|
|
|
|
private final LitmusURLs litmusURLs;
|
|
|
|
private final String appName;
|
|
|
|
private final String instanceId;
|
|
|
|
private final String backupFile;
|
|
@Nullable
|
|
private final String projectName;
|
|
@Nullable
|
|
private final String namePrefix;
|
|
private final long fetchLitmusExperimentsInterval;
|
|
private final LitmusContextProvider contextProvider;
|
|
private final boolean synchronousFetchOnInitialisation;
|
|
private final LitmusScheduledExecutor litmusScheduledExecutor;
|
|
private final Strategy fallbackStrategy;
|
|
private final LitmusExperimentBootstrapProvider litmusExperimentBootstrapProvider;
|
|
private final String clickStreamAPI;
|
|
|
|
|
|
private LitmusConfig(
|
|
URI litmusAPI,
|
|
String appName,
|
|
String instanceId,
|
|
String backupFile,
|
|
@Nullable String projectName,
|
|
@Nullable String namePrefix,
|
|
long fetchLitmusExperimentsInterval,
|
|
LitmusContextProvider contextProvider,
|
|
boolean synchronousFetchOnInitialisation,
|
|
LitmusScheduledExecutor litmusScheduledExecutor,
|
|
Strategy fallbackStrategy,
|
|
LitmusExperimentBootstrapProvider litmusBootstrapProvider,
|
|
String clickStreamAPI) {
|
|
|
|
if (appName == null) {
|
|
throw new IllegalStateException("You are required to specify the litmus appName");
|
|
}
|
|
|
|
if (instanceId == null) {
|
|
throw new IllegalStateException("You are required to specify the litmus instanceId");
|
|
}
|
|
|
|
if (litmusAPI == null) {
|
|
throw new IllegalStateException("You are required to specify the litmusAPI url");
|
|
}
|
|
|
|
if (litmusScheduledExecutor == null) {
|
|
throw new IllegalStateException("You are required to specify a scheduler");
|
|
}
|
|
|
|
this.fallbackStrategy = Objects.requireNonNullElseGet(fallbackStrategy, UnknownStrategy::new);
|
|
|
|
this.litmusAPI = litmusAPI;
|
|
this.litmusURLs = new LitmusURLs(litmusAPI);
|
|
this.appName = appName;
|
|
this.instanceId = instanceId;
|
|
this.backupFile = backupFile;
|
|
this.projectName = projectName;
|
|
this.namePrefix = namePrefix;
|
|
this.fetchLitmusExperimentsInterval = fetchLitmusExperimentsInterval;
|
|
this.contextProvider = contextProvider;
|
|
this.synchronousFetchOnInitialisation = synchronousFetchOnInitialisation;
|
|
this.litmusScheduledExecutor = litmusScheduledExecutor;
|
|
this.litmusExperimentBootstrapProvider = litmusBootstrapProvider;
|
|
this.clickStreamAPI = clickStreamAPI;
|
|
}
|
|
|
|
public static Builder builder() {
|
|
return new Builder();
|
|
}
|
|
|
|
public URI getLitmusAPI() {
|
|
return litmusAPI;
|
|
}
|
|
|
|
|
|
public String getAppName() {
|
|
return appName;
|
|
}
|
|
|
|
public String getInstanceId() {
|
|
return instanceId;
|
|
}
|
|
|
|
public long getFetchLitmusExperimentsInterval() {
|
|
return fetchLitmusExperimentsInterval;
|
|
}
|
|
|
|
public String getBackupFile() {
|
|
return this.backupFile;
|
|
}
|
|
|
|
public LitmusURLs getLitmusURLs() {
|
|
return litmusURLs;
|
|
}
|
|
|
|
@Nullable
|
|
public String getProjectName() {
|
|
return projectName;
|
|
}
|
|
|
|
@Nullable
|
|
public String getNamePrefix() {
|
|
return namePrefix;
|
|
}
|
|
|
|
public boolean isSynchronousFetchOnInitialisation() {
|
|
return synchronousFetchOnInitialisation;
|
|
}
|
|
|
|
public LitmusContextProvider getContextProvider() {
|
|
return contextProvider;
|
|
}
|
|
|
|
public LitmusScheduledExecutor getScheduledExecutor() {
|
|
return litmusScheduledExecutor;
|
|
}
|
|
|
|
public Strategy getFallbackStrategy() {
|
|
return fallbackStrategy;
|
|
}
|
|
|
|
public LitmusExperimentBootstrapProvider getLitmusExperimentBootstrapProvider() {
|
|
return litmusExperimentBootstrapProvider;
|
|
}
|
|
|
|
public String getClickStreamAPI() {
|
|
return clickStreamAPI;
|
|
}
|
|
|
|
public static class Builder {
|
|
|
|
private URI litmusAPI;
|
|
private @Nullable String appName;
|
|
private String instanceId = getDefaultInstanceId();
|
|
private @Nullable String backupFile;
|
|
private @Nullable String projectName;
|
|
private @Nullable String namePrefix;
|
|
private long fetchLitmusExperimentsInterval = 10;
|
|
private LitmusContextProvider contextProvider = LitmusContextProvider.getDefaultProvider();
|
|
private boolean synchronousFetchOnInitialisation = false;
|
|
private @Nullable LitmusScheduledExecutor scheduledExecutor;
|
|
private @Nullable Strategy fallbackStrategy;
|
|
private @Nullable LitmusExperimentBootstrapProvider litmusExperimentBootstrapProvider;
|
|
private @Nullable String clickStreamAPI;
|
|
|
|
private static String getHostname() {
|
|
String hostName = System.getProperty("hostname");
|
|
if (hostName == null || hostName.length() == 0) {
|
|
try {
|
|
hostName = InetAddress.getLocalHost().getHostName();
|
|
} catch (UnknownHostException e) {
|
|
}
|
|
}
|
|
return hostName + "-";
|
|
}
|
|
|
|
static String getDefaultInstanceId() {
|
|
return getHostname() + "generated-" + Math.round(Math.random() * 1000000.0D);
|
|
}
|
|
|
|
public Builder litmusAPI(URI litmusAPI) {
|
|
this.litmusAPI = litmusAPI;
|
|
return this;
|
|
}
|
|
|
|
public Builder litmusAPI(String litmusAPI) {
|
|
this.litmusAPI = URI.create(litmusAPI);
|
|
return this;
|
|
}
|
|
|
|
public Builder appName(String appName) {
|
|
this.appName = appName;
|
|
return this;
|
|
}
|
|
|
|
public Builder instanceId(String instanceId) {
|
|
this.instanceId = instanceId;
|
|
return this;
|
|
}
|
|
|
|
public Builder fetchLitmusExperimentsInterval(long fetchLitmusExperimentsInterval) {
|
|
this.fetchLitmusExperimentsInterval = fetchLitmusExperimentsInterval;
|
|
return this;
|
|
}
|
|
|
|
public Builder backupFile(String backupFile) {
|
|
this.backupFile = backupFile;
|
|
return this;
|
|
}
|
|
|
|
public Builder litmusContextProvider(LitmusContextProvider contextProvider) {
|
|
this.contextProvider = contextProvider;
|
|
return this;
|
|
}
|
|
|
|
public Builder projectName(String projectName) {
|
|
this.projectName = projectName;
|
|
return this;
|
|
}
|
|
|
|
public Builder namePrefix(String namePrefix) {
|
|
this.namePrefix = namePrefix;
|
|
return this;
|
|
}
|
|
|
|
public Builder synchronousFetchOnInitialisation(boolean enable) {
|
|
this.synchronousFetchOnInitialisation = enable;
|
|
return this;
|
|
}
|
|
|
|
public Builder scheduledExecutor(LitmusScheduledExecutor scheduledExecutor) {
|
|
this.scheduledExecutor = scheduledExecutor;
|
|
return this;
|
|
}
|
|
|
|
public Builder fallbackStrategy(@Nullable Strategy fallbackStrategy) {
|
|
this.fallbackStrategy = fallbackStrategy;
|
|
return this;
|
|
}
|
|
|
|
public Builder litmusExperimentBootstrapProvider(
|
|
@Nullable LitmusExperimentBootstrapProvider litmusExperimentBootstrapProvider) {
|
|
this.litmusExperimentBootstrapProvider = litmusExperimentBootstrapProvider;
|
|
return this;
|
|
}
|
|
|
|
public Builder clickStreamAPI(@Nullable String clickStreamAPI) {
|
|
this.clickStreamAPI = clickStreamAPI;
|
|
return this;
|
|
}
|
|
|
|
private String getBackupFile() {
|
|
if (backupFile != null) {
|
|
return backupFile;
|
|
} else {
|
|
String fileName = "litmus-" + appName + "-repo.json";
|
|
return System.getProperty("java.io.tmpdir") + File.separatorChar + fileName;
|
|
}
|
|
}
|
|
|
|
|
|
public LitmusConfig build() {
|
|
return new LitmusConfig(
|
|
litmusAPI,
|
|
appName,
|
|
instanceId,
|
|
getBackupFile(),
|
|
projectName,
|
|
namePrefix,
|
|
fetchLitmusExperimentsInterval,
|
|
contextProvider,
|
|
synchronousFetchOnInitialisation,
|
|
Optional.ofNullable(scheduledExecutor)
|
|
.orElseGet(LitmusScheduledExecutorImpl::getInstance),
|
|
fallbackStrategy,
|
|
litmusExperimentBootstrapProvider,
|
|
clickStreamAPI);
|
|
}
|
|
|
|
public String getDefaultSdkVersion() {
|
|
String version =
|
|
Optional.ofNullable(getClass().getPackage().getImplementationVersion())
|
|
.orElse("development");
|
|
return "litmus-client:" + version;
|
|
}
|
|
}
|
|
}
|