Skip to content

Commit 33120cc

Browse files
committed
Added HOCAN configuration provider
1 parent 9dd15cd commit 33120cc

File tree

7 files changed

+147
-53
lines changed

7 files changed

+147
-53
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,4 @@ Sledgehammer [Bungeecord]\.idea/**/mongoSettings.xml
173173
!/test.json
174174
/application/src/main/java/com/noahhusby/lib/application/test/
175175
/test.yml
176+
/test.cfg

application/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ dependencies {
2222
implementation project(":common")
2323
implementation "com.google.code.gson:gson:$gsonVersion"
2424
implementation "org.yaml:snakeyaml:$snakeVersion"
25+
implementation "com.typesafe:config:$configVersion"
2526
}

application/src/main/java/com/noahhusby/lib/application/config/Config.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import com.noahhusby.lib.application.config.provider.ConfigurationProvider;
2424
import com.noahhusby.lib.application.config.provider.JsonConfigurationProvider;
25-
import com.noahhusby.lib.application.config.provider.VisualConfigurationProvider;
25+
import com.noahhusby.lib.application.config.provider.HoconConfigurationProvider;
2626
import com.noahhusby.lib.application.config.provider.YamlConfigurationProvider;
2727
import lombok.AllArgsConstructor;
2828
import lombok.Getter;
@@ -52,7 +52,7 @@
5252
enum Type {
5353
JSON(".json", JsonConfigurationProvider.class),
5454
YAML(".yml", YamlConfigurationProvider.class),
55-
VISUAL(".cfg", VisualConfigurationProvider.class);
55+
HOCON(".cfg", HoconConfigurationProvider.class);
5656

5757
private String extension;
5858
private Class<? extends ConfigurationProvider> provider;

application/src/main/java/com/noahhusby/lib/application/config/Configuration.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,23 @@ public static Configuration of(@NonNull Class<?> clazz, @NonNull File directory)
236236
* @throws IllegalAccessException if a given property cannot be accessed
237237
*/
238238
public static Map<String, Property> getProperties(Class<?> clazz) throws IllegalAccessException {
239+
return getProperties(clazz, clazz.getDeclaredFields());
240+
}
241+
242+
/**
243+
* Get a map of properties from a config class.
244+
*
245+
* @param obj Object
246+
* @return A map of properties
247+
* @throws IllegalAccessException if a given property cannot be accessed
248+
*/
249+
public static Map<String, Property> getProperties(Object obj) throws IllegalAccessException {
250+
return getProperties(obj, obj.getClass().getDeclaredFields());
251+
}
252+
253+
public static Map<String, Property> getProperties(Object obj, Field[] fields) throws IllegalAccessException {
239254
Map<String, Property> properties = new HashMap<>();
240-
for (Field field : clazz.getDeclaredFields()) {
255+
for (Field field : fields) {
241256
if (field.isAnnotationPresent(Config.Ignore.class)) {
242257
continue;
243258
}
@@ -246,8 +261,9 @@ public static Map<String, Property> getProperties(Class<?> clazz) throws Illegal
246261
String name = nameAnnotation == null ? field.getName() : nameAnnotation.value();
247262
String[] comment = commentAnnotation == null ? null : commentAnnotation.value();
248263
field.setAccessible(true);
249-
properties.put(name, new Property(name, comment, field.get(clazz), field));
264+
properties.put(name, new Property(name, comment, field.get(obj), field));
250265
}
251266
return properties;
252267
}
268+
253269
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright 2020-2021 noahhusby
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
7+
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
8+
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
9+
* is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*
19+
*/
20+
21+
package com.noahhusby.lib.application.config.provider;
22+
23+
import com.google.gson.JsonArray;
24+
import com.google.gson.JsonElement;
25+
import com.google.gson.JsonObject;
26+
import com.noahhusby.lib.application.config.Configuration;
27+
import com.noahhusby.lib.application.config.Property;
28+
import com.noahhusby.lib.application.config.source.ConfigurationSource;
29+
import com.noahhusby.lib.common.util.HusbyUtil;
30+
import com.typesafe.config.Config;
31+
import com.typesafe.config.ConfigFactory;
32+
import lombok.Builder;
33+
import lombok.Cleanup;
34+
import lombok.NonNull;
35+
import lombok.SneakyThrows;
36+
37+
import java.io.BufferedWriter;
38+
import java.io.File;
39+
import java.io.FileWriter;
40+
import java.io.Reader;
41+
import java.nio.file.Files;
42+
import java.util.ArrayList;
43+
import java.util.HashMap;
44+
import java.util.Map;
45+
46+
/**
47+
* @author Noah Husby
48+
*/
49+
public class HoconConfigurationProvider extends ConfigurationProvider {
50+
51+
@Builder(setterPrefix = "set")
52+
public HoconConfigurationProvider(@NonNull ConfigurationSource source) {
53+
super(source);
54+
}
55+
56+
@SneakyThrows
57+
@Override
58+
public void load() {
59+
File file = source.getFile();
60+
@Cleanup Reader reader = Files.newBufferedReader(file.toPath());
61+
Config config = ConfigFactory.parseReader(reader);
62+
entries = config.root().unwrapped();
63+
}
64+
65+
@Override
66+
@SneakyThrows
67+
public void update(Map<String, Property> properties) {
68+
load();
69+
Map<String, Property> output = new HashMap<>(properties);
70+
for (String key : new ArrayList<>(properties.keySet())) {
71+
if (entries.containsKey(key)) {
72+
Property old = properties.get(key);
73+
Object newValue = HusbyUtil.GSON.fromJson(HusbyUtil.GSON.toJson(entries.get(key)), old.getField().getType());
74+
output.put(key, new Property(old.getName(), old.getComment(), newValue, old.getField()));
75+
}
76+
}
77+
BufferedWriter writer = new BufferedWriter(new FileWriter(getSource().getFile()));
78+
writer.write("# Configuration File\n\n");
79+
writeProperties(writer, output);
80+
writer.close();
81+
load();
82+
}
83+
84+
@SneakyThrows
85+
private void writeProperties(BufferedWriter writer, Map<String, Property> properties) {
86+
writeProperties(writer, properties, "");
87+
}
88+
89+
@SneakyThrows
90+
private void writeProperties(BufferedWriter writer, Map<String, Property> properties, String indent) {
91+
for (Map.Entry<String, Property> property : properties.entrySet()) {
92+
JsonElement element = HusbyUtil.GSON.toJsonTree(property.getValue().getValue());
93+
if (element instanceof JsonObject) {
94+
if (property.getValue().getComment() != null) {
95+
writer.write(indent + "##########################################################################################################\n");
96+
writer.write(indent + "# " + property.getKey() + "\n");
97+
writer.write(indent + "#--------------------------------------------------------------------------------------------------------#\n");
98+
for (String comment : property.getValue().getComment()) {
99+
writer.write(indent + "# " + comment + "\n");
100+
}
101+
writer.write(indent + "##########################################################################################################\n\n");
102+
}
103+
writer.write(indent + property.getKey() + " = {\n");
104+
writeProperties(writer, Configuration.getProperties(property.getValue().getValue()), indent + "\t");
105+
writer.write(indent + "}\n");
106+
} else {
107+
if (property.getValue().getComment() != null) {
108+
for (String comment : property.getValue().getComment()) {
109+
writer.write(indent + "# " + comment + "\n");
110+
}
111+
}
112+
if (element instanceof JsonArray) {
113+
writer.write(indent + property.getKey() + " = [\n");
114+
for (JsonElement e : element.getAsJsonArray()) {
115+
writer.write(indent + indent + e.toString() + "\n");
116+
}
117+
writer.write(indent + "]\n");
118+
} else {
119+
writer.write(indent + property.getKey() + " = " + property.getValue().getValue() + "\n\n");
120+
}
121+
}
122+
}
123+
}
124+
}

application/src/main/java/com/noahhusby/lib/application/config/provider/VisualConfigurationProvider.java

-49
This file was deleted.

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ allprojects {
2727
gsonVersion = "2.8.0"
2828
snakeVersion = "1.29"
2929
guavaVersion = "31.0.1-jre"
30+
configVersion = "1.4.1"
3031
mysqlVersion = "8.0.27"
3132
hikariVersion = "4.0.3"
3233
lombokVersion = "1.18.22"

0 commit comments

Comments
 (0)