From 5c4cde78536c014757672ea780abdef0a7283743 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Wed, 16 Oct 2019 10:07:27 +0900 Subject: [PATCH 1/2] Polish AbstractResource See gh-23813 --- .../core/io/AbstractResource.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java index de327d7e1c1..9f35ead87fe 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java @@ -25,6 +25,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; +import java.util.function.Supplier; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -60,10 +61,7 @@ public abstract class AbstractResource implements Resource { return getFile().exists(); } catch (IOException ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug("Could not retrieve File for existence check of " + getDescription(), ex); - } + debug(() -> "Could not retrieve File for existence check of " + getDescription(), ex); } } // Fall back to stream existence: can we open the stream? @@ -72,14 +70,18 @@ public abstract class AbstractResource implements Resource { return true; } catch (Throwable ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug("Could not retrieve InputStream for existence check of " + getDescription(), ex); - } + debug(() -> "Could not retrieve InputStream for existence check of " + getDescription(), ex); return false; } } + private void debug(Supplier message, Throwable ex) { + Log logger = LogFactory.getLog(getClass()); + if (logger.isDebugEnabled()) { + logger.debug(message.get(), ex); + } + } + /** * This implementation always returns {@code true} for a resource * that {@link #exists() exists} (revised as of 5.1). @@ -174,10 +176,7 @@ public abstract class AbstractResource implements Resource { is.close(); } catch (IOException ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug("Could not close content-length InputStream for " + getDescription(), ex); - } + debug(() -> "Could not close content-length InputStream for " + getDescription(), ex); } } } From 46cb5ab1355660979ad4cba59be023885540106d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 30 Nov 2021 14:29:05 +0100 Subject: [PATCH 2/2] Polish contribution See gh-23813 --- .../core/io/AbstractResource.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java index 9f35ead87fe..cf3266672f9 100644 --- a/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java +++ b/spring-core/src/main/java/org/springframework/core/io/AbstractResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -75,13 +75,6 @@ public abstract class AbstractResource implements Resource { } } - private void debug(Supplier message, Throwable ex) { - Log logger = LogFactory.getLog(getClass()); - if (logger.isDebugEnabled()) { - logger.debug(message.get(), ex); - } - } - /** * This implementation always returns {@code true} for a resource * that {@link #exists() exists} (revised as of 5.1). @@ -257,4 +250,11 @@ public abstract class AbstractResource implements Resource { return getDescription(); } + private void debug(Supplier message, Throwable ex) { + Log logger = LogFactory.getLog(getClass()); + if (logger.isDebugEnabled()) { + logger.debug(message.get(), ex); + } + } + }