From 7af5ec6b037bc281713eb7174c252bb3a16e9b63 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 19 Nov 2023 11:41:57 -0500 Subject: [PATCH] Use try-with-resources --- .../apache/commons/logging/LogFactory.java | 40 +++++++------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/apache/commons/logging/LogFactory.java b/src/main/java/org/apache/commons/logging/LogFactory.java index 324e6ff..df81201 100644 --- a/src/main/java/org/apache/commons/logging/LogFactory.java +++ b/src/main/java/org/apache/commons/logging/LogFactory.java @@ -996,42 +996,32 @@ public abstract class LogFactory { * {@code Null} is returned if the URL cannot be opened. */ private static Properties getProperties(final URL url) { - final PrivilegedAction action = () -> { - InputStream stream = null; + return AccessController.doPrivileged((PrivilegedAction) () -> { + // We must ensure that useCaches is set to false, as the + // default behavior of java is to cache file handles, and + // this "locks" files, preventing hot-redeploy on windows. try { - // We must ensure that useCaches is set to false, as the - // default behavior of java is to cache file handles, and - // this "locks" files, preventing hot-redeploy on windows. final URLConnection connection = url.openConnection(); connection.setUseCaches(false); - stream = connection.getInputStream(); - if (stream != null) { - final Properties props = new Properties(); - props.load(stream); - stream.close(); - stream = null; - return props; + try (InputStream stream = connection.getInputStream()) { + if (stream != null) { + final Properties props = new Properties(); + props.load(stream); + return props; + } + } catch (final IOException e) { + if (isDiagnosticsEnabled()) { + logDiagnostic("Unable to close stream for URL " + url); + } } } catch (final IOException e) { if (isDiagnosticsEnabled()) { logDiagnostic("Unable to read URL " + url); } - } finally { - if (stream != null) { - try { - stream.close(); - } catch (final IOException e) { - // ignore exception; this should not happen - if (isDiagnosticsEnabled()) { - logDiagnostic("Unable to close stream for URL " + url); - } - } - } } return null; - }; - return AccessController.doPrivileged(action); + }); } /**