Improve ConfigData processing code

Refactor `ConfigData` processing code to make it less awkward to
follow.

Prior to this commit the `ConfigDataLocationResolver` would take a
String location and return a `ConfigDataLocation` instance. This was
a little confusing since sometimes we would refer to `location` as the
String value, and sometimes it would be the typed instance. We also
had nowhere sensible to put the `optional:` prefix logic and we needed
to pass a `boolean` parameter to a number of methods. The recently
introduced `Orgin` support also didn't have a good home.

To solve this, `ConfigDataLocation` has been renamed to
`ConfigDataResource`. This frees up `ConfigDataLocation` to be used
as a richer `location` type that holds the String value, the `Orgin`
and provides a home for the `optional:` logic.

This commit also cleans up a few other areas of the code, including
renaming `ResourceConfigData...` to `StandardConfigData...`. It also
introduces a new exception hierarchy for `ConfigDataNotFoundExceptions`.

Closes gh-23711
This commit is contained in:
Phillip Webb
2020-10-13 14:43:34 -07:00
parent f89b99bdbc
commit 1cf9fc107e
65 changed files with 2080 additions and 1488 deletions

View File

@@ -36,7 +36,7 @@ import org.springframework.core.env.PropertySource;
*
* @author Phillip Webb
*/
class SubversionConfigDataLoader implements ConfigDataLoader<SubversionConfigDataLocation> {
class SubversionConfigDataLoader implements ConfigDataLoader<SubversionConfigDataResource> {
private static final ApplicationListener<BootstrapContextClosedEvent> closeListener = SubversionConfigDataLoader::onBootstrapContextClosed;
@@ -50,12 +50,12 @@ class SubversionConfigDataLoader implements ConfigDataLoader<SubversionConfigDat
}
@Override
public ConfigData load(ConfigDataLoaderContext context, SubversionConfigDataLocation location)
public ConfigData load(ConfigDataLoaderContext context, SubversionConfigDataResource resource)
throws IOException, ConfigDataLocationNotFoundException {
context.getBootstrapContext().registerIfAbsent(SubversionServerCertificate.class,
InstanceSupplier.of(location.getServerCertificate()));
InstanceSupplier.of(resource.getServerCertificate()));
SubversionClient client = context.getBootstrapContext().get(SubversionClient.class);
String loaded = client.load(location.getLocation());
String loaded = client.load(resource.getLocation());
PropertySource<?> propertySource = new MapPropertySource("svn", Collections.singletonMap("svn", loaded));
return new ConfigData(Collections.singleton(propertySource));
}

View File

@@ -19,30 +19,33 @@ package smoketest.bootstrapregistry.external.svn;
import java.util.Collections;
import java.util.List;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataLocationNotFoundException;
import org.springframework.boot.context.config.ConfigDataLocationResolver;
import org.springframework.boot.context.config.ConfigDataLocationResolverContext;
import org.springframework.boot.context.config.ConfigDataResourceNotFoundException;
/**
* {@link ConfigDataLocationResolver} for subversion.
*
* @author Phillip Webb
*/
class SubversionConfigDataLocationResolver implements ConfigDataLocationResolver<SubversionConfigDataLocation> {
class SubversionConfigDataLocationResolver implements ConfigDataLocationResolver<SubversionConfigDataResource> {
private static final String PREFIX = "svn:";
@Override
public boolean isResolvable(ConfigDataLocationResolverContext context, String location) {
return location.startsWith(PREFIX);
public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDataLocation location) {
return location.hasPrefix(PREFIX);
}
@Override
public List<SubversionConfigDataLocation> resolve(ConfigDataLocationResolverContext context, String location,
boolean optional) throws ConfigDataLocationNotFoundException {
public List<SubversionConfigDataResource> resolve(ConfigDataLocationResolverContext context,
ConfigDataLocation location)
throws ConfigDataLocationNotFoundException, ConfigDataResourceNotFoundException {
String serverCertificate = context.getBinder().bind("spring.svn.server.certificate", String.class).orElse(null);
return Collections.singletonList(
new SubversionConfigDataLocation(location.substring(PREFIX.length()), serverCertificate));
new SubversionConfigDataResource(location.getNonPrefixedValue(PREFIX), serverCertificate));
}
}

View File

@@ -16,20 +16,20 @@
package smoketest.bootstrapregistry.external.svn;
import org.springframework.boot.context.config.ConfigDataLocation;
import org.springframework.boot.context.config.ConfigDataResource;
/**
* A subversion {@link ConfigDataLocation}.
* A subversion {@link ConfigDataResource}.
*
* @author Phillip Webb
*/
class SubversionConfigDataLocation extends ConfigDataLocation {
class SubversionConfigDataResource extends ConfigDataResource {
private final String location;
private final SubversionServerCertificate serverCertificate;
SubversionConfigDataLocation(String location, String serverCertificate) {
SubversionConfigDataResource(String location, String serverCertificate) {
this.location = location;
this.serverCertificate = SubversionServerCertificate.of(serverCertificate);
}
@@ -50,7 +50,7 @@ class SubversionConfigDataLocation extends ConfigDataLocation {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
SubversionConfigDataLocation other = (SubversionConfigDataLocation) obj;
SubversionConfigDataResource other = (SubversionConfigDataResource) obj;
return this.location.equals(other.location);
}