1313import java .nio .file .WatchEvent ;
1414import java .nio .file .WatchKey ;
1515import java .nio .file .WatchService ;
16+ import java .util .Arrays ;
17+ import java .util .HashMap ;
18+ import java .util .Iterator ;
19+ import java .util .Map ;
20+ import java .util .Objects ;
21+ import java .util .stream .Collectors ;
1622import lombok .RequiredArgsConstructor ;
1723import lombok .extern .slf4j .Slf4j ;
1824import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
25+ import org .springframework .boot .env .OriginTrackedMapPropertySource ;
26+ import org .springframework .boot .origin .Origin ;
27+ import org .springframework .boot .origin .OriginLookup ;
28+ import org .springframework .context .ApplicationContext ;
29+ import org .springframework .core .env .AbstractEnvironment ;
30+ import org .springframework .core .env .ConfigurableEnvironment ;
31+ import org .springframework .core .env .EnumerablePropertySource ;
32+ import org .springframework .core .env .MapPropertySource ;
33+ import org .springframework .core .env .PropertySource ;
1934import org .springframework .stereotype .Service ;
35+ import org .stringtemplate .v4 .ST ;
2036
2137@ Service
22- @ ConditionalOnProperty (value = "dynamic.config.autoreload" , havingValue = "true" )
38+ // @ConditionalOnProperty(value = "dynamic.config.autoreload", havingValue = "true")
2339@ RequiredArgsConstructor
2440@ Slf4j
2541public class ConfigReloadService {
@@ -34,8 +50,69 @@ public class ConfigReloadService {
3450 private WatchService watchService ;
3551 private Thread watcherThread ;
3652
53+ private final ApplicationContext context ;
54+ private final ConfigurableEnvironment environment ;
55+
3756 @ PostConstruct
3857 public void init () {
58+
59+ /* environment.getPropertySources()
60+ .stream()
61+ .filter(ps -> ps instanceof OriginTrackedMapPropertySource)
62+ .map(ps -> (OriginTrackedMapPropertySource)ps)
63+ .map(ps -> ps.getSource())
64+ .map(source -> source.values())
65+ .map(values -> {
66+ return (HashMap<String, String>) values;
67+ })
68+ // .map(sourceValues -> sourceValues.)
69+ .collect(Collectors.toUnmodifiableList());*/
70+
71+
72+ // =============
73+
74+ /* environment.getPropertySources().stream()
75+ .filter(ps -> ps instanceof EnumerablePropertySource)
76+ .filter(ps -> ps instanceof OriginLookup)
77+ .flatMap(ps -> {
78+ EnumerablePropertySource<?> eps = (EnumerablePropertySource<?>) ps;
79+ OriginLookup<String> lookup = (OriginLookup<String>) ps;
80+ return Arrays.stream(eps.getPropertyNames())
81+ .map(name -> {
82+ Origin origin = lookup.getOrigin(name);
83+ return origin != null ? origin.toString() : null;
84+ });
85+ })
86+ .filter(Objects::nonNull)
87+ .distinct()
88+ .collect(Collectors.toUnmodifiableList());*/
89+
90+ // ===============
91+
92+ /* Map<String, Object> map = new HashMap();
93+ for(Iterator it = ((AbstractEnvironment) environment).getPropertySources().iterator(); it.hasNext(); ) {
94+ PropertySource propertySource = (PropertySource) it.next();
95+ if (propertySource instanceof MapPropertySource) {
96+ map.putAll(((MapPropertySource) propertySource).getSource());
97+ }
98+ }*/
99+
100+ // ====
101+
102+ SpringConfigurableEnvironment properties = new SpringConfigurableEnvironment (springEnv );
103+ SpringConfigurableEnvironment .PropertyInfo info = properties .get ("profile.env" );
104+ assertEquals ("default" , properties .get (info .getValue ());
105+ assertEquals (
106+ "Config resource 'class path resource [application.properties]' via location 'optional:classpath:/'" ,
107+ info .getSourceList .get (0 ));
108+
109+
110+
111+
112+ System .out .println ();
113+ // environment.getPropertySources()
114+ // .stream()
115+
39116 var configPath = dynamicConfigOperations .dynamicConfigFilePath ();
40117 if (!Files .exists (configPath ) || !Files .isReadable (configPath )) {
41118 log .warn ("Dynamic config file {} doesnt exist or is not readable. Auto reload is disabled" , configPath );
0 commit comments