From caa22b729183fc8990ec37c74790625811cadbbd Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 7 Sep 2020 18:50:16 +0200 Subject: [PATCH] Properties loading with ignoreResourceNotFound covers SocketException as well Closes gh-25717 --- .../annotation/ConfigurationClassParser.java | 51 ++++++++++--------- .../io/support/PropertiesLoaderSupport.java | 6 ++- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index cfb0872332f..511ec68ea8a 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ package org.springframework.context.annotation; import java.io.FileNotFoundException; import java.io.IOException; import java.lang.annotation.Annotation; +import java.net.SocketException; import java.net.UnknownHostException; import java.util.ArrayDeque; import java.util.ArrayList; @@ -458,7 +459,8 @@ class ConfigurationClassParser { catch (IOException ex) { // Resource not found when trying to open it if (ignoreResourceNotFound && - (ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) { + (ex instanceof FileNotFoundException || ex instanceof UnknownHostException || + ex instanceof SocketException)) { if (logger.isInfoEnabled()) { logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage()); } @@ -473,32 +475,35 @@ class ConfigurationClassParser { private void addPropertySource(PropertySource propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); - if (propertySources.contains(name) && this.propertySourceNames.contains(name)) { + + if (this.propertySourceNames.contains(name)) { // We've already added a version, we need to extend it PropertySource existing = propertySources.get(name); - PropertySource newSource = (propertySource instanceof ResourcePropertySource ? - ((ResourcePropertySource) propertySource).withResourceName() : propertySource); - if (existing instanceof CompositePropertySource) { - ((CompositePropertySource) existing).addFirstPropertySource(newSource); - } - else { - if (existing instanceof ResourcePropertySource) { - existing = ((ResourcePropertySource) existing).withResourceName(); + if (existing != null) { + PropertySource newSource = (propertySource instanceof ResourcePropertySource ? + ((ResourcePropertySource) propertySource).withResourceName() : propertySource); + if (existing instanceof CompositePropertySource) { + ((CompositePropertySource) existing).addFirstPropertySource(newSource); + } + else { + if (existing instanceof ResourcePropertySource) { + existing = ((ResourcePropertySource) existing).withResourceName(); + } + CompositePropertySource composite = new CompositePropertySource(name); + composite.addPropertySource(newSource); + composite.addPropertySource(existing); + propertySources.replace(name, composite); } - CompositePropertySource composite = new CompositePropertySource(name); - composite.addPropertySource(newSource); - composite.addPropertySource(existing); - propertySources.replace(name, composite); + return; } } + + if (this.propertySourceNames.isEmpty()) { + propertySources.addLast(propertySource); + } else { - if (this.propertySourceNames.isEmpty()) { - propertySources.addLast(propertySource); - } - else { - String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1); - propertySources.addBefore(firstProcessed, propertySource); - } + String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1); + propertySources.addBefore(firstProcessed, propertySource); } this.propertySourceNames.add(name); } @@ -675,7 +680,7 @@ class ConfigurationClassParser { } /** - * Factory method to obtain {@link SourceClass}s from class names. + * Factory method to obtain a {@link SourceClass} collection from class names. */ private Collection asSourceClasses(String[] classNames) throws IOException { List annotatedClasses = new ArrayList(classNames.length); diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java b/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java index 73f127741f1..90d5fa2de02 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.springframework.core.io.support; import java.io.FileNotFoundException; import java.io.IOException; +import java.net.SocketException; import java.net.UnknownHostException; import java.util.Properties; @@ -180,7 +181,8 @@ public abstract class PropertiesLoaderSupport { catch (IOException ex) { // Resource not found when trying to open it if (this.ignoreResourceNotFound && - (ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) { + (ex instanceof FileNotFoundException || ex instanceof UnknownHostException || + ex instanceof SocketException)) { if (logger.isInfoEnabled()) { logger.info("Properties resource not found: " + ex.getMessage()); }