1
0

Use try-with-resources

This commit is contained in:
Gary Gregory
2023-11-19 11:41:57 -05:00
parent fa9feb0d9e
commit 7af5ec6b03

View File

@@ -996,42 +996,32 @@ public abstract class LogFactory {
* {@code Null} is returned if the URL cannot be opened. * {@code Null} is returned if the URL cannot be opened.
*/ */
private static Properties getProperties(final URL url) { private static Properties getProperties(final URL url) {
final PrivilegedAction<Properties> action = () -> { return AccessController.doPrivileged((PrivilegedAction<Properties>) () -> {
InputStream stream = null;
try {
// We must ensure that useCaches is set to false, as the // We must ensure that useCaches is set to false, as the
// default behavior of java is to cache file handles, and // default behavior of java is to cache file handles, and
// this "locks" files, preventing hot-redeploy on windows. // this "locks" files, preventing hot-redeploy on windows.
try {
final URLConnection connection = url.openConnection(); final URLConnection connection = url.openConnection();
connection.setUseCaches(false); connection.setUseCaches(false);
stream = connection.getInputStream(); try (InputStream stream = connection.getInputStream()) {
if (stream != null) { if (stream != null) {
final Properties props = new Properties(); final Properties props = new Properties();
props.load(stream); props.load(stream);
stream.close();
stream = null;
return props; return props;
} }
} catch (final IOException e) {
if (isDiagnosticsEnabled()) {
logDiagnostic("Unable to close stream for URL " + url);
}
}
} catch (final IOException e) { } catch (final IOException e) {
if (isDiagnosticsEnabled()) { if (isDiagnosticsEnabled()) {
logDiagnostic("Unable to read URL " + url); 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 null;
}; });
return AccessController.doPrivileged(action);
} }
/** /**