|
21 | 21 | import java.util.List; |
22 | 22 | import java.util.Map; |
23 | 23 | import java.util.concurrent.Executors; |
24 | | -import java.util.stream.StreamSupport; |
25 | 24 |
|
26 | 25 | /** |
27 | 26 | * Enhance PropertySource when spring.config.location is specified, it will start directory-watch, |
@@ -72,32 +71,35 @@ public void watchConfigDirectory() { |
72 | 71 | return; |
73 | 72 | } |
74 | 73 | MutablePropertySources propertySources = env.getPropertySources(); |
75 | | - StreamSupport.stream(propertySources.spliterator(), false) |
76 | | - .filter(ps -> ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN) || ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN_LEGACY)) |
77 | | - .forEach(ps -> { |
78 | | - String name = ps.getName(); |
79 | | - int beginIndex = name.indexOf("[") + 1; |
80 | | - int endIndex = name.indexOf("]"); |
81 | | - if (beginIndex < 1 && endIndex < 1) { |
82 | | - log.warn("unrecognized config location, property source name is: {}", name); |
83 | | - } |
84 | | - String pathStr = name.substring(beginIndex, endIndex); |
85 | | - if (pathStr.contains(FILE_COLON_SYMBOL)) { |
86 | | - pathStr = pathStr.replace(FILE_COLON_SYMBOL, ""); |
87 | | - } |
88 | | - PROPERTY_SOURCE_META_MAP.put(pathStr.replaceAll("\\\\", "/"), new PropertySourceMeta(ps, Paths.get(pathStr), 0L)); |
89 | | - if (log.isDebugEnabled()) { |
90 | | - log.debug("configuration file found: {}", pathStr); |
91 | | - } |
92 | | - }); |
| 74 | + for (PropertySource<?> ps : propertySources) { |
| 75 | + boolean isFilePropSource = ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN_LEGACY) || ps.getName().matches(FILE_SOURCE_CONFIGURATION_PATTERN); |
| 76 | + if (isFilePropSource) { |
| 77 | + normalizeAndRecordPropSource(ps); |
| 78 | + } |
| 79 | + } |
93 | 80 | Executors.newSingleThreadExecutor(r -> new Thread(r, "config-watcher")).submit(this::startWatchDir); |
94 | 81 | } |
95 | 82 |
|
| 83 | + private void normalizeAndRecordPropSource(PropertySource<?> ps) { |
| 84 | + String name = ps.getName(); |
| 85 | + int beginIndex = name.indexOf("[") + 1; |
| 86 | + int endIndex = name.indexOf("]"); |
| 87 | + if (beginIndex < 1 && endIndex < 1) { |
| 88 | + log.warn("unrecognized config location, property source name is: {}", name); |
| 89 | + } |
| 90 | + String pathStr = name.substring(beginIndex, endIndex); |
| 91 | + if (pathStr.contains(FILE_COLON_SYMBOL)) { |
| 92 | + pathStr = pathStr.replace(FILE_COLON_SYMBOL, ""); |
| 93 | + } |
| 94 | + PROPERTY_SOURCE_META_MAP.put(pathStr.replaceAll("\\\\", "/"), new PropertySourceMeta(ps, Paths.get(pathStr), 0L)); |
| 95 | + log.debug("configuration file found: {}", pathStr); |
| 96 | + } |
| 97 | + |
96 | 98 | private void startWatchDir() { |
97 | 99 | try { |
98 | 100 | log.info("start watching configuration directory: {}", configLocation); |
99 | 101 | watchService = FileSystems.getDefault().newWatchService(); |
100 | | - Paths.get(configLocation).register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); |
| 102 | + Paths.get(configLocation).register(watchService, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE); |
101 | 103 | WatchKey key; |
102 | 104 | while ((key = watchService.take()) != null) { |
103 | 105 | // avoid receiving two ENTRY_MODIFY events: file modified and timestamp updated |
|
0 commit comments