Skip to content

Commit 12f8c25

Browse files
committed
Polish
1 parent b66c3dd commit 12f8c25

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,59 @@ public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProce
5252

5353
private static final String CONFIG_PATH = "/.config/spring-boot/";
5454

55+
private static final File HOME_FOLDER;
56+
static {
57+
String home = System.getProperty("user.home");
58+
HOME_FOLDER = StringUtils.hasLength(home) ? new File(home) : null;
59+
}
60+
5561
@Override
5662
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
5763
if (DevToolsEnablementDeducer.shouldEnable(Thread.currentThread())) {
58-
List<PropertySource> propertySources = getPropertySources();
64+
List<PropertySource<?>> propertySources = getPropertySources();
5965
if (propertySources.isEmpty()) {
60-
addPropertySource(LEGACY_FILE_NAME, (file) -> "devtools-local", propertySources);
66+
addPropertySource(propertySources, LEGACY_FILE_NAME, (file) -> "devtools-local");
6167
}
62-
propertySources.forEach((source) -> environment.getPropertySources().addFirst(source));
68+
propertySources.forEach(environment.getPropertySources()::addFirst);
6369
}
6470
}
6571

66-
private List<PropertySource> getPropertySources() {
67-
List<PropertySource> propertySources = new ArrayList<>();
72+
private List<PropertySource<?>> getPropertySources() {
73+
List<PropertySource<?>> propertySources = new ArrayList<>();
6874
for (String fileName : FILE_NAMES) {
69-
addPropertySource(CONFIG_PATH + fileName, (file) -> "devtools-local: [" + file.toURI() + "]",
70-
propertySources);
75+
addPropertySource(propertySources, CONFIG_PATH + fileName, this::getPropertySourceName);
7176
}
7277
return propertySources;
7378
}
7479

75-
private void addPropertySource(String fileName, Function<File, String> propertySourceName,
76-
List<PropertySource> propertySources) {
77-
Properties properties;
80+
private String getPropertySourceName(File file) {
81+
return "devtools-local: [" + file.toURI() + "]";
82+
}
83+
84+
private void addPropertySource(List<PropertySource<?>> propertySources, String fileName,
85+
Function<File, String> propertySourceNamer) {
7886
File home = getHomeFolder();
79-
File propertyFile = (home != null) ? new File(home, fileName) : null;
80-
if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) {
81-
FileSystemResource resource = new FileSystemResource(propertyFile);
82-
try {
83-
properties = PropertiesLoaderUtils.loadProperties(resource);
84-
propertySources.add(new PropertiesPropertySource(propertySourceName.apply(propertyFile), properties));
85-
}
86-
catch (IOException ex) {
87-
throw new IllegalStateException("Unable to load " + fileName, ex);
88-
}
87+
File file = (home != null) ? new File(home, fileName) : null;
88+
FileSystemResource resource = (file != null) ? new FileSystemResource(file) : null;
89+
if (resource != null && resource.exists() && resource.isFile()) {
90+
addPropertySource(propertySources, resource, propertySourceNamer);
8991
}
9092
}
9193

92-
protected File getHomeFolder() {
93-
String home = System.getProperty("user.home");
94-
if (StringUtils.hasLength(home)) {
95-
return new File(home);
94+
private void addPropertySource(List<PropertySource<?>> propertySources, FileSystemResource resource,
95+
Function<File, String> propertySourceNamer) {
96+
try {
97+
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
98+
String name = propertySourceNamer.apply(resource.getFile());
99+
propertySources.add(new PropertiesPropertySource(name, properties));
100+
}
101+
catch (IOException ex) {
102+
throw new IllegalStateException("Unable to load " + resource.getFilename(), ex);
96103
}
97-
return null;
104+
}
105+
106+
protected File getHomeFolder() {
107+
return HOME_FOLDER;
98108
}
99109

100110
}

0 commit comments

Comments
 (0)