From 1528b6c2f8b0524360e0850f7c8e354d7635d0c2 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 27 Sep 2019 22:58:55 -0700 Subject: [PATCH] Polish --- .../DevToolsHomePropertiesPostProcessor.java | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java index 84d34fe7af..8c1a4e5069 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsHomePropertiesPostProcessor.java @@ -55,37 +55,45 @@ public class DevToolsHomePropertiesPostProcessor implements EnvironmentPostProce @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (DevToolsEnablementDeducer.shouldEnable(Thread.currentThread())) { - List propertySources = getPropertySources(); + List> propertySources = getPropertySources(); if (propertySources.isEmpty()) { - addPropertySource(LEGACY_FILE_NAME, (file) -> "devtools-local", propertySources); + addPropertySource(propertySources, LEGACY_FILE_NAME, (file) -> "devtools-local"); } - propertySources.forEach((source) -> environment.getPropertySources().addFirst(source)); + propertySources.forEach(environment.getPropertySources()::addFirst); } } - private List getPropertySources() { - List propertySources = new ArrayList<>(); + private List> getPropertySources() { + List> propertySources = new ArrayList<>(); for (String fileName : FILE_NAMES) { - addPropertySource(CONFIG_PATH + fileName, (file) -> "devtools-local: [" + file.toURI() + "]", - propertySources); + addPropertySource(propertySources, CONFIG_PATH + fileName, this::getPropertySourceName); } return propertySources; } - private void addPropertySource(String fileName, Function propertySourceName, - List propertySources) { - Properties properties; + private String getPropertySourceName(File file) { + return "devtools-local: [" + file.toURI() + "]"; + } + + private void addPropertySource(List> propertySources, String fileName, + Function propertySourceNamer) { File home = getHomeFolder(); - File propertyFile = (home != null) ? new File(home, fileName) : null; - if (propertyFile != null && propertyFile.exists() && propertyFile.isFile()) { - FileSystemResource resource = new FileSystemResource(propertyFile); - try { - properties = PropertiesLoaderUtils.loadProperties(resource); - propertySources.add(new PropertiesPropertySource(propertySourceName.apply(propertyFile), properties)); - } - catch (IOException ex) { - throw new IllegalStateException("Unable to load " + fileName, ex); - } + File file = (home != null) ? new File(home, fileName) : null; + FileSystemResource resource = (file != null) ? new FileSystemResource(file) : null; + if (resource != null && resource.exists() && resource.isFile()) { + addPropertySource(propertySources, resource, propertySourceNamer); + } + } + + private void addPropertySource(List> propertySources, FileSystemResource resource, + Function propertySourceNamer) { + try { + Properties properties = PropertiesLoaderUtils.loadProperties(resource); + String name = propertySourceNamer.apply(resource.getFile()); + propertySources.add(new PropertiesPropertySource(name, properties)); + } + catch (IOException ex) { + throw new IllegalStateException("Unable to load " + resource.getFilename(), ex); } }