Skip to content

Commit 54180f9

Browse files
committed
UnknownHostException as resource-not-found for properties files
Plus first-class declaration of FileNotFoundException in Resource javadoc. Issue: SPR-15433 (cherry picked from commit c4e0d6c)
1 parent 865953f commit 54180f9

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
21+
import java.net.UnknownHostException;
2122
import java.util.ArrayDeque;
2223
import java.util.ArrayList;
2324
import java.util.Collection;
@@ -444,9 +445,10 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
444445
throw ex;
445446
}
446447
}
447-
catch (FileNotFoundException ex) {
448+
catch (IOException ex) {
448449
// Resource not found when trying to open it
449-
if (ignoreResourceNotFound) {
450+
if (ignoreResourceNotFound &&
451+
(ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) {
450452
if (logger.isInfoEnabled()) {
451453
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
452454
}
@@ -491,6 +493,7 @@ private void addPropertySource(PropertySource<?> propertySource) {
491493
this.propertySourceNames.add(name);
492494
}
493495

496+
494497
/**
495498
* Returns {@code @Import} class, considering all meta-annotations.
496499
*/

spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,15 +41,15 @@
4141
public interface InputStreamSource {
4242

4343
/**
44-
* Return an {@link InputStream}.
44+
* Return an {@link InputStream} for the content of an underlying resource.
4545
* <p>It is expected that each call creates a <i>fresh</i> stream.
4646
* <p>This requirement is particularly important when you consider an API such
4747
* as JavaMail, which needs to be able to read the stream multiple times when
4848
* creating mail attachments. For such a use case, it is <i>required</i>
4949
* that each {@code getInputStream()} call returns a fresh stream.
5050
* @return the input stream for the underlying resource (must not be {@code null})
51-
* @throws IOException if the stream could not be opened
52-
* @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource)
51+
* @throws java.io.FileNotFoundException if the underlying resource doesn't exist
52+
* @throws IOException if the content stream could not be opened
5353
*/
5454
InputStream getInputStream() throws IOException;
5555

spring-core/src/main/java/org/springframework/core/io/Resource.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,8 +90,10 @@ public interface Resource extends InputStreamSource {
9090

9191
/**
9292
* Return a File handle for this resource.
93-
* @throws IOException if the resource cannot be resolved as absolute
94-
* file path, i.e. if the resource is not available in a file system
93+
* @throws java.io.FileNotFoundException if the resource cannot be resolved as
94+
* absolute file path, i.e. if the resource is not available in a file system
95+
* @throws IOException in case of general resolution/reading failures
96+
* @see #getInputStream()
9597
*/
9698
File getFile() throws IOException;
9799

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.FileNotFoundException;
2020
import java.io.IOException;
21+
import java.net.UnknownHostException;
2122
import java.util.Properties;
2223

2324
import org.apache.commons.logging.Log;
@@ -176,8 +177,10 @@ protected void loadProperties(Properties props) throws IOException {
176177
PropertiesLoaderUtils.fillProperties(
177178
props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
178179
}
179-
catch (FileNotFoundException ex) {
180-
if (this.ignoreResourceNotFound) {
180+
catch (IOException ex) {
181+
// Resource not found when trying to open it
182+
if (this.ignoreResourceNotFound &&
183+
(ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) {
181184
if (logger.isInfoEnabled()) {
182185
logger.info("Properties resource not found: " + ex.getMessage());
183186
}

0 commit comments

Comments
 (0)