From 54180f90e275621281713679ec9230226b1567cf Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 11 Apr 2017 11:45:30 +0200 Subject: [PATCH] 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) --- .../context/annotation/ConfigurationClassParser.java | 7 +++++-- .../org/springframework/core/io/InputStreamSource.java | 8 ++++---- .../main/java/org/springframework/core/io/Resource.java | 8 +++++--- .../core/io/support/PropertiesLoaderSupport.java | 7 +++++-- 4 files changed, 19 insertions(+), 11 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 2691a71ee56..a4faae32971 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 @@ -18,6 +18,7 @@ package org.springframework.context.annotation; import java.io.FileNotFoundException; import java.io.IOException; +import java.net.UnknownHostException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; @@ -444,9 +445,10 @@ class ConfigurationClassParser { throw ex; } } - catch (FileNotFoundException ex) { + catch (IOException ex) { // Resource not found when trying to open it - if (ignoreResourceNotFound) { + if (ignoreResourceNotFound && + (ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) { if (logger.isInfoEnabled()) { logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage()); } @@ -491,6 +493,7 @@ class ConfigurationClassParser { this.propertySourceNames.add(name); } + /** * Returns {@code @Import} class, considering all meta-annotations. */ diff --git a/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java b/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java index f31e6ef9a25..0a1758ad147 100644 --- a/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java +++ b/spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 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. @@ -41,15 +41,15 @@ import java.io.InputStream; public interface InputStreamSource { /** - * Return an {@link InputStream}. + * Return an {@link InputStream} for the content of an underlying resource. *

It is expected that each call creates a fresh stream. *

This requirement is particularly important when you consider an API such * as JavaMail, which needs to be able to read the stream multiple times when * creating mail attachments. For such a use case, it is required * that each {@code getInputStream()} call returns a fresh stream. * @return the input stream for the underlying resource (must not be {@code null}) - * @throws IOException if the stream could not be opened - * @see org.springframework.mail.javamail.MimeMessageHelper#addAttachment(String, InputStreamSource) + * @throws java.io.FileNotFoundException if the underlying resource doesn't exist + * @throws IOException if the content stream could not be opened */ InputStream getInputStream() throws IOException; diff --git a/spring-core/src/main/java/org/springframework/core/io/Resource.java b/spring-core/src/main/java/org/springframework/core/io/Resource.java index ac4fcb29676..98091347ca4 100644 --- a/spring-core/src/main/java/org/springframework/core/io/Resource.java +++ b/spring-core/src/main/java/org/springframework/core/io/Resource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 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. @@ -90,8 +90,10 @@ public interface Resource extends InputStreamSource { /** * Return a File handle for this resource. - * @throws IOException if the resource cannot be resolved as absolute - * file path, i.e. if the resource is not available in a file system + * @throws java.io.FileNotFoundException if the resource cannot be resolved as + * absolute file path, i.e. if the resource is not available in a file system + * @throws IOException in case of general resolution/reading failures + * @see #getInputStream() */ File getFile() throws IOException; 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 88edb0ec8a3..9bc061a53b0 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 @@ -18,6 +18,7 @@ package org.springframework.core.io.support; import java.io.FileNotFoundException; import java.io.IOException; +import java.net.UnknownHostException; import java.util.Properties; import org.apache.commons.logging.Log; @@ -176,8 +177,10 @@ public abstract class PropertiesLoaderSupport { PropertiesLoaderUtils.fillProperties( props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister); } - catch (FileNotFoundException ex) { - if (this.ignoreResourceNotFound) { + catch (IOException ex) { + // Resource not found when trying to open it + if (this.ignoreResourceNotFound && + (ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) { if (logger.isInfoEnabled()) { logger.info("Properties resource not found: " + ex.getMessage()); }