|
1 | 1 | package io.kafbat.ui.service.app; |
2 | 2 |
|
3 | 3 | import io.kafbat.ui.config.auth.RoleBasedAccessControlProperties; |
4 | | -import io.kafbat.ui.service.rbac.AccessControlService; |
5 | 4 | import io.kafbat.ui.util.MultiFileWatcher; |
6 | 5 | import jakarta.annotation.PostConstruct; |
7 | 6 | import jakarta.annotation.PreDestroy; |
8 | 7 | import java.io.IOException; |
9 | | -import java.io.InputStream; |
10 | | -import java.nio.file.Files; |
11 | 8 | import java.nio.file.Path; |
12 | 9 | import java.nio.file.Paths; |
13 | 10 | import java.util.LinkedHashSet; |
14 | 11 | import java.util.List; |
15 | 12 | import java.util.Objects; |
16 | | -import java.util.Properties; |
17 | 13 | import java.util.stream.Collectors; |
18 | 14 | import java.util.stream.Stream; |
19 | 15 | import java.util.stream.StreamSupport; |
20 | | -import lombok.Cleanup; |
21 | 16 | import lombok.RequiredArgsConstructor; |
22 | 17 | import lombok.extern.slf4j.Slf4j; |
23 | | -import org.springframework.beans.factory.ObjectProvider; |
24 | | -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
25 | | -import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; |
26 | 18 | import org.springframework.boot.context.properties.bind.Binder; |
27 | 19 | import org.springframework.boot.env.OriginTrackedMapPropertySource; |
28 | 20 | import org.springframework.boot.env.YamlPropertySourceLoader; |
29 | 21 | import org.springframework.boot.origin.Origin; |
30 | 22 | import org.springframework.boot.origin.OriginTrackedValue; |
31 | 23 | import org.springframework.boot.origin.TextResourceOrigin; |
32 | | -import org.springframework.context.ApplicationContext; |
33 | | -import org.springframework.context.ConfigurableApplicationContext; |
34 | 24 | import org.springframework.core.env.ConfigurableEnvironment; |
35 | | -import org.springframework.core.env.MutablePropertySources; |
36 | | -import org.springframework.core.env.PropertiesPropertySource; |
37 | 25 | import org.springframework.core.env.PropertySource; |
38 | 26 | import org.springframework.core.io.FileSystemResource; |
39 | 27 | import org.springframework.core.io.Resource; |
40 | 28 | import org.springframework.stereotype.Service; |
41 | 29 |
|
42 | 30 | @Service |
43 | | -//@ConditionalOnProperty(value = "dynamic.config.autoreload", havingValue = "true") |
44 | 31 | @RequiredArgsConstructor |
45 | 32 | @Slf4j |
46 | 33 | public class ConfigReloadService { |
@@ -101,23 +88,25 @@ public void init() { |
101 | 88 | private void reloadFile(Path path) { |
102 | 89 | log.info("Reloading file {}", path); |
103 | 90 | try { |
104 | | - if (path.toString().endsWith(".yml") || path.toString().endsWith(".yaml")) { |
105 | | - String name = String.format("Config resource 'file [%s] via location '%s'", |
106 | | - path.toAbsolutePath().toString(), |
107 | | - path.toAbsolutePath().toString()); |
108 | | - |
109 | | - List<PropertySource<?>> load = yamlLoader.load(path.toString(), new FileSystemResource(path)); |
110 | | - environment.getPropertySources().remove(name); |
111 | | - environment.getPropertySources().addFirst(load.getFirst()); |
112 | | - Binder binder = Binder.get(environment); |
113 | | - binder.bind("rbac", RoleBasedAccessControlProperties.class).ifBound(bound -> |
114 | | - rbacProperties.setRoles(bound.getRoles()) |
115 | | - ); |
| 91 | + if (!path.toString().endsWith(".yml") && !path.toString().endsWith(".yaml")) { |
| 92 | + log.trace("Skipping non-YML file {}", path); |
116 | 93 | } |
| 94 | + |
| 95 | + String name = String.format("Config resource 'file [%s] via location '%s'", |
| 96 | + path.toAbsolutePath(), |
| 97 | + path.toAbsolutePath()); // TODO extract an obj reference from env |
| 98 | + |
| 99 | + List<PropertySource<?>> load = yamlLoader.load(path.toString(), new FileSystemResource(path)); |
| 100 | + environment.getPropertySources().remove(name); |
| 101 | + environment.getPropertySources().addFirst(load.getFirst()); |
| 102 | + Binder binder = Binder.get(environment); |
| 103 | + |
| 104 | + binder.bind("rbac", RoleBasedAccessControlProperties.class) |
| 105 | + .ifBound(bound -> rbacProperties.setRoles(bound.getRoles()) |
| 106 | + ); |
117 | 107 | } catch (Throwable e) { |
118 | 108 | log.error("Error while reloading file {}", path, e); |
119 | 109 | } |
120 | | - |
121 | 110 | } |
122 | 111 |
|
123 | 112 | @PreDestroy |
|
0 commit comments