|
| 1 | +package io.kafbat.ui.service.app; |
| 2 | + |
| 3 | +import io.kafbat.ui.config.auth.RoleBasedAccessControlProperties; |
| 4 | +import io.kafbat.ui.util.MultiFileWatcher; |
| 5 | +import jakarta.annotation.PostConstruct; |
| 6 | +import jakarta.annotation.PreDestroy; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.LinkedHashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import java.util.stream.Stream; |
| 15 | +import java.util.stream.StreamSupport; |
| 16 | +import lombok.RequiredArgsConstructor; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 19 | +import org.springframework.boot.context.properties.bind.Binder; |
| 20 | +import org.springframework.boot.env.OriginTrackedMapPropertySource; |
| 21 | +import org.springframework.boot.env.YamlPropertySourceLoader; |
| 22 | +import org.springframework.boot.origin.Origin; |
| 23 | +import org.springframework.boot.origin.OriginTrackedValue; |
| 24 | +import org.springframework.boot.origin.TextResourceOrigin; |
| 25 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 26 | +import org.springframework.core.env.PropertySource; |
| 27 | +import org.springframework.core.io.FileSystemResource; |
| 28 | +import org.springframework.core.io.Resource; |
| 29 | +import org.springframework.stereotype.Service; |
| 30 | + |
| 31 | +@Service |
| 32 | +@RequiredArgsConstructor |
| 33 | +@Slf4j |
| 34 | +@ConditionalOnProperty(value = "config.autoreload", havingValue = "true") |
| 35 | +public class ConfigReloadService { |
| 36 | + |
| 37 | + private static final String THREAD_NAME = "config-watcher-thread"; |
| 38 | + |
| 39 | + private final ConfigurableEnvironment environment; |
| 40 | + private final RoleBasedAccessControlProperties rbacProperties; |
| 41 | + private final YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader(); |
| 42 | + |
| 43 | + private Thread watcherThread; |
| 44 | + private MultiFileWatcher multiFileWatcher; |
| 45 | + |
| 46 | + @PostConstruct |
| 47 | + public void init() { |
| 48 | + var propertySourcePaths = StreamSupport.stream(environment.getPropertySources().spliterator(), false) |
| 49 | + .filter(OriginTrackedMapPropertySource.class::isInstance) |
| 50 | + .map(OriginTrackedMapPropertySource.class::cast) |
| 51 | + .flatMap(ps -> ps.getSource().values().stream()) |
| 52 | + .map(v -> (v instanceof OriginTrackedValue otv) ? otv.getOrigin() : null) |
| 53 | + .filter(Objects::nonNull) |
| 54 | + .flatMap(o -> Stream.iterate(o, Objects::nonNull, Origin::getParent)) |
| 55 | + .filter(TextResourceOrigin.class::isInstance) |
| 56 | + .map(TextResourceOrigin.class::cast) |
| 57 | + .map(TextResourceOrigin::getResource) |
| 58 | + .filter(Objects::nonNull) |
| 59 | + .filter(Resource::exists) |
| 60 | + .filter(Resource::isReadable) |
| 61 | + .filter(Resource::isFile) |
| 62 | + .map(r -> { |
| 63 | + try { |
| 64 | + return r.getURI(); |
| 65 | + } catch (IOException e) { |
| 66 | + log.error("can't retrieve resource URL", e); |
| 67 | + return null; |
| 68 | + } |
| 69 | + }) |
| 70 | + .filter(Objects::nonNull) |
| 71 | + .map(Paths::get) |
| 72 | + .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 73 | + |
| 74 | + if (propertySourcePaths.isEmpty()) { |
| 75 | + log.debug("No config files found, auto reload is disabled"); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + log.debug("Auto reload is enabled, will watch for config changes"); |
| 80 | + |
| 81 | + try { |
| 82 | + this.multiFileWatcher = new MultiFileWatcher(propertySourcePaths, this::reloadFile); |
| 83 | + this.watcherThread = new Thread(multiFileWatcher::watchLoop, THREAD_NAME); |
| 84 | + this.watcherThread.start(); |
| 85 | + } catch (IOException e) { |
| 86 | + log.error("Error while registering watch service", e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void reloadFile(Path path) { |
| 91 | + log.info("Reloading file {}", path); |
| 92 | + try { |
| 93 | + if (!path.toString().endsWith(".yml") && !path.toString().endsWith(".yaml")) { |
| 94 | + log.trace("Skipping non-YML file {}", path); |
| 95 | + } |
| 96 | + |
| 97 | + String name = String.format("Config resource 'file [%s] via location '%s'", |
| 98 | + path.toAbsolutePath(), |
| 99 | + path.toAbsolutePath()); // TODO extract an obj reference from env |
| 100 | + |
| 101 | + List<PropertySource<?>> load = yamlLoader.load(path.toString(), new FileSystemResource(path)); |
| 102 | + environment.getPropertySources().remove(name); |
| 103 | + environment.getPropertySources().addFirst(load.getFirst()); |
| 104 | + Binder binder = Binder.get(environment); |
| 105 | + |
| 106 | + binder.bind("rbac", RoleBasedAccessControlProperties.class) |
| 107 | + .ifBound(bound -> rbacProperties.setRoles(bound.getRoles())); |
| 108 | + } catch (Throwable e) { |
| 109 | + log.error("Error while reloading file {}", path, e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @PreDestroy |
| 114 | + public void shutdown() { |
| 115 | + try { |
| 116 | + if (multiFileWatcher != null) { |
| 117 | + multiFileWatcher.close(); |
| 118 | + } |
| 119 | + } catch (IOException ignored) { |
| 120 | + } |
| 121 | + if (watcherThread != null) { |
| 122 | + this.watcherThread.interrupt(); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments