|
| 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 | +} |
0 commit comments