diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLoader.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLoader.java index 95e947fe..31fbcc83 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLoader.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLoader.java @@ -24,9 +24,10 @@ import org.apache.commons.logging.Log; import org.springframework.boot.context.config.ConfigData; import org.springframework.boot.context.config.ConfigDataLoader; import org.springframework.boot.context.config.ConfigDataLoaderContext; +import org.springframework.boot.context.config.ConfigDataLocation; import org.springframework.boot.context.config.ConfigDataLocationNotFoundException; -public class ConsulConfigDataLoader implements ConfigDataLoader { +public class ConsulConfigDataLoader implements ConfigDataLoader { private final Log log; @@ -35,17 +36,17 @@ public class ConsulConfigDataLoader implements ConfigDataLoader { +public class ConsulConfigDataLocationResolver implements ConfigDataLocationResolver { /** * Consul ConfigData prefix. @@ -64,8 +65,8 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol } @Override - public boolean isResolvable(ConfigDataLocationResolverContext context, String location) { - if (!location.startsWith(PREFIX)) { + public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) { + if (!location.hasPrefix(PREFIX)) { return false; } // only bind if correct prefix @@ -76,14 +77,14 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol } @Override - public List resolve(ConfigDataLocationResolverContext context, String location, - boolean optional) throws ConfigDataLocationNotFoundException { + public List resolve(ConfigDataLocationResolverContext context, + ConfigDataLocation location) throws ConfigDataLocationNotFoundException { return Collections.emptyList(); } @Override - public List resolveProfileSpecific(ConfigDataLocationResolverContext resolverContext, - String location, boolean optional, Profiles profiles) throws ConfigDataLocationNotFoundException { + public List resolveProfileSpecific(ConfigDataLocationResolverContext resolverContext, + ConfigDataLocation location, Profiles profiles) throws ConfigDataLocationNotFoundException { UriComponents locationUri = parseLocation(resolverContext, location); // create consul client @@ -105,8 +106,8 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol registerAndPromoteBean(resolverContext, ConsulConfigIndexes.class, InstanceSupplier.from(ConsulConfigDataIndexes::new)); - return contexts.stream().map(propertySourceContext -> new ConsulConfigDataLocation(propertySourceContext, - optional, properties, consulPropertySources)).collect(Collectors.toList()); + return contexts.stream().map(propertySourceContext -> new ConsulConfigDataResource(propertySourceContext, + location.isOptional(), properties, consulPropertySources)).collect(Collectors.toList()); } private List getCustomContexts(UriComponents uriComponents, ConsulConfigProperties properties) { @@ -132,8 +133,9 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol } @Nullable - protected UriComponents parseLocation(ConfigDataLocationResolverContext context, String location) { - String uri = location.substring(PREFIX.length()); + protected UriComponents parseLocation(ConfigDataLocationResolverContext context, ConfigDataLocation location) { + String originalLocation = location.getNonPrefixedValue(PREFIX); + String uri = originalLocation; if (!StringUtils.hasText(uri)) { return null; } @@ -141,7 +143,7 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol uri = PREFIX + "//" + uri; } else { - uri = location; + uri = originalLocation; } return UriComponentsBuilder.fromUriString(uri).build(); } diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLocation.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataResource.java similarity index 88% rename from spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLocation.java rename to spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataResource.java index a7181c96..29968088 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLocation.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataResource.java @@ -18,10 +18,10 @@ package org.springframework.cloud.consul.config; import java.util.Objects; -import org.springframework.boot.context.config.ConfigDataLocation; +import org.springframework.boot.context.config.ConfigDataResource; import org.springframework.core.style.ToStringCreator; -public class ConsulConfigDataLocation extends ConfigDataLocation { +public class ConsulConfigDataResource extends ConfigDataResource { private final ConsulConfigProperties properties; @@ -31,7 +31,7 @@ public class ConsulConfigDataLocation extends ConfigDataLocation { private final ConsulPropertySources consulPropertySources; - public ConsulConfigDataLocation(String context, boolean optional, ConsulConfigProperties properties, + public ConsulConfigDataResource(String context, boolean optional, ConsulConfigProperties properties, ConsulPropertySources consulPropertySources) { this.properties = properties; this.context = context; @@ -63,7 +63,7 @@ public class ConsulConfigDataLocation extends ConfigDataLocation { if (o == null || getClass() != o.getClass()) { return false; } - ConsulConfigDataLocation that = (ConsulConfigDataLocation) o; + ConsulConfigDataResource that = (ConsulConfigDataResource) o; return this.optional == that.optional && this.context.equals(that.context); } diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolverTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolverTests.java index 9cff992d..e54cbd87 100644 --- a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolverTests.java +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolverTests.java @@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory; import org.junit.jupiter.api.Test; import org.springframework.boot.BootstrapRegistry.InstanceSupplier; +import org.springframework.boot.context.config.ConfigDataLocation; import org.springframework.boot.context.config.ConfigDataLocationResolverContext; import org.springframework.boot.context.config.Profiles; import org.springframework.boot.context.properties.bind.Binder; @@ -41,18 +42,19 @@ public class ConsulConfigDataLocationResolverTests { @Test public void testParseLocation() { ConsulConfigDataLocationResolver resolver = new ConsulConfigDataLocationResolver(LogFactory.getLog(getClass())); - UriComponents uriComponents = resolver.parseLocation(null, "consul:myhost:8501/mypath1;/mypath2;/mypath3"); + UriComponents uriComponents = resolver.parseLocation(null, + ConfigDataLocation.of("consul:myhost:8501/mypath1;/mypath2;/mypath3")); assertThat(uriComponents.toUri()).hasScheme("consul").hasHost("myhost").hasPort(8501) .hasPath("/mypath1;/mypath2;/mypath3"); - uriComponents = resolver.parseLocation(null, "consul:myhost:8501"); + uriComponents = resolver.parseLocation(null, ConfigDataLocation.of("consul:myhost:8501")); assertThat(uriComponents.toUri()).hasScheme("consul").hasHost("myhost").hasPort(8501).hasPath(""); } @Test public void testResolveProfileSpecificWithCustomPaths() { String location = "consul:myhost:8501/mypath1;/mypath2;/mypath3"; - List locations = testResolveProfileSpecific(location); + List locations = testResolveProfileSpecific(location); assertThat(locations).hasSize(3); assertThat(toContexts(locations)).containsExactly("/mypath1/", "/mypath2/", "/mypath3/"); } @@ -60,7 +62,7 @@ public class ConsulConfigDataLocationResolverTests { @Test public void testResolveProfileSpecificWithAutomaticPaths() { String location = "consul:myhost"; - List locations = testResolveProfileSpecific(location); + List locations = testResolveProfileSpecific(location); assertThat(locations).hasSize(4); assertThat(toContexts(locations)).containsExactly("config/testapp,dev/", "config/testapp/", "config/application,dev/", "config/application/"); @@ -74,11 +76,11 @@ public class ConsulConfigDataLocationResolverTests { assertThat(properties.getPort()).isEqualTo(8502); } - private List toContexts(List locations) { - return locations.stream().map(ConsulConfigDataLocation::getContext).collect(Collectors.toList()); + private List toContexts(List locations) { + return locations.stream().map(ConsulConfigDataResource::getContext).collect(Collectors.toList()); } - private List testResolveProfileSpecific(String location) { + private List testResolveProfileSpecific(String location) { ConsulConfigDataLocationResolver resolver = createResolver(); ConfigDataLocationResolverContext context = mock(ConfigDataLocationResolverContext.class); MockEnvironment env = new MockEnvironment(); @@ -86,7 +88,7 @@ public class ConsulConfigDataLocationResolverTests { when(context.getBinder()).thenReturn(Binder.get(env)); Profiles profiles = mock(Profiles.class); when(profiles.getAccepted()).thenReturn(Collections.singletonList("dev")); - return resolver.resolveProfileSpecific(context, location, false, profiles); + return resolver.resolveProfileSpecific(context, ConfigDataLocation.of(location), profiles); } private ConsulConfigDataLocationResolver createResolver() {