Collapse Closer.close(Silently) methods into one Now that all the classes in Closer implement AutoCloseable (Java 7), these methods are redundant and can be replaced with a single AutoCloseable version.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/fe4296a4 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/fe4296a4 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/fe4296a4 Branch: refs/heads/master Commit: fe4296a4c8aa90ed5ae181e28e1d591bb6af0120 Parents: ab1672c Author: Matt Sicker <[email protected]> Authored: Tue Jan 3 21:51:30 2017 -0600 Committer: Matt Sicker <[email protected]> Committed: Tue Jan 3 21:51:30 2017 -0600 ---------------------------------------------------------------------- .../apache/logging/log4j/core/util/Closer.java | 134 ++----------------- 1 file changed, 12 insertions(+), 122 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/fe4296a4/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Closer.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Closer.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Closer.java index 3f3f398..26a9226 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Closer.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/Closer.java @@ -17,14 +17,6 @@ package org.apache.logging.log4j.core.util; -import java.io.Closeable; -import java.io.IOException; -import java.net.DatagramSocket; -import java.net.ServerSocket; -import java.sql.Connection; -import java.sql.SQLException; -import java.sql.Statement; - /** * Helper class for closing resources. */ @@ -34,132 +26,30 @@ public final class Closer { } /** - * Closes the specified {@code Closeable} (stream or reader/writer), - * ignoring any exceptions thrown by the close operation. - * - * @param closeable the resource to close, may be {@code null} - */ - public static boolean closeSilently(final Closeable closeable) { - try { - close(closeable); - return true; - } catch (final Exception ignored) { - // ignored - return false; - } - } - - /** - * Closes the specified {@code Closeable} (stream or reader/writer). + * Closes an AutoCloseable or ignores if {@code null}. * - * @param closeable the resource to close, may be {@code null} - * @throws IOException if a problem occurred closing the specified resource + * @param closeable the resource to close; may be null + * @throws Exception if the resource cannot be closed + * @since 2.8 */ - public static void close(final Closeable closeable) throws IOException { + public static void close(final AutoCloseable closeable) throws Exception { if (closeable != null) { closeable.close(); } } /** - * Closes the specified resource, ignoring any exceptions thrown by the close operation. - * - * @param serverSocket the resource to close, may be {@code null} - */ - public static void closeSilently(final ServerSocket serverSocket) { - try { - close(serverSocket); - } catch (final Exception ignored) { - // ignored - } - } - - /** - * Closes the specified resource. - * - * @param serverSocket the resource to close, may be {@code null} - * @throws IOException if a problem occurred closing the specified resource - */ - public static void close(final ServerSocket serverSocket) throws IOException { - if (serverSocket != null) { - serverSocket.close(); - } - } - - /** - * Closes the specified resource, ignoring any exceptions thrown by the close operation. + * Closes an AutoCloseable and returns {@code true} if it closed without exception. * - * @param datagramSocket the resource to close, may be {@code null} + * @param closeable the resource to close; may be null + * @return true if resource was closed successfully, or false if an exception was thrown */ - public static void closeSilently(final DatagramSocket datagramSocket) { + public static boolean closeSilently(final AutoCloseable closeable) { try { - close(datagramSocket); - } catch (final Exception ignored) { - // ignored - } - } - - /** - * Closes the specified resource. - * - * @param datagramSocket the resource to close, may be {@code null} - * @throws IOException if a problem occurred closing the specified resource - */ - public static void close(final DatagramSocket datagramSocket) throws IOException { - if (datagramSocket != null) { - datagramSocket.close(); - } - } - - /** - * Closes the specified {@code Statement}, ignoring any exceptions thrown by - * the close operation. - * - * @param statement the resource to close, may be {@code null} - */ - public static void closeSilently(final Statement statement) { - try { - close(statement); - } catch (final Exception ignored) { - // ignored - } - } - - /** - * Closes the specified {@code Statement}. - * - * @param statement the resource to close, may be {@code null} - * @throws SQLException if a problem occurred closing the specified resource - */ - public static void close(final Statement statement) throws SQLException { - if (statement != null) { - statement.close(); - } - } - - /** - * Closes the specified {@code Connection}, ignoring any exceptions thrown - * by the close operation. - * - * @param connection the resource to close, may be {@code null} - */ - public static void closeSilently(final Connection connection) { - try { - close(connection); + close(closeable); + return true; } catch (final Exception ignored) { - // ignored - } - } - - /** - * Closes the specified {@code Connection}. - * - * @param connection the resource to close, may be {@code null} - * @throws SQLException if a problem occurred closing the specified resource - */ - public static void close(final Connection connection) throws SQLException { - if (connection != null) { - connection.close(); + return false; } }
