-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathConfigLoader.java
83 lines (68 loc) · 3.25 KB
/
ConfigLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package net.openhft.chronicle.wire.utils;
import net.openhft.chronicle.core.io.IOTools;
import net.openhft.chronicle.wire.TextWire;
import net.openhft.chronicle.wire.WireType;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import static net.openhft.chronicle.bytes.util.PropertyReplacer.replaceTokensWithProperties;
/**
* Utility class for loading configuration files in YAML format. The class provides methods to load configuration
* files in YAML format from the classpath and convert them to Java objects. The class will replace
* tokens in the format {@code ${property}} within strings with System properties or supplied properties.
* <p>
* Files must be in YAML format that conform to WireType.TEXT. For example:
* <pre>{@code
* !SimpleConfig {
* name: "some name",
* value: 10,
* }
* }</pre>
* <p>
* The class must be fully qualified or added to the {@link net.openhft.chronicle.core.pool.ClassAliasPool} to
* enable the conversion.
* <pre>
* {@code ClassAliasPool.CLASS_ALIASES.addAlias(SimpleConfig.class);}
* </pre>
*/
public enum ConfigLoader {
; // none
public static String loadFile(Class<?> classLoader, String filename) throws IOException {
return new String(IOTools.readFile(classLoader, filename), StandardCharsets.UTF_8);
}
public static <T> T loadFromFile(String filename) throws IOException {
return loadFromFile(ConfigLoader.class, filename);
}
public static <T> T loadFromFile(Class<?> classLoader, String filename) throws IOException {
return load(loadFile(classLoader, filename));
}
public static <T> T loadFromFile(String filename, Properties properties) throws IOException {
return loadFromFile(ConfigLoader.class, filename, properties);
}
public static <T> T loadFromFile(Class<?> classLoader, String filename, Properties properties) throws IOException {
return loadWithProperties(loadFile(classLoader, filename), properties);
}
public static <T> T loadFromFile(Class<T> expectedClass, Class<?> classLoader, String filename, Properties properties) throws IOException {
return loadWithProperties(expectedClass, loadFile(classLoader, filename), properties);
}
@SuppressWarnings("unchecked")
public static <T> T load(String fileAsString) {
return (T) TextWire.from(replaceTokensWithProperties(fileAsString)).readObject();
}
@SuppressWarnings("unchecked")
public static <T> T loadWithProperties(String fileAsString) {
return loadWithProperties(fileAsString, System.getProperties());
}
@SuppressWarnings("unchecked")
public static <T> T loadWithProperties(String fileAsString, Properties properties) {
return loadWithProperties(null, fileAsString, properties);
}
@SuppressWarnings("unchecked")
public static <T> T loadWithProperties(Class<T> expectedClass, String fileAsString) {
return loadWithProperties(expectedClass, fileAsString, System.getProperties());
}
@SuppressWarnings("unchecked")
public static <T> T loadWithProperties(Class<T> expectedClass, String fileAsString, Properties properties) {
return WireType.TEXT.fromString(expectedClass, replaceTokensWithProperties(fileAsString, properties));
}
}