Skip to content

Commit ee173a5

Browse files
authored
Merge pull request #95 from avaje/feature/internals-parsers
Refactor internals adding Parsers to hold all parsers
2 parents 383100b + a40dd3c commit ee173a5

File tree

4 files changed

+86
-43
lines changed

4 files changed

+86
-43
lines changed

avaje-config/src/main/java/io/avaje/config/FileWatch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ final class FileWatch {
1111

1212
private final ConfigurationLog log;
1313
private final Configuration configuration;
14-
private final Map<String, ConfigParser> parserMap;
14+
private final Parsers parsers;
1515
private final List<Entry> files;
1616
private final long delay;
1717
private final long period;
1818

19-
FileWatch(CoreConfiguration configuration, List<File> loadedFiles, Map<String, ConfigParser> parserMap) {
19+
FileWatch(CoreConfiguration configuration, List<File> loadedFiles, Parsers parsers) {
2020
this.log = configuration.log();
2121
this.configuration = configuration;
2222
this.delay = configuration.getLong("config.watch.delay", 60);
2323
this.period = configuration.getInt("config.watch.period", 10);
24-
this.parserMap = parserMap;
24+
this.parsers = parsers;
2525
this.files = initFiles(loadedFiles);
2626
if (files.isEmpty()) {
2727
log.log(Level.ERROR, "No files to watch?");
@@ -84,7 +84,7 @@ private void reloadProps(Entry file, Map<String, String> keyValues) {
8484
}
8585

8686
private void reloadYaml(Entry file, Map<String, String> keyValues) {
87-
var parser = parserMap.get(file.extension);
87+
var parser = parsers.get(file.extension);
8888
if (parser == null) {
8989
log.log(Level.ERROR, "Unexpected - no parser to reload config file " + file);
9090
} else {

avaje-config/src/main/java/io/avaje/config/InitialLoader.java

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ enum Source {
3939
private final ConfigurationLog log;
4040
private final InitialLoadContext loadContext;
4141
private final Set<String> profileResourceLoaded = new HashSet<>();
42-
private final Map<String, ConfigParser> parserMap = new HashMap<>();
42+
private final Parsers parsers;
4343

4444
InitialLoader(ConfigurationLog log, ResourceLoader resourceLoader) {
4545
this.log = log;
4646
this.loadContext = new InitialLoadContext(log, resourceLoader);
47-
48-
initCustomLoaders();
47+
this.parsers = new Parsers();
4948
}
5049

5150
Set<String> loadedFrom() {
@@ -92,29 +91,7 @@ CoreMap load() {
9291

9392
void initWatcher(CoreConfiguration configuration) {
9493
if (configuration.getBool("config.watch.enabled", false)) {
95-
configuration.setWatcher(new FileWatch(configuration, loadContext.loadedFiles(), parserMap));
96-
}
97-
}
98-
99-
private void initCustomLoaders() {
100-
if (!"true".equals(System.getProperty("skipYaml"))) {
101-
YamlLoader yamlLoader;
102-
try {
103-
Class.forName("org.yaml.snakeyaml.Yaml");
104-
yamlLoader = new YamlLoaderSnake();
105-
} catch (ClassNotFoundException e) {
106-
yamlLoader = new YamlLoaderSimple();
107-
}
108-
parserMap.put("yml", yamlLoader);
109-
parserMap.put("yaml", yamlLoader);
110-
}
111-
112-
if (!"true".equals(System.getProperty("skipCustomParsing"))) {
113-
ServiceLoader.load(ConfigParser.class).forEach(p -> {
114-
for (var ext : p.supportedExtensions()) {
115-
parserMap.put(ext, p);
116-
}
117-
});
94+
configuration.setWatcher(new FileWatch(configuration, loadContext.loadedFiles(), parsers));
11895
}
11996
}
12097

@@ -174,7 +151,7 @@ private void loadCommandLineArg(String arg) {
174151

175152
private boolean isValidExtension(String arg) {
176153
var extension = arg.substring(arg.lastIndexOf(".") + 1);
177-
return "properties".equals(extension) || parserMap.containsKey(extension);
154+
return "properties".equals(extension) || parsers.supportsExtension(extension);
178155
}
179156

180157
/**
@@ -278,11 +255,11 @@ boolean loadWithExtensionCheck(String fileName) {
278255
if ("properties".equals(extension)) {
279256
return loadProperties(fileName, RESOURCE) | loadProperties(fileName, FILE);
280257
} else {
281-
var parser = parserMap.get(extension);
258+
var parser = parsers.get(extension);
282259
if (parser == null) {
283260
throw new IllegalArgumentException(
284261
"Expecting only properties or "
285-
+ parserMap.keySet()
262+
+ parsers.supportedExtensions()
286263
+ " file extensions but got ["
287264
+ fileName
288265
+ "]");
@@ -308,7 +285,7 @@ boolean load(String resourcePath, Source source) {
308285
}
309286

310287
private boolean loadCustom(String resourcePath, Source source) {
311-
for (var entry : parserMap.entrySet()) {
288+
for (var entry : parsers.entrySet()) {
312289
var extension = entry.getKey();
313290
if (loadCustomExtension(resourcePath + "." + extension, entry.getValue(), source)) {
314291
return true;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.avaje.config;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.ServiceLoader;
6+
import java.util.Set;
7+
8+
/**
9+
* Holds the non-properties ConfigParsers.
10+
*/
11+
final class Parsers {
12+
13+
private final Map<String, ConfigParser> parserMap = new HashMap<>();
14+
15+
Parsers() {
16+
if (!"true".equals(System.getProperty("skipYaml"))) {
17+
initYamlParser();
18+
}
19+
if (!"true".equals(System.getProperty("skipCustomParsing"))) {
20+
initParsers();
21+
}
22+
}
23+
24+
private void initYamlParser() {
25+
YamlLoader yamlLoader;
26+
try {
27+
Class.forName("org.yaml.snakeyaml.Yaml");
28+
yamlLoader = new YamlLoaderSnake();
29+
} catch (ClassNotFoundException e) {
30+
yamlLoader = new YamlLoaderSimple();
31+
}
32+
parserMap.put("yml", yamlLoader);
33+
parserMap.put("yaml", yamlLoader);
34+
}
35+
36+
private void initParsers() {
37+
ServiceLoader.load(ConfigParser.class).forEach(p -> {
38+
for (var ext : p.supportedExtensions()) {
39+
parserMap.put(ext, p);
40+
}
41+
});
42+
}
43+
44+
/**
45+
* Return the extension ConfigParser pairs.
46+
*/
47+
Set<Map.Entry<String, ConfigParser>> entrySet() {
48+
return parserMap.entrySet();
49+
}
50+
51+
/**
52+
* Return the ConfigParser for the given extension.
53+
*/
54+
ConfigParser get(String extension) {
55+
return parserMap.get(extension);
56+
}
57+
58+
/**
59+
* Return true if the extension has a matching parser.
60+
*/
61+
boolean supportsExtension(String extension) {
62+
return parserMap.containsKey(extension);
63+
}
64+
65+
/**
66+
* Return the set of supported extensions.
67+
*/
68+
Set<String> supportedExtensions() {
69+
return parserMap.keySet();
70+
}
71+
}

avaje-config/src/test/java/io/avaje/config/FileWatchTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.io.IOException;
1111
import java.util.ArrayList;
1212
import java.util.List;
13-
import java.util.Map;
1413
import java.util.Properties;
1514

1615
import static org.assertj.core.api.Assertions.assertThat;
@@ -25,8 +24,7 @@ void test_when_notChanged() {
2524

2625
CoreConfiguration config = newConfig();
2726
List<File> files = files();
28-
YamlLoader yamlLoader = new YamlLoaderSnake();
29-
final FileWatch watch = new FileWatch(config, files, Map.of("yml", yamlLoader, "yaml", yamlLoader));
27+
final FileWatch watch = new FileWatch(config, files, new Parsers());
3028

3129
assertThat(config.size()).isEqualTo(2);
3230
// not touched
@@ -40,8 +38,7 @@ void test_check_whenTouched_expect_loaded() {
4038

4139
CoreConfiguration config = newConfig();
4240
List<File> files = files();
43-
YamlLoader yamlLoader = new YamlLoaderSnake();
44-
final FileWatch watch = new FileWatch(config, files, Map.of("yml", yamlLoader, "yaml", yamlLoader));
41+
final FileWatch watch = new FileWatch(config, files, new Parsers());
4542

4643
assertThat(config.size()).isEqualTo(2);
4744
assertThat(config.getOptional("one")).isEmpty();
@@ -69,8 +66,7 @@ void test_check_whenTouchedScheduled_expect_loaded() {
6966
fail("File " + file.getAbsolutePath() + " does not exist?");
7067
}
7168
}
72-
YamlLoader yamlLoader = new YamlLoaderSnake();
73-
final FileWatch watch = new FileWatch(config, files, Map.of("yml", yamlLoader, "yaml", yamlLoader));
69+
final FileWatch watch = new FileWatch(config, files, new Parsers());
7470
System.out.println(watch);
7571

7672
// assert not loaded
@@ -97,8 +93,7 @@ void test_check_whenFileWritten() throws Exception {
9793
CoreConfiguration config = newConfig();
9894
List<File> files = files();
9995

100-
YamlLoader yamlLoader = new YamlLoaderSnake();
101-
final FileWatch watch = new FileWatch(config, files, Map.of("yml", yamlLoader, "yaml", yamlLoader));
96+
final FileWatch watch = new FileWatch(config, files, new Parsers());
10297

10398
if (isGithubActions()) {
10499
File aFile = files.get(0);

0 commit comments

Comments
 (0)