Skip to content

Commit 4a81814

Browse files
committed
Check exception cause for @propertysource(ignoreResourceNotFound) support
Prior to this commit, the ignoreResourceNotFound flag in @propertysource was ignored by PropertySourceProcessor if a PropertySourceFactory threw an exception which wrapped an exception that would otherwise be ignored -- for example, a FileNotFoundException. To address this issue, this commit updates PropertySourceFactory so that it catches RuntimeException and IOException and then checks if the exception or its cause is an "ignorable" exception in terms of ignoreResourceNotFound semantics. Closes gh-22276
1 parent 1451f30 commit 4a81814

File tree

3 files changed

+199
-3
lines changed

3 files changed

+199
-3
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.core.env.PropertySource;
3535
import org.springframework.core.io.Resource;
3636
import org.springframework.core.io.ResourceLoader;
37+
import org.springframework.lang.Nullable;
3738
import org.springframework.util.Assert;
3839
import org.springframework.util.ReflectionUtils;
3940

@@ -44,6 +45,7 @@
4445
* single {@link PropertySource} rather than creating dedicated ones.
4546
*
4647
* @author Stephane Nicoll
48+
* @author Sam Brannen
4749
* @since 6.0
4850
* @see PropertySourceDescriptor
4951
*/
@@ -88,9 +90,10 @@ public void processPropertySource(PropertySourceDescriptor descriptor) throws IO
8890
Resource resource = this.resourceLoader.getResource(resolvedLocation);
8991
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
9092
}
91-
catch (IllegalArgumentException | FileNotFoundException | UnknownHostException | SocketException ex) {
92-
// Placeholders not resolvable or resource not found when trying to open it
93-
if (ignoreResourceNotFound) {
93+
catch (RuntimeException | IOException ex) {
94+
// Placeholders not resolvable (IllegalArgumentException) or resource not found when trying to open it
95+
if (ignoreResourceNotFound && (ex instanceof IllegalArgumentException || isIgnorableException(ex) ||
96+
isIgnorableException(ex.getCause()))) {
9497
if (logger.isInfoEnabled()) {
9598
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
9699
}
@@ -150,4 +153,14 @@ private static PropertySourceFactory instantiateClass(Class<? extends PropertySo
150153
}
151154
}
152155

156+
/**
157+
* Determine if the supplied exception can be ignored according to
158+
* {@code ignoreResourceNotFound} semantics.
159+
*/
160+
private static boolean isIgnorableException(@Nullable Throwable ex) {
161+
return (ex instanceof FileNotFoundException ||
162+
ex instanceof UnknownHostException ||
163+
ex instanceof SocketException);
164+
}
165+
153166
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright 2002-2023 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.core.io.support;
18+
19+
import java.io.FileNotFoundException;
20+
import java.io.IOException;
21+
import java.io.UncheckedIOException;
22+
import java.net.SocketException;
23+
import java.net.UnknownHostException;
24+
import java.util.List;
25+
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Nested;
28+
import org.junit.jupiter.api.Test;
29+
30+
import org.springframework.core.env.PropertySource;
31+
import org.springframework.core.env.StandardEnvironment;
32+
import org.springframework.core.io.DefaultResourceLoader;
33+
import org.springframework.core.io.ResourceLoader;
34+
import org.springframework.util.ClassUtils;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
38+
import static org.assertj.core.api.Assertions.assertThatNoException;
39+
40+
/**
41+
* Unit tests for {@link PropertySourceProcessor}.
42+
*
43+
* @author Sam Brannen
44+
* @since 6.0.12
45+
*/
46+
class PropertySourceProcessorTests {
47+
48+
private static final String PROPS_FILE = ClassUtils.classPackageAsResourcePath(PropertySourceProcessorTests.class) + "/test.properties";
49+
50+
private final StandardEnvironment environment = new StandardEnvironment();
51+
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
52+
private final PropertySourceProcessor processor = new PropertySourceProcessor(environment, resourceLoader);
53+
54+
55+
@BeforeEach
56+
void checkInitialPropertySources() {
57+
assertThat(environment.getPropertySources()).hasSize(2);
58+
}
59+
60+
@Test
61+
void processorRegistersPropertySource() throws Exception {
62+
PropertySourceDescriptor descriptor = new PropertySourceDescriptor(List.of(PROPS_FILE), false, null, DefaultPropertySourceFactory.class, null);
63+
processor.processPropertySource(descriptor);
64+
assertThat(environment.getPropertySources()).hasSize(3);
65+
assertThat(environment.getProperty("enigma")).isEqualTo("42");
66+
}
67+
68+
@Nested
69+
class FailOnErrorTests {
70+
71+
@Test
72+
void processorFailsOnIllegalArgumentException() {
73+
assertProcessorFailsOnError(IllegalArgumentExceptionPropertySourceFactory.class, IllegalArgumentException.class);
74+
}
75+
76+
@Test
77+
void processorFailsOnFileNotFoundException() {
78+
assertProcessorFailsOnError(FileNotFoundExceptionPropertySourceFactory.class, FileNotFoundException.class);
79+
}
80+
81+
private void assertProcessorFailsOnError(
82+
Class<? extends PropertySourceFactory> factoryClass, Class<? extends Throwable> exceptionType) {
83+
84+
PropertySourceDescriptor descriptor =
85+
new PropertySourceDescriptor(List.of(PROPS_FILE), false, null, factoryClass, null);
86+
assertThatExceptionOfType(exceptionType).isThrownBy(() -> processor.processPropertySource(descriptor));
87+
assertThat(environment.getPropertySources()).hasSize(2);
88+
}
89+
90+
}
91+
92+
@Nested
93+
class IgnoreResourceNotFoundTests {
94+
95+
@Test
96+
void processorIgnoresIllegalArgumentException() {
97+
assertProcessorIgnoresFailure(IllegalArgumentExceptionPropertySourceFactory.class);
98+
}
99+
100+
@Test
101+
void processorIgnoresFileNotFoundException() {
102+
assertProcessorIgnoresFailure(FileNotFoundExceptionPropertySourceFactory.class);
103+
}
104+
105+
@Test
106+
void processorIgnoresUnknownHostException() {
107+
assertProcessorIgnoresFailure(UnknownHostExceptionPropertySourceFactory.class);
108+
}
109+
110+
@Test
111+
void processorIgnoresSocketException() {
112+
assertProcessorIgnoresFailure(SocketExceptionPropertySourceFactory.class);
113+
}
114+
115+
@Test
116+
void processorIgnoresSupportedExceptionWrappedInIllegalStateException() {
117+
assertProcessorIgnoresFailure(WrappedIOExceptionPropertySourceFactory.class);
118+
}
119+
120+
@Test
121+
void processorIgnoresSupportedExceptionWrappedInUncheckedIOException() {
122+
assertProcessorIgnoresFailure(UncheckedIOExceptionPropertySourceFactory.class);
123+
}
124+
125+
private void assertProcessorIgnoresFailure(Class<? extends PropertySourceFactory> factoryClass) {
126+
PropertySourceDescriptor descriptor = new PropertySourceDescriptor(List.of(PROPS_FILE), true, null, factoryClass, null);
127+
assertThatNoException().isThrownBy(() -> processor.processPropertySource(descriptor));
128+
assertThat(environment.getPropertySources()).hasSize(2);
129+
}
130+
131+
}
132+
133+
134+
private static class IllegalArgumentExceptionPropertySourceFactory implements PropertySourceFactory {
135+
136+
@Override
137+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
138+
throw new IllegalArgumentException("bogus");
139+
}
140+
}
141+
142+
private static class FileNotFoundExceptionPropertySourceFactory implements PropertySourceFactory {
143+
144+
@Override
145+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
146+
throw new FileNotFoundException("bogus");
147+
}
148+
}
149+
150+
private static class UnknownHostExceptionPropertySourceFactory implements PropertySourceFactory {
151+
152+
@Override
153+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
154+
throw new UnknownHostException("bogus");
155+
}
156+
}
157+
158+
private static class SocketExceptionPropertySourceFactory implements PropertySourceFactory {
159+
160+
@Override
161+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
162+
throw new SocketException("bogus");
163+
}
164+
}
165+
166+
private static class WrappedIOExceptionPropertySourceFactory implements PropertySourceFactory {
167+
168+
@Override
169+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) {
170+
throw new IllegalStateException("Wrapped", new FileNotFoundException("bogus"));
171+
}
172+
}
173+
174+
private static class UncheckedIOExceptionPropertySourceFactory implements PropertySourceFactory {
175+
176+
@Override
177+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) {
178+
throw new UncheckedIOException("Wrapped", new FileNotFoundException("bogus"));
179+
}
180+
}
181+
182+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
enigma = 42

0 commit comments

Comments
 (0)