diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolver.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolver.java index 6be1a3a0..0a97667d 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolver.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigDataLocationResolver.java @@ -42,7 +42,8 @@ import org.springframework.web.util.UriComponentsBuilder; import static org.springframework.cloud.consul.config.ConsulConfigProperties.Format.FILES; -public class ConsulConfigDataLocationResolver implements ConfigDataLocationResolver { +public class ConsulConfigDataLocationResolver + implements ConfigDataLocationResolver { /** * Consul ConfigData prefix. @@ -55,40 +56,51 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol .unmodifiableList(Arrays.asList(".yml", ".yaml", ".properties")); @Override - public boolean isResolvable(ConfigDataLocationResolverContext context, String location) { - boolean enabled = context.getBinder().bind(ConsulProperties.PREFIX + ".enabled", Boolean.class).orElse(true); - boolean configEnabled = context.getBinder().bind(ConsulConfigProperties.PREFIX + ".enabled", Boolean.class) + public boolean isResolvable(ConfigDataLocationResolverContext context, + String location) { + boolean enabled = context.getBinder() + .bind(ConsulProperties.PREFIX + ".enabled", Boolean.class).orElse(true); + boolean configEnabled = context.getBinder() + .bind(ConsulConfigProperties.PREFIX + ".enabled", Boolean.class) .orElse(true); return location.startsWith(PREFIX) && configEnabled && enabled; } @Override - public List resolve(ConfigDataLocationResolverContext context, String location, - boolean optional) throws ConfigDataLocationNotFoundException { + public List resolve( + ConfigDataLocationResolverContext context, String location, boolean optional) + throws ConfigDataLocationNotFoundException { return Collections.emptyList(); } @Override - public List resolveProfileSpecific(ConfigDataLocationResolverContext context, - String location, boolean optional, Profiles profiles) throws ConfigDataLocationNotFoundException { + public List resolveProfileSpecific( + ConfigDataLocationResolverContext context, String location, boolean optional, + Profiles profiles) throws ConfigDataLocationNotFoundException { UriComponents locationUri = parseLocation(context, location); - ConsulConfigProperties properties = loadConfigProperties(context.getBinder(), locationUri); + ConsulConfigProperties properties = loadConfigProperties(context.getBinder(), + locationUri); - List contexts = (locationUri == null || CollectionUtils.isEmpty(locationUri.getPathSegments())) - ? getAutomaticContexts(profiles, properties) : getCustomContexts(locationUri, properties); + List contexts = (locationUri == null + || CollectionUtils.isEmpty(locationUri.getPathSegments())) + ? getAutomaticContexts(profiles, properties) + : getCustomContexts(locationUri, properties); - registerBean(context, ConsulClient.class, () -> createConsulClient(context, locationUri)); + registerBean(context, ConsulClient.class, + () -> createConsulClient(context, locationUri)); registerBean(context, ConsulConfigIndexes.class, ConsulConfigDataIndexes::new); return contexts.stream() - .map(propertySourceContext -> new ConsulConfigDataLocation(properties, propertySourceContext, optional)) + .map(propertySourceContext -> new ConsulConfigDataLocation(properties, + propertySourceContext, optional)) .collect(Collectors.toList()); } - private List getCustomContexts(UriComponents uriComponents, ConsulConfigProperties properties) { + private List getCustomContexts(UriComponents uriComponents, + ConsulConfigProperties properties) { if (StringUtils.isEmpty(uriComponents.getPath())) { return Collections.emptyList(); } @@ -110,7 +122,8 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol return DIR_SUFFIXES; } - protected List getAutomaticContexts(Profiles profiles, ConsulConfigProperties properties) { + protected List getAutomaticContexts(Profiles profiles, + ConsulConfigProperties properties) { List contexts = new ArrayList<>(); String prefix = properties.getPrefix(); @@ -145,15 +158,17 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol } } - protected void addProfiles(List contexts, String baseContext, Profiles profiles, String suffix, - ConsulConfigProperties properties) { + protected void addProfiles(List contexts, String baseContext, + Profiles profiles, String suffix, ConsulConfigProperties properties) { for (String profile : profiles.getAccepted()) { - contexts.add(baseContext + properties.getProfileSeparator() + profile + suffix); + contexts.add( + baseContext + properties.getProfileSeparator() + profile + suffix); } } @Nullable - protected UriComponents parseLocation(ConfigDataLocationResolverContext context, String location) { + protected UriComponents parseLocation(ConfigDataLocationResolverContext context, + String location) { String uri = location.substring(PREFIX.length()); if (!StringUtils.hasText(uri)) { return null; @@ -167,19 +182,23 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol return UriComponentsBuilder.fromUriString(uri).build(); } - protected void registerBean(ConfigDataLocationResolverContext context, Class type, Supplier supplier) { + protected void registerBean(ConfigDataLocationResolverContext context, + Class type, Supplier supplier) { context.getBootstrapRegistry().register(type, supplier) - .onApplicationContextPrepared((ctxt, consulClient) -> ctxt.getBeanFactory() - .registerSingleton("configData" + type.getSimpleName(), consulClient)); + .onApplicationContextPrepared( + (ctxt, consulClient) -> ctxt.getBeanFactory().registerSingleton( + "configData" + type.getSimpleName(), consulClient)); } - protected ConsulClient createConsulClient(ConfigDataLocationResolverContext context, UriComponents location) { + protected ConsulClient createConsulClient(ConfigDataLocationResolverContext context, + UriComponents location) { ConsulProperties properties = loadProperties(context.getBinder(), location); return ConsulAutoConfiguration.createConsulClient(properties); } protected ConsulProperties loadProperties(Binder binder, UriComponents location) { - ConsulProperties consulProperties = binder.bind(ConsulProperties.PREFIX, Bindable.of(ConsulProperties.class)) + ConsulProperties consulProperties = binder + .bind(ConsulProperties.PREFIX, Bindable.of(ConsulProperties.class)) .orElse(new ConsulProperties()); if (location != null) { @@ -194,13 +213,16 @@ public class ConsulConfigDataLocationResolver implements ConfigDataLocationResol return consulProperties; } - protected ConsulConfigProperties loadConfigProperties(Binder binder, UriComponents location) { + protected ConsulConfigProperties loadConfigProperties(Binder binder, + UriComponents location) { ConsulConfigProperties properties = binder - .bind(ConsulConfigProperties.PREFIX, Bindable.of(ConsulConfigProperties.class)) + .bind(ConsulConfigProperties.PREFIX, + Bindable.of(ConsulConfigProperties.class)) .orElse(new ConsulConfigProperties()); if (StringUtils.isEmpty(properties.getName())) { - properties.setName(binder.bind("spring.application.name", String.class).orElse("application")); + properties.setName(binder.bind("spring.application.name", String.class) + .orElse("application")); } return properties; }