diff --git a/pom.xml b/pom.xml index 2e1ac898..da3fec6d 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,13 @@ + + org.springframework.boot + spring-boot-dependencies + 2.4.2-SNAPSHOT + pom + import + org.springframework.cloud spring-cloud-config-dependencies diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java index 1df63fc8..eeb9a4ff 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java @@ -21,6 +21,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -28,6 +29,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; +import org.springframework.boot.context.config.ConfigDataEnvironmentUpdateListener; +import org.springframework.boot.context.config.ConfigDataLocation; +import org.springframework.boot.context.config.ConfigDataResource; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.environment.PropertySource; import org.springframework.core.NestedExceptionUtils; @@ -130,11 +134,12 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc try { ConfigurableEnvironment environment = getEnvironment(config, profile, label); DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); + TrackingConfigDataEnvironmentUpdateListener listener = new TrackingConfigDataEnvironmentUpdateListener(); ConfigDataEnvironmentPostProcessor.applyTo(environment, resourceLoader, null, - StringUtils.commaDelimitedListToStringArray(profile)); + StringUtils.commaDelimitedListToSet(profile), listener); environment.getPropertySources().remove("config-data-setup"); - return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label, includeOrigin)); + return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label, includeOrigin), listener); } catch (Exception e) { String msg = String.format("Could not construct context for config=%s profile=%s label=%s includeOrigin=%b", @@ -217,7 +222,7 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc return environment; } - protected Environment clean(Environment value) { + protected Environment clean(Environment value, TrackingConfigDataEnvironmentUpdateListener listener) { Environment result = new Environment(value.getName(), value.getProfiles(), value.getLabel(), this.version, value.getState()); for (PropertySource source : value.getPropertySources()) { @@ -225,11 +230,18 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc if (this.environment.getPropertySources().contains(name)) { continue; } - Matcher matcher = RESOURCE_PATTERN.matcher(name); String location = null; - if (matcher.find()) { - name = matcher.group(1); - location = matcher.group(2); + if (listener != null && listener.getEntries().containsKey(name)) { + Entry entry = listener.getEntries().get(name); + name = entry.resource.toString(); + location = entry.location.toString(); + } + else { + Matcher matcher = RESOURCE_PATTERN.matcher(name); + if (matcher.find()) { + name = matcher.group(1); + location = matcher.group(2); + } } // TODO: needed anymore? name = name.replace("applicationConfig: [", ""); @@ -258,12 +270,13 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc matches = true; break; } - if (location.startsWith("file:")) { - location = StringUtils - .cleanPath(new File(location.substring("file:".length())).getAbsolutePath()) + "/"; + String locationToTest = location; + if (locationToTest.startsWith("file:")) { + locationToTest = StringUtils + .cleanPath(new File(locationToTest.substring("file:".length())).getAbsolutePath()) + "/"; } - if (location != null && location.startsWith(pattern) - && !location.substring(pattern.length()).contains("/")) { + if (locationToTest != null && locationToTest.startsWith(pattern) + && !locationToTest.substring(pattern.length()).contains("/")) { matches = true; break; } @@ -321,4 +334,36 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc this.order = order; } + private class TrackingConfigDataEnvironmentUpdateListener implements ConfigDataEnvironmentUpdateListener { + + private Map entries = new ConcurrentHashMap<>(); + + @Override + public void onPropertySourceAdded(org.springframework.core.env.PropertySource propertySource, ConfigDataLocation location, ConfigDataResource resource) { + entries.put(propertySource.getName(), new Entry(location, resource)); + } + + public Map getEntries() { + return this.entries; + } + } + + private class Entry { + private final ConfigDataLocation location; + private final ConfigDataResource resource; + + private Entry(ConfigDataLocation location, ConfigDataResource resource) { + this.location = location; + this.resource = resource; + } + + public ConfigDataLocation getLocation() { + return this.location; + } + + public ConfigDataResource getResource() { + return this.resource; + } + } + } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java index 60f094d6..630b019a 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java @@ -98,6 +98,7 @@ public class NativeEnvironmentRepositoryTests { environment.add(new PropertySource( "Config resource 'file [/tmp/config-repo-7780026223759117699/application-dev.yml]' via location 'file:/tmp/config-repo-7780026223759117699/'", Collections.singletonMap("foo", "bar"))); + //environment = repository.clean(environment, null); assertThat(environment.getPropertySources().size()).isEqualTo(1); assertThat(environment.getPropertySources().get(0).getName().contains("application-dev.yml")); } @@ -108,16 +109,19 @@ public class NativeEnvironmentRepositoryTests { environment.add(new PropertySource( "Config resource 'classpath:/configs/application-myprofile.yml' via location 'classpath:/configs/' (document #0)", Collections.singletonMap("foo", "bar"))); + //environment = repository.clean(environment, null); assertThat(environment.getPropertySources().size()).isEqualTo(1); assertThat(environment.getPropertySources().get(0).getName().contains("application-myprofile.yml")); } @Test public void cleanBoot241Classpath() { + //this.repository.setSearchLocations("classpath:/configs"); Environment environment = new Environment("application"); environment.add(new PropertySource( "Config resource 'class path resource [configs/application.yml]' via location 'classpath:/configs/' (document #0)", Collections.singletonMap("foo", "bar"))); + //environment = repository.clean(environment, null); assertThat(environment.getPropertySources().size()).isEqualTo(1); assertThat(environment.getPropertySources().get(0).getName().contains("application-myprofile.yml")); }