11package io .kafbat .ui .service .app ;
22
3+ import io .kafbat .ui .config .auth .RoleBasedAccessControlProperties ;
4+ import io .kafbat .ui .service .rbac .AccessControlService ;
35import io .kafbat .ui .util .MultiFileWatcher ;
46import jakarta .annotation .PostConstruct ;
57import jakarta .annotation .PreDestroy ;
68import java .io .IOException ;
9+ import java .io .InputStream ;
10+ import java .nio .file .Files ;
711import java .nio .file .Paths ;
812import java .util .LinkedHashSet ;
913import java .util .Objects ;
14+ import java .util .Properties ;
1015import java .util .stream .Collectors ;
1116import java .util .stream .Stream ;
1217import java .util .stream .StreamSupport ;
18+ import lombok .Cleanup ;
1319import lombok .RequiredArgsConstructor ;
1420import lombok .extern .slf4j .Slf4j ;
21+ import org .springframework .beans .factory .ObjectProvider ;
22+ import org .springframework .beans .factory .support .DefaultSingletonBeanRegistry ;
23+ import org .springframework .boot .context .properties .bind .Binder ;
1524import org .springframework .boot .env .OriginTrackedMapPropertySource ;
1625import org .springframework .boot .origin .Origin ;
1726import org .springframework .boot .origin .OriginTrackedValue ;
1827import org .springframework .boot .origin .TextResourceOrigin ;
28+ import org .springframework .context .ApplicationContext ;
1929import org .springframework .core .env .ConfigurableEnvironment ;
30+ import org .springframework .core .env .MutablePropertySources ;
31+ import org .springframework .core .env .PropertiesPropertySource ;
32+ import org .springframework .core .env .PropertySource ;
2033import org .springframework .core .io .Resource ;
2134import org .springframework .stereotype .Service ;
2235
@@ -29,10 +42,14 @@ public class ConfigReloadService {
2942 private static final String THREAD_NAME = "config-watcher-thread" ;
3043
3144 private final ConfigurableEnvironment environment ;
45+ private final ApplicationContext appContext ;
3246
3347 private Thread watcherThread ;
3448 private MultiFileWatcher multiFileWatcher ;
3549
50+ private final ObjectProvider <AccessControlService > accessControlService ;
51+ private final ObjectProvider <RoleBasedAccessControlProperties > roleBasedAccessControlProperties ;
52+
3653 @ PostConstruct
3754 public void init () {
3855 var propertySourcePaths = StreamSupport .stream (environment .getPropertySources ().spliterator (), false )
@@ -69,7 +86,28 @@ public void init() {
6986 log .debug ("Auto reload is enabled, will watch for config changes" );
7087
7188 try {
72- this .multiFileWatcher = new MultiFileWatcher (propertySourcePaths , this ::reload );
89+ this .multiFileWatcher = new MultiFileWatcher (propertySourcePaths , path -> {
90+ System .out .println (path );
91+ var propertySources = environment .getPropertySources ();
92+
93+
94+
95+ Properties properties = new Properties ();
96+ try {
97+ @ Cleanup InputStream inputStream = Files .newInputStream (Paths .get ("/tmp/kek.yaml" ));
98+ properties .load (inputStream );
99+ } catch (IOException e ) {
100+ throw new RuntimeException (e );
101+ }
102+
103+ PropertySource <?> origin =
104+ propertySources .stream ().filter (ps -> ps .getName ().contains ("tmp/kek" )).findFirst ().get ();
105+ environment .getPropertySources ().replace (origin .getName (), new PropertiesPropertySource (origin .getName (), properties ));
106+
107+ System .out .println ();
108+ var kekw = appContext .getBean (AccessControlService .class );
109+ return null ;
110+ });
73111 this .watcherThread = new Thread (multiFileWatcher ::watchLoop , THREAD_NAME );
74112 this .watcherThread .start ();
75113 } catch (IOException e ) {
@@ -91,6 +129,19 @@ public void shutdown() {
91129 }
92130
93131 private void reload () {
132+ var registry = (DefaultSingletonBeanRegistry ) appContext .getAutowireCapableBeanFactory ();
133+
134+ registry .destroySingleton ("AccessControlService" );
135+
136+ Binder .get (environment )
137+ .bind ("rbac" , RoleBasedAccessControlProperties .class )
138+ .orElseThrow (() -> new IllegalStateException ("no rbac config" ));
139+
140+ var newProps = appContext .getBean (AccessControlService .class );
141+ newProps .init ();
142+ // accessControlService.init();
143+ System .out .println ();
144+
94145
95146 }
96147
0 commit comments