Skip to content

Commit

Permalink
Polish PropertySourceProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Aug 5, 2023
1 parent 4b54ca4 commit 1451f30
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,24 @@
*/
public class PropertySourceProcessor {

private static final PropertySourceFactory DEFAULT_PROPERTY_SOURCE_FACTORY = new DefaultPropertySourceFactory();
private static final PropertySourceFactory defaultPropertySourceFactory = new DefaultPropertySourceFactory();

private static final Log logger = LogFactory.getLog(PropertySourceProcessor.class);


private final ConfigurableEnvironment environment;

private final ResourceLoader resourceLoader;

private final List<String> propertySourceNames;
private final List<String> propertySourceNames = new ArrayList<>();


public PropertySourceProcessor(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.environment = environment;
this.resourceLoader = resourceLoader;
this.propertySourceNames = new ArrayList<>();
}


/**
* Process the specified {@link PropertySourceDescriptor} against the
* environment managed by this instance.
Expand All @@ -78,7 +80,7 @@ public void processPropertySource(PropertySourceDescriptor descriptor) throws IO
Assert.isTrue(locations.size() > 0, "At least one @PropertySource(value) location is required");
boolean ignoreResourceNotFound = descriptor.ignoreResourceNotFound();
PropertySourceFactory factory = (descriptor.propertySourceFactory() != null ?
instantiateClass(descriptor.propertySourceFactory()) : DEFAULT_PROPERTY_SOURCE_FACTORY);
instantiateClass(descriptor.propertySourceFactory()) : defaultPropertySourceFactory);

for (String location : locations) {
try {
Expand All @@ -100,13 +102,13 @@ public void processPropertySource(PropertySourceDescriptor descriptor) throws IO
}
}

private void addPropertySource(org.springframework.core.env.PropertySource<?> propertySource) {
private void addPropertySource(PropertySource<?> propertySource) {
String name = propertySource.getName();
MutablePropertySources propertySources = this.environment.getPropertySources();

if (this.propertySourceNames.contains(name)) {
// We've already added a version, we need to extend it
org.springframework.core.env.PropertySource<?> existing = propertySources.get(name);
PropertySource<?> existing = propertySources.get(name);
if (existing != null) {
PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource rps ?
rps.withResourceName() : propertySource);
Expand Down Expand Up @@ -136,7 +138,8 @@ private void addPropertySource(org.springframework.core.env.PropertySource<?> pr
this.propertySourceNames.add(name);
}

private PropertySourceFactory instantiateClass(Class<? extends PropertySourceFactory> type) {

private static PropertySourceFactory instantiateClass(Class<? extends PropertySourceFactory> type) {
try {
Constructor<? extends PropertySourceFactory> constructor = type.getDeclaredConstructor();
ReflectionUtils.makeAccessible(constructor);
Expand Down

0 comments on commit 1451f30

Please sign in to comment.