|
| 1 | +package flash.config; |
| 2 | + |
| 3 | +import org.simpleyaml.configuration.file.YamlConfiguration; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.nio.file.Files; |
| 11 | + |
| 12 | +/** |
| 13 | + * The FlashConfiguration class provides utility methods for loading, saving, and managing YAML configuration files. |
| 14 | + */ |
| 15 | +public class FlashConfiguration extends YamlConfiguration { |
| 16 | + private static final Logger logger = LoggerFactory.getLogger(FlashConfiguration.class); |
| 17 | + private final File file; |
| 18 | + |
| 19 | + /** |
| 20 | + * Constructs a FlashConfiguration instance of the default configuration file. |
| 21 | + */ |
| 22 | + public FlashConfiguration() { |
| 23 | + this("config.yml"); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Constructs a FlashConfiguration instance with a specified file path. |
| 28 | + * |
| 29 | + * @param path the path to the configuration file. |
| 30 | + */ |
| 31 | + public FlashConfiguration(String path) { |
| 32 | + this(getFile(path)); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructs a FlashConfiguration instance with a specified file. |
| 37 | + * |
| 38 | + * @param file the configuration file. |
| 39 | + */ |
| 40 | + private FlashConfiguration(File file) { |
| 41 | + super(loadConfig(file)); |
| 42 | + this.file = file; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Loads a YamlConfiguration from a file. |
| 47 | + * |
| 48 | + * @param file the configuration file to load |
| 49 | + * @return the loaded YamlConfiguration |
| 50 | + */ |
| 51 | + private static YamlConfiguration loadConfig(File file) { |
| 52 | + try { |
| 53 | + return YamlConfiguration.loadConfiguration(file); |
| 54 | + } catch (IOException e) { |
| 55 | + logger.error("Failed to load configuration file: {}", file.getPath(), e); |
| 56 | + throw new RuntimeException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Retrieves the configuration file. |
| 62 | + * |
| 63 | + * @param path the path to the configuration file. |
| 64 | + * @return the configuration file |
| 65 | + */ |
| 66 | + private static File getFile(String path) { |
| 67 | + File file = new File(path); |
| 68 | + try { |
| 69 | + if (!file.exists()) { |
| 70 | + try (InputStream resourceStream = FlashConfiguration.class.getClassLoader().getResourceAsStream(file.getName())) { |
| 71 | + if (resourceStream != null) { |
| 72 | + Files.copy(resourceStream, file.toPath()); |
| 73 | + } else { |
| 74 | + file.createNewFile(); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return file; |
| 79 | + } catch (IOException e) { |
| 80 | + logger.error("Failed to create configuration file: {}", path, e); |
| 81 | + throw new RuntimeException(e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Saves the configuration file to disk. |
| 87 | + */ |
| 88 | + public void save() { |
| 89 | + try { |
| 90 | + save(file); |
| 91 | + } catch (IOException e) { |
| 92 | + logger.error("Failed to save configuration file: {}", file.getPath(), e); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments