Skip to content

Commit a500c81

Browse files
authored
feat: lazy load jsonParser and yamlParser FlagFileParser (#35)
I want to be able to use the thin jar without the snakeyaml dependency, since I don't need to parse yaml files, just json files. If I try that I get the following exception: ``` Cause: java.lang.ClassNotFoundException: org.yaml.snakeyaml.error.YAMLException at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:476) at sbt.internal.ManagedClassLoader.findClass(ManagedClassLoader.java:103) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:594) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:527) at com.launchdarkly.sdk.server.integrations.FileDataSourceParsing$FlagFileParser.<clinit>(FileDataSourceParsing.java:76) at com.launchdarkly.sdk.server.integrations.FileDataSourceImpl$DataLoader.load(FileDataSourceImpl.java:241) at com.launchdarkly.sdk.server.integrations.FileDataSourceImpl.reload(FileDataSourceImpl.java:108) at com.launchdarkly.sdk.server.integrations.FileDataSourceImpl.start(FileDataSourceImpl.java:92) at com.launchdarkly.sdk.server.LDClient.<init>(LDClient.java:239) ``` This PR avoids loading the `YamlFlagFileParser` by using a holder class idiom for lazy initialization of a static field. **Requirements** - [ ] I have added test coverage for new or changed functionality - Existing tests still pass, but I don't see a way to add unit tests for this functionality. I published this library locally and tested it on my project successfully. - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [ ] I have validated my changes against all supported platform versions **Related issues** n/a **Describe the solution you've provided** The solution is to lazy load the `YamlFlagFileParser` (and the `JsonFlagFileParser`) so they are only initialised if needed **Describe alternatives you've considered** n/a **Additional context** n/a
1 parent 8fe5eef commit a500c81

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/sdk/server/src/main/java/com/launchdarkly/sdk/server/integrations/FileDataSourceParsing.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,24 @@ static final class FlagFileRep {
7272
}
7373

7474
static abstract class FlagFileParser {
75-
private static final FlagFileParser jsonParser = new JsonFlagFileParser();
76-
private static final FlagFileParser yamlParser = new YamlFlagFileParser();
75+
static class JsonParserHolder {
76+
private static final JsonFlagFileParser INSTANCE = new JsonFlagFileParser();
77+
static JsonFlagFileParser getInstance() {
78+
return INSTANCE;
79+
}
80+
}
81+
static class YamlParserHolder {
82+
private static final YamlFlagFileParser INSTANCE = new YamlFlagFileParser();
83+
static YamlFlagFileParser getInstance() {
84+
return INSTANCE;
85+
}
86+
}
7787

7888
public abstract FlagFileRep parse(InputStream input) throws FileDataException, IOException;
7989

8090
public static FlagFileParser selectForContent(byte[] data) {
8191
Reader r = new InputStreamReader(new ByteArrayInputStream(data));
82-
return detectJson(r) ? jsonParser : yamlParser;
92+
return detectJson(r) ? JsonParserHolder.getInstance() : YamlParserHolder.getInstance();
8393
}
8494

8595
private static boolean detectJson(Reader r) {

0 commit comments

Comments
 (0)