Browse Source

UnknownHostException as resource-not-found for properties files

Plus first-class declaration of FileNotFoundException in Resource javadoc.

Issue: SPR-15433
pull/1362/merge
Juergen Hoeller 9 years ago
parent
commit
c4e0d6c2a2
  1. 3
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
  2. 7
      spring-core/src/main/java/org/springframework/core/io/InputStreamSource.java
  3. 16
      spring-core/src/main/java/org/springframework/core/io/Resource.java
  4. 3
      spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java

3
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.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -442,7 +443,7 @@ class ConfigurationClassParser {
Resource resource = this.resourceLoader.getResource(resolvedLocation); Resource resource = this.resourceLoader.getResource(resolvedLocation);
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding))); addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
} }
catch (IllegalArgumentException | FileNotFoundException ex) { catch (IllegalArgumentException | FileNotFoundException | UnknownHostException ex) {
// Placeholders not resolvable or resource not found when trying to open it // Placeholders not resolvable or resource not found when trying to open it
if (ignoreResourceNotFound) { if (ignoreResourceNotFound) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {

7
spring-core/src/main/java/org/springframework/core/io/InputStreamSource.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,14 +41,15 @@ import java.io.InputStream;
public interface InputStreamSource { public interface InputStreamSource {
/** /**
* Return an {@link InputStream}. * Return an {@link InputStream} for the content of an underlying resource.
* <p>It is expected that each call creates a <i>fresh</i> stream. * <p>It is expected that each call creates a <i>fresh</i> stream.
* <p>This requirement is particularly important when you consider an API such * <p>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 * as JavaMail, which needs to be able to read the stream multiple times when
* creating mail attachments. For such a use case, it is <i>required</i> * creating mail attachments. For such a use case, it is <i>required</i>
* that each {@code getInputStream()} call returns a fresh stream. * that each {@code getInputStream()} call returns a fresh stream.
* @return the input stream for the underlying resource (must not be {@code null}) * @return the input stream for the underlying resource (must not be {@code null})
* @throws IOException if the stream could not be opened * @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; InputStream getInputStream() throws IOException;

16
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -109,19 +109,23 @@ public interface Resource extends InputStreamSource {
/** /**
* Return a File handle for this resource. * Return a File handle for this resource.
* @throws IOException if the resource cannot be resolved as absolute * @throws java.io.FileNotFoundException if the resource cannot be resolved as
* file path, i.e. if the resource is not available in a file system * 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; File getFile() throws IOException;
/** /**
* Return a {@link ReadableByteChannel}. * Return a {@link ReadableByteChannel}.
* <p>It is expected that each call creates a <i>fresh</i> channel. * <p>It is expected that each call creates a <i>fresh</i> channel.
* <p>The default implementation returns {@link Channels#newChannel(InputStream)} with the * <p>The default implementation returns {@link Channels#newChannel(InputStream)}
* result of {@link #getInputStream()}. * with the result of {@link #getInputStream()}.
* @return the byte channel for the underlying resource (must not be {@code null}) * @return the byte channel for the underlying resource (must not be {@code null})
* @throws IOException if the channel could not be opened * @throws java.io.FileNotFoundException if the underlying resource doesn't exist
* @throws IOException if the content channel could not be opened
* @since 5.0 * @since 5.0
* @see #getInputStream()
*/ */
default ReadableByteChannel readableChannel() throws IOException { default ReadableByteChannel readableChannel() throws IOException {
return Channels.newChannel(getInputStream()); return Channels.newChannel(getInputStream());

3
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.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Properties; import java.util.Properties;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -176,7 +177,7 @@ public abstract class PropertiesLoaderSupport {
PropertiesLoaderUtils.fillProperties( PropertiesLoaderUtils.fillProperties(
props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister); props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
} }
catch (FileNotFoundException ex) { catch (FileNotFoundException | UnknownHostException ex) {
if (this.ignoreResourceNotFound) { if (this.ignoreResourceNotFound) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("Properties resource not found: " + ex.getMessage()); logger.info("Properties resource not found: " + ex.getMessage());

Loading…
Cancel
Save