Skip to content

Commit f5a284f

Browse files
hutiefang76hutiefang
andauthored
Fix native file monitor relative URI handling (#3258)
Fixes gh-2997 Signed-off-by: hutiefang <hutiefang@qq.com> Co-authored-by: hutiefang <hutiefang@qq.com>
1 parent 6dfbfd3 commit f5a284f

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

spring-cloud-config-monitor/src/main/java/org/springframework/cloud/config/monitor/FileMonitorConfiguration.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private Set<Path> getFileRepo() {
202202
}
203203
Resource resource = this.resourceLoader.getResource(repositoryUri);
204204
if (resource instanceof FileSystemResource || resource instanceof FileUrlResource) {
205-
paths.add(Paths.get(resource.getURI()));
205+
paths.add(resolvePath(resource));
206206
}
207207
}
208208
return paths;
@@ -217,7 +217,7 @@ private Set<Path> getFileRepo() {
217217
Resource resource = this.resourceLoader.getResource(path);
218218
if (resource.exists()) {
219219
try {
220-
paths.add(Paths.get(resource.getURI()));
220+
paths.add(resolvePath(resource));
221221
}
222222
catch (Exception e) {
223223
log.error("Cannot resolve URI for path: " + path);
@@ -229,6 +229,13 @@ private Set<Path> getFileRepo() {
229229
return null;
230230
}
231231

232+
private Path resolvePath(Resource resource) throws IOException {
233+
if (resource instanceof FileSystemResource || resource instanceof FileUrlResource) {
234+
return resource.getFile().toPath().toAbsolutePath().normalize();
235+
}
236+
return Paths.get(resource.getURI());
237+
}
238+
232239
private Set<File> filesFromEvents() {
233240
Set<File> files = new LinkedHashSet<File>();
234241
if (this.watcher == null) {

spring-cloud-config-monitor/src/test/java/org/springframework/cloud/config/monitor/FileMonitorConfigurationTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package org.springframework.cloud.config.monitor;
1818

19+
import java.nio.file.Files;
1920
import java.nio.file.Path;
21+
import java.nio.file.Paths;
2022
import java.util.ArrayList;
2123
import java.util.Arrays;
2224
import java.util.List;
@@ -26,6 +28,7 @@
2628
import org.junit.jupiter.api.AfterEach;
2729
import org.junit.jupiter.api.BeforeEach;
2830
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.io.TempDir;
2932

3033
import org.springframework.cloud.config.server.environment.AbstractScmEnvironmentRepository;
3134
import org.springframework.cloud.config.server.environment.JGitEnvironmentProperties;
@@ -54,6 +57,9 @@ public class FileMonitorConfigurationTest {
5457

5558
private List<AbstractScmEnvironmentRepository> repositories = new ArrayList<>();
5659

60+
@TempDir
61+
private Path tempDir;
62+
5763
@BeforeEach
5864
public void setup() {
5965
fileMonitorConfiguration.setResourceLoader(new FileSystemResourceLoader());
@@ -89,6 +95,22 @@ public void testStart_withNativeEnvironmentRepository() {
8995
assertOnDirectory(1);
9096
}
9197

98+
@Test
99+
public void testStart_withNativeEnvironmentRepositoryAndRelativeFileUri() throws Exception {
100+
// given
101+
Path relativeLocation = Paths.get("target", "file-monitor", tempDir.getFileName().toString(), "config.d");
102+
Files.createDirectories(relativeLocation);
103+
NativeEnvironmentRepository repository = createNativeEnvironmentRepository("file:" + relativeLocation);
104+
ReflectionTestUtils.setField(fileMonitorConfiguration, "nativeEnvironmentRepository", repository);
105+
106+
// when
107+
fileMonitorConfiguration.start();
108+
109+
// then
110+
Set<Path> directory = getDirectory();
111+
assertThat(directory).containsExactly(relativeLocation.toAbsolutePath());
112+
}
113+
92114
@Test
93115
public void testStart_withOneScmRepository() {
94116
// given
@@ -203,9 +225,13 @@ private void addScmRepository(AbstractScmEnvironmentRepository... repository) {
203225
}
204226

205227
private NativeEnvironmentRepository createNativeEnvironmentRepository() {
228+
return createNativeEnvironmentRepository("classpath:pathsamples");
229+
}
230+
231+
private NativeEnvironmentRepository createNativeEnvironmentRepository(String... searchLocations) {
206232
ConfigurableEnvironment environment = createConfigurableEnvironment();
207233
NativeEnvironmentProperties properties = new NativeEnvironmentProperties();
208-
properties.setSearchLocations(new String[] { "classpath:pathsamples" });
234+
properties.setSearchLocations(searchLocations);
209235
return new NativeEnvironmentRepository(environment, properties, ObservationRegistry.NOOP);
210236
}
211237

0 commit comments

Comments
 (0)