Polish AbstractResource

See gh-23813
This commit is contained in:
Johnny Lim
2019-10-16 10:07:27 +09:00
committed by Stephane Nicoll
parent da3b4cb65d
commit 5c4cde7853

View File

@@ -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<String> 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);
}
}
}