Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
ReloadableConfigSource.java
Go to the documentation of this file.
1package dev.rafex.ether.config.sources;
2
3/*-
4 * #%L
5 * ether-config
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.io.IOException;
30import java.nio.file.Path;
31import java.nio.file.StandardWatchEventKinds;
32import java.nio.file.WatchEvent;
33import java.nio.file.WatchService;
34import java.util.Map;
35import java.util.Objects;
36import java.util.Optional;
37import java.util.concurrent.atomic.AtomicBoolean;
38import java.util.concurrent.atomic.AtomicReference;
39
40public final class ReloadableConfigSource implements ConfigSource, AutoCloseable {
41
42 public enum Format {
44 }
45
46 private final String name;
47 private final Path path;
48 private final Format format;
49 private final AtomicReference<Map<String, String>> values;
50 private final AtomicBoolean running;
51 private final WatchService watchService;
52 private final Thread watcherThread;
53
54 private ReloadableConfigSource(final Path path, final Format format) throws IOException {
55 this.path = Objects.requireNonNull(path, "path");
56 this.format = Objects.requireNonNull(format, "format");
57 name = "reloadable:" + format.name().toLowerCase() + ":" + path.toAbsolutePath();
58 values = new AtomicReference<>(load(path, format));
59 running = new AtomicBoolean(true);
60 watchService = path.getParent().getFileSystem().newWatchService();
61 path.getParent().register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
62 StandardWatchEventKinds.ENTRY_MODIFY);
63 watcherThread = Thread.ofVirtual().name("ether-config-reload-" + path.getFileName()).start(this::watchLoop);
64 }
65
66 public static ReloadableConfigSource of(final Path path, final Format format) throws IOException {
67 return new ReloadableConfigSource(path, format);
68 }
69
70 @Override
71 public String name() {
72 return name;
73 }
74
75 @Override
76 public Optional<String> get(final String key) {
77 return Optional.ofNullable(values.get().get(key));
78 }
79
80 @Override
81 public Map<String, String> entries() {
82 return values.get();
83 }
84
85 public void reloadNow() throws IOException {
86 values.set(load(path, format));
87 }
88
89 @Override
90 public void close() throws IOException {
91 running.set(false);
92 watcherThread.interrupt();
93 watchService.close();
94 }
95
96 private void watchLoop() {
97 while (running.get()) {
98 try {
99 final var key = watchService.take();
100 for (final WatchEvent<?> event : key.pollEvents()) {
101 final var changed = (Path) event.context();
102 if (path.getFileName().equals(changed)) {
103 try {
104 reloadNow();
105 } catch (IOException _) {
106 // Keep serving the previous snapshot until a valid reload is available.
107 }
108 }
109 }
110 key.reset();
111 } catch (InterruptedException _) {
112 Thread.currentThread().interrupt();
113 return;
114 }
115 }
116 }
117
118 private static Map<String, String> load(final Path path, final Format format) throws IOException {
119 return switch (format) {
120 case PROPERTIES -> new PropertiesFileConfigSource(path).entries();
121 case JSON -> StructuredConfigSource.loadJson(path);
122 case YAML -> StructuredConfigSource.loadYaml(path);
123 case TOML -> StructuredConfigSource.loadToml(path);
124 };
125 }
126}
static ReloadableConfigSource of(final Path path, final Format format)