Skip to content

Commit

Permalink
Add new config fileResource and refactor localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
nmayorsplit committed Aug 15, 2023
1 parent d439f75 commit f95cd7e
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.split.client;

import com.google.gson.stream.JsonReader;
import io.split.client.dtos.SplitChange;
import io.split.client.utils.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Map;

public class JsonFileLocalhostSplitChangeFetcher extends JsonLocalhostSplitChangeFetcher {

private static final Logger _log = LoggerFactory.getLogger(JsonFileLocalhostSplitChangeFetcher.class);
private final File _file;


public JsonFileLocalhostSplitChangeFetcher(String filePath) {
_file = new File(filePath);
super.lastHash = new byte[0];
}

@Override
public JsonReader readFile() {
try {
return new JsonReader(new FileReader("_file"));
} catch (FileNotFoundException f) {
_log.warn(String.format("There was no file named %s found. " +
"We created a split client that returns default treatments for all feature flags for all of your users. " +
"If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " +
"treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " +
"considered comments",
getFilePath(), getFilePath()), f);
throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f);
}
}

@Override
public String getFilePath() {
return _file.getPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,26 @@
import io.split.engine.experiments.SplitChangeFetcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher {

public abstract class JsonLocalhostSplitChangeFetcher implements SplitChangeFetcher {
private static final Logger _log = LoggerFactory.getLogger(JsonLocalhostSplitChangeFetcher.class);
private final File _file;
private byte [] lastHash;

public JsonLocalhostSplitChangeFetcher(String filePath) {
_file = new File(filePath);
lastHash = new byte[0];
}
byte [] lastHash;
public abstract JsonReader readFile();
public abstract String getFilePath();

@Override
public SplitChange fetch(long since, FetchOptions options) {

try {
JsonReader jsonReader = new JsonReader(new FileReader(_file));
SplitChange splitChange = Json.fromJson(jsonReader, SplitChange.class);
SplitChange splitChange = Json.fromJson(readFile(), SplitChange.class);
return processSplitChange(splitChange, since);
} catch (FileNotFoundException f){
_log.warn(String.format("There was no file named %s found. " +
"We created a split client that returns default treatments for all feature flags for all of your users. " +
"If you wish to return a specific treatment for a feature flag, enter the name of that feature flag name and " +
"treatment name separated by whitespace in %s; one pair per line. Empty lines or lines starting with '#' are " +
"considered comments",
_file.getPath(), _file.getPath()), f);
throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f);
} catch (Exception e) {
_log.warn(String.format("Problem to fetch split change using the file %s",
_file.getPath()), e);
getFilePath()), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.split.client;

import com.google.gson.stream.JsonReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class JsonResourceLocalhostSplitChangeFetcher extends JsonLocalhostSplitChangeFetcher {
private static final Logger _log = LoggerFactory.getLogger(JsonResourceLocalhostSplitChangeFetcher.class);
private final String _fileName;

public JsonResourceLocalhostSplitChangeFetcher(String fileName) {
_fileName = fileName;
super.lastHash = new byte[0];
}

@Override
public JsonReader readFile() {
try {
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(_fileName);
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
JsonReader jsonReader = new JsonReader(streamReader);
return jsonReader;
} catch (Exception e){
_log.warn(String.format("Problem to fetch split change using the file %s",
_fileName), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
}

@Override
public String getFilePath() {
return _fileName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class LegacyLocalhostSplitChangeFetcher implements SplitChangeFetcher {

private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class);
private static final Logger _log = LoggerFactory.getLogger(LegacyLocalhostSplitChangeFetcher.class);
static final String FILENAME = ".split";
private final File _splitFile;

Expand Down
20 changes: 20 additions & 0 deletions client/src/main/java/io/split/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class SplitClientConfig {
private final int _maxStringLength;
private final boolean _destroyOnShutDown;
private final String _splitFile;
private final String _splitFileResource;
private final String _segmentDirectory;
private final IntegrationsConfig _integrationsConfig;
private final boolean _streamingEnabled;
Expand Down Expand Up @@ -111,6 +112,7 @@ private SplitClientConfig(String endpoint,
int maxStringLength,
boolean destroyOnShutDown,
String splitFile,
String splitFileResource,
String segmentDirectory,
IntegrationsConfig integrationsConfig,
boolean streamingEnabled,
Expand Down Expand Up @@ -159,6 +161,7 @@ private SplitClientConfig(String endpoint,
_maxStringLength = maxStringLength;
_destroyOnShutDown = destroyOnShutDown;
_splitFile = splitFile;
_splitFileResource = splitFileResource;
_segmentDirectory = segmentDirectory;
_integrationsConfig = integrationsConfig;
_streamingEnabled = streamingEnabled;
Expand Down Expand Up @@ -300,6 +303,9 @@ public boolean destroyOnShutDown() {
public String splitFile() {
return _splitFile;
}
public String splitFileResource() {
return _splitFileResource;
}

public String segmentDirectory() {
return _segmentDirectory;
Expand Down Expand Up @@ -394,6 +400,7 @@ public static final class Builder {
private int _maxStringLength = 250;
private boolean _destroyOnShutDown = true;
private String _splitFile = null;
private String _splitFileResource = null;
private String _segmentDirectory = null;
private IntegrationsConfig _integrationsConfig = null;
private boolean _streamingEnabled = true;
Expand Down Expand Up @@ -748,6 +755,18 @@ public Builder splitFile(String splitFile) {
return this;
}

/**
* Set the location of the new yaml file for localhost when is located in resources, and it will be use inside a jar.
* This setting is optional.
*
* @param splitFileResource location
* @return this builder
*/
public Builder splitFileResource(String splitFileResource) {
_splitFileResource = splitFileResource;
return this;
}

/**
* Set the location of the directory where are the segment json files for localhost mode.
* This setting is optional.
Expand Down Expand Up @@ -1005,6 +1024,7 @@ public SplitClientConfig build() {
_maxStringLength,
_destroyOnShutDown,
_splitFile,
_splitFileResource,
_segmentDirectory,
_integrationsConfig,
_streamingEnabled,
Expand Down
21 changes: 16 additions & 5 deletions client/src/main/java/io/split/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,25 @@ protected SplitFactoryImpl(SplitClientConfig config) {
// SplitFetcher
SplitChangeFetcher splitChangeFetcher;
String splitFile = config.splitFile();
if (splitFile != null && splitFile.toLowerCase().endsWith(".json")){
splitChangeFetcher = new JsonLocalhostSplitChangeFetcher(config.splitFile());
} else if (splitFile != null && !splitFile.isEmpty() && (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml"))) {
splitChangeFetcher = new YamlLocalhostSplitChangeFetcher(splitFile);
if (splitFile != null){
if (splitFile.toLowerCase().endsWith(".json")){
splitChangeFetcher= null;
//splitChangeFetcher = new JsonFileLocalhostSplitChangeFetcher(config.splitFile());
} else if (!splitFile.isEmpty() && (splitFile.endsWith(".yaml") || splitFile.endsWith(".yml"))) {
splitChangeFetcher = new YamlFileLocalhostSplitChangeFetcher(config.splitFile());
} else {
splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile());
}
} else {
splitChangeFetcher = new LegacyLocalhostSplitChangeFetcher(config.splitFile());
String splitFileResource = config.splitFileResource();
if (splitFileResource != null && (splitFileResource.endsWith(".yaml") || splitFileResource.endsWith(".yml"))) {
splitChangeFetcher = new YamlResourceLocalhostSplitChangeFetcher(splitFileResource);
} else {
splitChangeFetcher = new YamlResourceLocalhostSplitChangeFetcher(splitFileResource);
}
}


SplitParser splitParser = new SplitParser();

_splitFetcher = new SplitFetcherImp(splitChangeFetcher, splitParser, splitCache, _telemetryStorageProducer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.split.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Map;

public class YamlFileLocalhostSplitChangeFetcher extends YamlLocalhostSplitChangeFetcher{

private static final Logger _log = LoggerFactory.getLogger(YamlFileLocalhostSplitChangeFetcher.class);
private final File _splitFile;

public YamlFileLocalhostSplitChangeFetcher(String fileName) {
_splitFile = new File(fileName);
}

@Override
public List<Map<String, Map<String, Object>>> readFile() {
try {
Yaml yaml = new Yaml();
return yaml.load(new FileReader(_splitFile));
} catch (FileNotFoundException f) {
_log.warn(String.format("There was no file named %s found. We created a split client that returns default treatments " +
"for all feature flags for all of your users. If you wish to return a specific treatment for a feature flag, " +
"enter the name of that feature flag name and treatment name separated by whitespace in %s; one pair per line. " +
"Empty lines or lines starting with '#' are considered comments",
_splitFile.getPath(), _splitFile.getPath()), f);
throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f);
} catch (Exception e) {
_log.warn(String.format("Problem to fetch split change using the file %s",
_splitFile.getPath()), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
}

@Override
public String getFilePath() {
return _splitFile.getPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import io.split.engine.experiments.SplitChangeFetcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -23,21 +19,18 @@

import static io.split.client.utils.LocalhostSanitizer.createCondition;

public abstract class YamlLocalhostSplitChangeFetcher implements SplitChangeFetcher {
private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class);

public class YamlLocalhostSplitChangeFetcher implements SplitChangeFetcher {
public abstract List<Map<String, Map<String, Object>>> readFile();

private static final Logger _log = LoggerFactory.getLogger(YamlLocalhostSplitChangeFetcher.class);
private final File _splitFile;
public abstract String getFilePath();

public YamlLocalhostSplitChangeFetcher(String filePath) {
_splitFile = new File(filePath);
}

@Override
public SplitChange fetch(long since, FetchOptions options) {
try {
Yaml yaml = new Yaml();
List<Map<String, Map<String, Object>>> yamlSplits = yaml.load(new FileReader(_splitFile));
List<Map<String, Map<String, Object>>> yamlSplits = readFile();
SplitChange splitChange = new SplitChange();
splitChange.splits = new ArrayList<>();
for(Map<String, Map<String, Object>> aSplit : yamlSplits) {
Expand Down Expand Up @@ -76,17 +69,10 @@ public SplitChange fetch(long since, FetchOptions options) {
splitChange.till = since;
splitChange.since = since;
return splitChange;
} catch (FileNotFoundException f) {
_log.warn(String.format("There was no file named %s found. We created a split client that returns default treatments " +
"for all feature flags for all of your users. If you wish to return a specific treatment for a feature flag, " +
"enter the name of that feature flag name and treatment name separated by whitespace in %s; one pair per line. " +
"Empty lines or lines starting with '#' are considered comments",
_splitFile.getPath(), _splitFile.getPath()), f);
throw new IllegalStateException("Problem fetching splitChanges: " + f.getMessage(), f);
} catch (Exception e) {
_log.warn(String.format("Problem to fetch split change using the file %s",
_splitFile.getPath()), e);
getFilePath()), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.split.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class YamlResourceLocalhostSplitChangeFetcher extends YamlLocalhostSplitChangeFetcher {
private static final Logger _log = LoggerFactory.getLogger(YamlFileLocalhostSplitChangeFetcher.class);
private final String _fileName;

public YamlResourceLocalhostSplitChangeFetcher(String fileName) {
_fileName = fileName;
}
@Override
public List<Map<String, Map<String, Object>>> readFile() {
try {
Yaml yaml = new Yaml();
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(_fileName);
return yaml.load(inputStream);
} catch (Exception e) {
_log.warn(String.format("Problem to fetch split change using the file %s",
_fileName), e);
throw new IllegalStateException("Problem fetching splitChanges: " + e.getMessage(), e);
}
}

@Override
public String getFilePath() {
return _fileName;
}
}
Loading

0 comments on commit f95cd7e

Please sign in to comment.