Moves defaulting app name to loadConfigProperties.
Fixes NPE if locationUri is null. Various polishing.
This commit is contained in:
@@ -34,6 +34,7 @@ import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.cloud.consul.ConsulAutoConfiguration;
|
||||
import org.springframework.cloud.consul.ConsulProperties;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
@@ -41,8 +42,12 @@ import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import static org.springframework.cloud.consul.config.ConsulConfigProperties.Format.FILES;
|
||||
|
||||
public class ConsulConfigDataLocationResolver
|
||||
implements ConfigDataLocationResolver<ConsulConfigDataLocation> {
|
||||
public class ConsulConfigDataLocationResolver implements ConfigDataLocationResolver<ConsulConfigDataLocation> {
|
||||
|
||||
/**
|
||||
* Consul ConfigData prefix.
|
||||
*/
|
||||
public static final String PREFIX = "consul:";
|
||||
|
||||
protected static final List<String> DIR_SUFFIXES = Collections.singletonList("/");
|
||||
|
||||
@@ -50,61 +55,45 @@ public class ConsulConfigDataLocationResolver
|
||||
.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("consul:") && configEnabled && enabled;
|
||||
return location.startsWith(PREFIX) && configEnabled && enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConsulConfigDataLocation> resolve(
|
||||
ConfigDataLocationResolverContext context, String location, boolean optional)
|
||||
throws ConfigDataLocationNotFoundException {
|
||||
public List<ConsulConfigDataLocation> resolve(ConfigDataLocationResolverContext context, String location,
|
||||
boolean optional) throws ConfigDataLocationNotFoundException {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConsulConfigDataLocation> resolveProfileSpecific(
|
||||
ConfigDataLocationResolverContext context, String location, boolean optional,
|
||||
Profiles profiles) throws ConfigDataLocationNotFoundException {
|
||||
public List<ConsulConfigDataLocation> 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);
|
||||
|
||||
String appName = properties.getName();
|
||||
if (StringUtils.isEmpty(appName)) {
|
||||
appName = context.getBinder().bind("spring.application.name", String.class)
|
||||
.orElse("application");
|
||||
}
|
||||
List<String> contexts = (locationUri == null || CollectionUtils.isEmpty(locationUri.getPathSegments()))
|
||||
? getAutomaticContexts(profiles, properties) : getCustomContexts(locationUri, properties);
|
||||
|
||||
List<String> contexts = (CollectionUtils.isEmpty(locationUri.getPathSegments()))
|
||||
? getAutomaticContexts(profiles, properties, appName)
|
||||
: 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<String> getCustomContexts(UriComponents uriComponents,
|
||||
ConsulConfigProperties properties) {
|
||||
List<String> contexts = new ArrayList<>();
|
||||
private List<String> getCustomContexts(UriComponents uriComponents, ConsulConfigProperties properties) {
|
||||
if (StringUtils.isEmpty(uriComponents.getPath())) {
|
||||
return contexts;
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
List<String> contexts = new ArrayList<>();
|
||||
for (String path : uriComponents.getPath().split(";")) {
|
||||
for (String suffix : getSuffixes(properties)) {
|
||||
contexts.add(path + suffix);
|
||||
@@ -121,8 +110,7 @@ public class ConsulConfigDataLocationResolver
|
||||
return DIR_SUFFIXES;
|
||||
}
|
||||
|
||||
protected List<String> getAutomaticContexts(Profiles profiles,
|
||||
ConsulConfigProperties properties, String appName) {
|
||||
protected List<String> getAutomaticContexts(Profiles profiles, ConsulConfigProperties properties) {
|
||||
List<String> contexts = new ArrayList<>();
|
||||
|
||||
String prefix = properties.getPrefix();
|
||||
@@ -134,7 +122,8 @@ public class ConsulConfigDataLocationResolver
|
||||
addProfiles(contexts, defaultContext, profiles, suffix, properties);
|
||||
}
|
||||
|
||||
String baseContext = getContext(prefix, appName);
|
||||
// getName() defaults to ${spring.application.name} or application
|
||||
String baseContext = getContext(prefix, properties.getName());
|
||||
|
||||
for (String suffix : getSuffixes(properties)) {
|
||||
contexts.add(baseContext + suffix);
|
||||
@@ -156,22 +145,21 @@ public class ConsulConfigDataLocationResolver
|
||||
}
|
||||
}
|
||||
|
||||
protected void addProfiles(List<String> contexts, String baseContext,
|
||||
Profiles profiles, String suffix, ConsulConfigProperties properties) {
|
||||
protected void addProfiles(List<String> 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);
|
||||
}
|
||||
}
|
||||
|
||||
protected UriComponents parseLocation(ConfigDataLocationResolverContext context,
|
||||
String location) {
|
||||
String uri = location.substring("consul:".length());
|
||||
@Nullable
|
||||
protected UriComponents parseLocation(ConfigDataLocationResolverContext context, String location) {
|
||||
String uri = location.substring(PREFIX.length());
|
||||
if (!StringUtils.hasText(uri)) {
|
||||
return null;
|
||||
}
|
||||
if (!uri.startsWith("//")) {
|
||||
uri = "consul:" + "//" + uri;
|
||||
uri = PREFIX + "//" + uri;
|
||||
}
|
||||
else {
|
||||
uri = location;
|
||||
@@ -179,41 +167,42 @@ public class ConsulConfigDataLocationResolver
|
||||
return UriComponentsBuilder.fromUriString(uri).build();
|
||||
}
|
||||
|
||||
protected <T> void registerBean(ConfigDataLocationResolverContext context,
|
||||
Class<T> type, Supplier<T> supplier) {
|
||||
protected <T> void registerBean(ConfigDataLocationResolverContext context, Class<T> type, Supplier<T> 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 (StringUtils.hasText(location.getHost())) {
|
||||
consulProperties.setHost(location.getHost());
|
||||
}
|
||||
if (location.getPort() >= 0) {
|
||||
consulProperties.setPort(location.getPort());
|
||||
if (location != null) {
|
||||
if (StringUtils.hasText(location.getHost())) {
|
||||
consulProperties.setHost(location.getHost());
|
||||
}
|
||||
if (location.getPort() >= 0) {
|
||||
consulProperties.setPort(location.getPort());
|
||||
}
|
||||
}
|
||||
|
||||
return consulProperties;
|
||||
}
|
||||
|
||||
protected ConsulConfigProperties loadConfigProperties(Binder binder,
|
||||
UriComponents location) {
|
||||
return binder
|
||||
.bind(ConsulConfigProperties.PREFIX,
|
||||
Bindable.of(ConsulConfigProperties.class))
|
||||
protected ConsulConfigProperties loadConfigProperties(Binder binder, UriComponents location) {
|
||||
ConsulConfigProperties properties = binder
|
||||
.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"));
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
protected static class ConsulConfigDataIndexes implements ConsulConfigIndexes {
|
||||
|
||||
@@ -89,7 +89,7 @@ public class ConsulPropertySourceLocator
|
||||
|
||||
String appName = this.properties.getName();
|
||||
if (StringUtils.isEmpty(appName)) {
|
||||
appName = env.getProperty("spring.application.name");
|
||||
appName = env.getProperty("spring.application.name", "application");
|
||||
}
|
||||
|
||||
List<String> profiles = Arrays.asList(env.getActiveProfiles());
|
||||
|
||||
Reference in New Issue
Block a user