Skip to content

Commit 588b91e

Browse files
committed
cleanup
1 parent 21ad391 commit 588b91e

File tree

4 files changed

+25
-40
lines changed

4 files changed

+25
-40
lines changed

api/src/main/java/io/kafbat/ui/config/auth/RoleBasedAccessControlProperties.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
public class RoleBasedAccessControlProperties {
1111

1212
private List<Role> roles = new ArrayList<>();
13-
// private String haha;
1413

1514
@PostConstruct
1615
public void init() {

api/src/main/java/io/kafbat/ui/service/app/ConfigReloadService.java

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,33 @@
11
package io.kafbat.ui.service.app;
22

33
import io.kafbat.ui.config.auth.RoleBasedAccessControlProperties;
4-
import io.kafbat.ui.service.rbac.AccessControlService;
54
import io.kafbat.ui.util.MultiFileWatcher;
65
import jakarta.annotation.PostConstruct;
76
import jakarta.annotation.PreDestroy;
87
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.nio.file.Files;
118
import java.nio.file.Path;
129
import java.nio.file.Paths;
1310
import java.util.LinkedHashSet;
1411
import java.util.List;
1512
import java.util.Objects;
16-
import java.util.Properties;
1713
import java.util.stream.Collectors;
1814
import java.util.stream.Stream;
1915
import java.util.stream.StreamSupport;
20-
import lombok.Cleanup;
2116
import lombok.RequiredArgsConstructor;
2217
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;
2618
import org.springframework.boot.context.properties.bind.Binder;
2719
import org.springframework.boot.env.OriginTrackedMapPropertySource;
2820
import org.springframework.boot.env.YamlPropertySourceLoader;
2921
import org.springframework.boot.origin.Origin;
3022
import org.springframework.boot.origin.OriginTrackedValue;
3123
import org.springframework.boot.origin.TextResourceOrigin;
32-
import org.springframework.context.ApplicationContext;
33-
import org.springframework.context.ConfigurableApplicationContext;
3424
import org.springframework.core.env.ConfigurableEnvironment;
35-
import org.springframework.core.env.MutablePropertySources;
36-
import org.springframework.core.env.PropertiesPropertySource;
3725
import org.springframework.core.env.PropertySource;
3826
import org.springframework.core.io.FileSystemResource;
3927
import org.springframework.core.io.Resource;
4028
import org.springframework.stereotype.Service;
4129

4230
@Service
43-
//@ConditionalOnProperty(value = "dynamic.config.autoreload", havingValue = "true")
4431
@RequiredArgsConstructor
4532
@Slf4j
4633
public class ConfigReloadService {
@@ -101,23 +88,25 @@ public void init() {
10188
private void reloadFile(Path path) {
10289
log.info("Reloading file {}", path);
10390
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);
11693
}
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+
);
117107
} catch (Throwable e) {
118108
log.error("Error while reloading file {}", path, e);
119109
}
120-
121110
}
122111

123112
@PreDestroy

api/src/main/java/io/kafbat/ui/service/rbac/AccessControlService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import lombok.extern.slf4j.Slf4j;
3333
import org.apache.commons.collections.CollectionUtils;
3434
import org.apache.commons.lang3.StringUtils;
35-
import org.springframework.beans.factory.ObjectProvider;
3635
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3736
import org.springframework.core.env.Environment;
3837
import org.springframework.security.access.AccessDeniedException;

api/src/main/java/io/kafbat/ui/util/MultiFileWatcher.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
import java.nio.file.WatchEvent;
1414
import java.nio.file.WatchKey;
1515
import java.nio.file.WatchService;
16-
import java.time.Duration;
1716
import java.util.Collection;
1817
import java.util.HashMap;
19-
import java.util.List;
2018
import java.util.Map;
2119
import java.util.Set;
2220
import java.util.concurrent.ConcurrentHashMap;
@@ -27,15 +25,11 @@
2725
@Slf4j
2826
public final class MultiFileWatcher implements AutoCloseable {
2927

30-
private static final long DEBOUNCE_MS = Duration.ofMillis(1000).toMillis();
31-
3228
private final WatchService watchService = FileSystems.getDefault().newWatchService();
3329
private final Set<URI> watchedFiles = ConcurrentHashMap.newKeySet();
3430
private final Map<WatchKey, Path> watchDirsByKey = new HashMap<>();
3531
private final Consumer<Path> reloader;
3632

37-
private long lastTriggerAt = 0;
38-
3933
public MultiFileWatcher(Collection<Path> filesToWatch, Consumer<Path> reloader) throws IOException {
4034
Assert.notNull(reloader, "reloader must not be null");
4135
this.reloader = reloader;
@@ -45,11 +39,11 @@ public MultiFileWatcher(Collection<Path> filesToWatch, Consumer<Path> reloader)
4539
}
4640

4741

48-
List<Path> directories = filesToWatch.stream().map(Path::getParent).distinct().toList();
42+
var directories = filesToWatch.stream().map(Path::getParent).distinct().toList();
4943
watchedFiles.addAll(filesToWatch.stream()
50-
.map(p -> p.toAbsolutePath().normalize())
51-
.map(Path::toUri)
52-
.toList()
44+
.map(p -> p.toAbsolutePath().normalize())
45+
.map(Path::toUri)
46+
.toList()
5347
);
5448

5549
if (watchedFiles.isEmpty()) {
@@ -63,21 +57,25 @@ public MultiFileWatcher(Collection<Path> filesToWatch, Consumer<Path> reloader)
6357
directories
6458
.forEach(dir -> {
6559
try {
66-
WatchKey key = dir.register(watchService, ENTRY_MODIFY, ENTRY_CREATE, ENTRY_DELETE);
60+
var key = dir.register(watchService, ENTRY_MODIFY, ENTRY_CREATE, ENTRY_DELETE);
6761
watchDirsByKey.put(key, dir);
6862
} catch (IOException e) {
6963
throw new UncheckedIOException(e);
7064
}
7165
});
7266

73-
// log.trace("Watching directories: {}", watchDirsByKey.values().stream().map(a -> getParentPath().apply(a)).map(Path::toString).toList());
67+
log.trace("Watching directories: {}", directories.stream().map(Path::toString).toList());
7468
}
7569

7670
public void watchLoop() {
7771
while (true) {
7872
try {
7973
var key = watchService.take();
8074
Path dir = watchDirsByKey.get(key);
75+
if (dir == null) {
76+
continue;
77+
}
78+
8179
for (WatchEvent<?> event : key.pollEvents()) {
8280
Path relativePath = (Path) event.context();
8381
Path path = dir.resolve(relativePath);

0 commit comments

Comments
 (0)