Skip to content

Commit 54e65d7

Browse files
committed
initial
1 parent cb4d53f commit 54e65d7

22 files changed

+1466
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target/
2+
.classpath
3+
.project
4+
.settings/
5+
.idea/
6+
*.iml

pom.xml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.avaje</groupId>
8+
<artifactId>config</artifactId>
9+
<version>0.1-SNAPSHOT</version>
10+
11+
<parent>
12+
<groupId>org.avaje</groupId>
13+
<artifactId>java8-parent</artifactId>
14+
<version>1.3</version>
15+
</parent>
16+
17+
18+
<properties>
19+
<snakeyaml.version>1.21</snakeyaml.version>
20+
</properties>
21+
22+
<dependencies>
23+
24+
<!-- Exclude snakeyaml if you desire - it is not strictly required. -->
25+
<dependency>
26+
<groupId>org.yaml</groupId>
27+
<artifactId>snakeyaml</artifactId>
28+
<version>${snakeyaml.version}</version>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-api</artifactId>
34+
<version>1.7.25</version>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.avaje.composite</groupId>
39+
<artifactId>junit</artifactId>
40+
<version>1.1</version>
41+
<scope>test</scope>
42+
</dependency>
43+
44+
</dependencies>
45+
46+
</project>
+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package io.avaje.config;
2+
3+
import io.avaje.config.properties.PropertiesLoader;
4+
5+
import java.util.Properties;
6+
import java.util.function.Consumer;
7+
8+
/**
9+
* Provides application Configuration based on loading properties and yaml files
10+
* as well as plugins that supply properties (like dynamic configuration loaded from a db).
11+
* <p>
12+
* The application can register onChange listeners to handle changes to configuration
13+
* properties at runtime. Plugins or code can dynamically load and change properties and
14+
* this can fire any registered callback handlers.
15+
* </p>
16+
*/
17+
public class Config {
18+
19+
private static ConfigurationData data = init();
20+
21+
private static ConfigurationData init() {
22+
Properties properties = PropertiesLoader.load();
23+
return new ConfigurationData(properties);
24+
}
25+
26+
/**
27+
* Return a required configuration value as String.
28+
* <p>
29+
* IllegalStateException is thrown if the value is not defined in configuration.
30+
* </p>
31+
*
32+
* @param key The configuration key
33+
* @return The configured value
34+
*/
35+
public static String get(String key) {
36+
return data.get(key);
37+
}
38+
39+
/**
40+
* Return a configuration value as String given a default value.
41+
*
42+
* @param key The configuration key
43+
* @param defaultValue The default value used
44+
* @return The configured or default value
45+
*/
46+
public static String get(String key, String defaultValue) {
47+
return data.get(key, defaultValue);
48+
}
49+
50+
/**
51+
* Return a required boolean configuration value.
52+
* <p>
53+
* IllegalStateException is thrown if the value is not defined in configuration.
54+
* </p>
55+
*
56+
* @param key The configuration key
57+
* @return The configured value
58+
*/
59+
public static boolean getBool(String key) {
60+
return data.getBool(key);
61+
}
62+
63+
/**
64+
* Return a configuration value as boolean given a default value.
65+
*
66+
* @param key The configuration key
67+
* @param defaultValue The default value used
68+
* @return The configured or default value
69+
*/
70+
public static boolean getBool(String key, boolean defaultValue) {
71+
return data.getBool(key, defaultValue);
72+
}
73+
74+
/**
75+
* Return a required int configuration value.
76+
* <p>
77+
* IllegalStateException is thrown if the value is not defined in configuration.
78+
* </p>
79+
*
80+
* @param key The configuration key
81+
* @return The configured value
82+
*/
83+
public static int getInt(String key) {
84+
return data.getInt(key);
85+
}
86+
87+
/**
88+
* Return a configuration value as int given a default value.
89+
*
90+
* @param key The configuration key
91+
* @param defaultValue The default value used
92+
* @return The configured or default value
93+
*/
94+
public static int getInt(String key, int defaultValue) {
95+
return data.getInt(key, defaultValue);
96+
}
97+
98+
/**
99+
* Return a required long configuration value.
100+
* <p>
101+
* IllegalStateException is thrown if the value is not defined in configuration.
102+
* </p>
103+
*
104+
* @param key The configuration key
105+
* @return The configured value
106+
*/
107+
public static long getLong(String key) {
108+
return data.getLong(key);
109+
}
110+
111+
/**
112+
* Return a configuration value as long given a default value.
113+
*
114+
* @param key The configuration key
115+
* @param defaultValue The default value used
116+
* @return The configured or default value
117+
*/
118+
public static long getLong(String key, long defaultValue) {
119+
return data.getLong(key, defaultValue);
120+
}
121+
122+
/**
123+
* Set a configuration value.
124+
* <p>
125+
* This will fire an configuration callback listeners that are registered
126+
* for this key.
127+
* </p>
128+
*/
129+
public static void setProperty(String key, String value) {
130+
data.setProperty(key, value);
131+
}
132+
133+
/**
134+
* Register a callback for a change to the given configuration key.
135+
*
136+
* @param key The configuration key we want to detect changes to
137+
* @param callback The callback handling to fire when the configuration changes.
138+
*/
139+
public static void onChange(String key, Consumer<String> callback) {
140+
data.onChange(key, callback);
141+
}
142+
143+
/**
144+
* Register a callback for a change to the given configuration key as an Int value.
145+
*
146+
* @param key The configuration key we want to detect changes to
147+
* @param callback The callback handling to fire when the configuration changes.
148+
*/
149+
public static void onChangeInt(String key, Consumer<Integer> callback) {
150+
data.onChangeInt(key, callback);
151+
}
152+
153+
/**
154+
* Register a callback for a change to the given configuration key as an Long value.
155+
*
156+
* @param key The configuration key we want to detect changes to
157+
* @param callback The callback handling to fire when the configuration changes.
158+
*/
159+
public static void onChangeLong(String key, Consumer<Long> callback) {
160+
data.onChangeLong(key, callback);
161+
}
162+
163+
/**
164+
* Register a callback for a change to the given configuration key as an Boolean value.
165+
*
166+
* @param key The configuration key we want to detect changes to
167+
* @param callback The callback handling to fire when the configuration changes.
168+
*/
169+
public static void onChangeBool(String key, Consumer<Boolean> callback) {
170+
data.onChangeBool(key, callback);
171+
}
172+
}

0 commit comments

Comments
 (0)