Skip to content

Commit e207e7c

Browse files
committed
Polish EnvConfigData
Rename classes to align with existing `SystemEnvironment...` classes and extract common `FileExtensionHint` logic. See gh-41609
1 parent aaaeb1e commit e207e7c

14 files changed

+455
-305
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataResourceNotFoundException.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private static String getReferenceDescription(ConfigDataResource resource, Confi
116116
* @param pathToCheck the path to check
117117
*/
118118
public static void throwIfDoesNotExist(ConfigDataResource resource, Path pathToCheck) {
119-
throwIfDoesNotExist(resource, Files.exists(pathToCheck));
119+
throwIfNot(resource, Files.exists(pathToCheck));
120120
}
121121

122122
/**
@@ -126,7 +126,7 @@ public static void throwIfDoesNotExist(ConfigDataResource resource, Path pathToC
126126
* @param fileToCheck the file to check
127127
*/
128128
public static void throwIfDoesNotExist(ConfigDataResource resource, File fileToCheck) {
129-
throwIfDoesNotExist(resource, fileToCheck.exists());
129+
throwIfNot(resource, fileToCheck.exists());
130130
}
131131

132132
/**
@@ -136,11 +136,11 @@ public static void throwIfDoesNotExist(ConfigDataResource resource, File fileToC
136136
* @param resourceToCheck the resource to check
137137
*/
138138
public static void throwIfDoesNotExist(ConfigDataResource resource, Resource resourceToCheck) {
139-
throwIfDoesNotExist(resource, resourceToCheck.exists());
139+
throwIfNot(resource, resourceToCheck.exists());
140140
}
141141

142-
private static void throwIfDoesNotExist(ConfigDataResource resource, boolean exists) {
143-
if (!exists) {
142+
private static void throwIfNot(ConfigDataResource resource, boolean check) {
143+
if (!check) {
144144
throw new ConfigDataResourceNotFoundException(resource);
145145
}
146146
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/EnvConfigDataLoader.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/EnvConfigDataResource.java

Lines changed: 0 additions & 75 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.config;
18+
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
/**
23+
* User provided hint for an otherwise missing file extension.
24+
*
25+
* @author Phillip Webb
26+
*/
27+
final class FileExtensionHint {
28+
29+
private static final Pattern PATTERN = Pattern.compile("^(.*)\\[(\\.\\w+)](?!\\[)$");
30+
31+
private static final FileExtensionHint NONE = new FileExtensionHint(null);
32+
33+
private final Matcher matcher;
34+
35+
private FileExtensionHint(Matcher matcher) {
36+
this.matcher = matcher;
37+
}
38+
39+
/**
40+
* Return {@code true} if the hint is present.
41+
* @return if the hint is present
42+
*/
43+
boolean isPresent() {
44+
return this.matcher != null;
45+
}
46+
47+
/**
48+
* Return the extension from the hint or return the parameter if the hint is not
49+
* {@link #isPresent() present}.
50+
* @param extension the fallback extension
51+
* @return the extension either from the hint or fallback
52+
*/
53+
String orElse(String extension) {
54+
return (this.matcher != null) ? toString() : extension;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return (this.matcher != null) ? this.matcher.group(2) : "";
60+
}
61+
62+
/**
63+
* Return the {@link FileExtensionHint} from the given value.
64+
* @param value the source value
65+
* @return the {@link FileExtensionHint} (never {@code null})
66+
*/
67+
static FileExtensionHint from(String value) {
68+
Matcher matcher = PATTERN.matcher(value);
69+
return (matcher.matches()) ? new FileExtensionHint(matcher) : NONE;
70+
}
71+
72+
/**
73+
* Remove any hint from the given value.
74+
* @param value the source value
75+
* @return the value without any hint
76+
*/
77+
static String removeFrom(String value) {
78+
Matcher matcher = PATTERN.matcher(value);
79+
return (matcher.matches()) ? matcher.group(1) : value;
80+
}
81+
82+
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/config/StandardConfigDataLocationResolver.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.LinkedHashSet;
2727
import java.util.List;
2828
import java.util.Set;
29-
import java.util.regex.Matcher;
3029
import java.util.regex.Pattern;
3130
import java.util.stream.Collectors;
3231

@@ -68,8 +67,6 @@ public class StandardConfigDataLocationResolver
6867

6968
private static final Pattern URL_PREFIX = Pattern.compile("^([a-zA-Z][a-zA-Z0-9*]*?:)(.*$)");
7069

71-
private static final Pattern EXTENSION_HINT_PATTERN = Pattern.compile("^(.*)\\[(\\.\\w+)](?!\\[)$");
72-
7370
private static final String NO_PROFILE = null;
7471

7572
private final Log logger;
@@ -238,17 +235,16 @@ private Deque<StandardConfigDataReference> getReferencesForConfigName(String nam
238235

239236
private Set<StandardConfigDataReference> getReferencesForFile(ConfigDataLocation configDataLocation, String file,
240237
String profile) {
241-
Matcher extensionHintMatcher = EXTENSION_HINT_PATTERN.matcher(file);
242-
boolean extensionHintLocation = extensionHintMatcher.matches();
243-
if (extensionHintLocation) {
244-
file = extensionHintMatcher.group(1) + extensionHintMatcher.group(2);
238+
FileExtensionHint fileExtensionHint = FileExtensionHint.from(file);
239+
if (fileExtensionHint.isPresent()) {
240+
file = FileExtensionHint.removeFrom(file) + fileExtensionHint;
245241
}
246242
for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) {
247-
String extension = getLoadableFileExtension(propertySourceLoader, file);
248-
if (extension != null) {
249-
String root = file.substring(0, file.length() - extension.length() - 1);
243+
String fileExtension = getLoadableFileExtension(propertySourceLoader, file);
244+
if (fileExtension != null) {
245+
String root = file.substring(0, file.length() - fileExtension.length() - 1);
250246
StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation, null, root,
251-
profile, (!extensionHintLocation) ? extension : null, propertySourceLoader);
247+
profile, (!fileExtensionHint.isPresent()) ? fileExtension : null, propertySourceLoader);
252248
return Collections.singleton(reference);
253249
}
254250
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.config;
18+
19+
import java.io.IOException;
20+
import java.util.List;
21+
22+
import org.springframework.core.env.PropertySource;
23+
24+
/**
25+
* {@link ConfigDataLoader} to load data from system environment variables.
26+
*
27+
* @author Moritz Halbritter
28+
*/
29+
class SystemEnvironmentConfigDataLoader implements ConfigDataLoader<SystemEnvironmentConfigDataResource> {
30+
31+
@Override
32+
public ConfigData load(ConfigDataLoaderContext context, SystemEnvironmentConfigDataResource resource)
33+
throws IOException, ConfigDataResourceNotFoundException {
34+
List<PropertySource<?>> loaded = resource.load();
35+
if (loaded == null) {
36+
throw new ConfigDataResourceNotFoundException(resource);
37+
}
38+
return new ConfigData(loaded);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)