001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017
018package org.apache.logging.log4j.core.util;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.net.DatagramSocket;
023import java.net.ServerSocket;
024import java.sql.Connection;
025import java.sql.ResultSet;
026import java.sql.SQLException;
027import java.sql.Statement;
028
029/**
030 * Helper class for closing resources.
031 */
032public final class Closer {
033
034    private Closer() {
035    }
036
037    /**
038     * Closes the specified {@code Closeable} (stream or reader/writer),
039     * ignoring any exceptions thrown by the close operation.
040     *
041     * @param closeable the resource to close, may be {@code null}
042     */
043    public static void closeSilently(final Closeable closeable) {
044        try {
045            close(closeable);
046        } catch (final Exception ignored) {
047            // ignored
048        }
049    }
050
051    /**
052     * Closes the specified {@code Closeable} (stream or reader/writer).
053     *
054     * @param closeable the resource to close, may be {@code null}
055     * @throws IOException if a problem occurred closing the specified resource
056     */
057    public static void close(final Closeable closeable) throws IOException {
058        if (closeable != null) {
059            closeable.close();
060        }
061    }
062
063    /**
064     * Closes the specified resource, ignoring any exceptions thrown by the close operation.
065     *
066     * @param serverSocket the resource to close, may be {@code null}
067     */
068    public static void closeSilently(final ServerSocket serverSocket) {
069        try {
070            close(serverSocket);
071        } catch (final Exception ignored) {
072            // ignored
073        }
074    }
075
076    /**
077     * Closes the specified resource.
078     *
079     * @param serverSocket the resource to close, may be {@code null}
080     * @throws IOException if a problem occurred closing the specified resource
081     */
082    public static void close(final ServerSocket serverSocket) throws IOException {
083        if (serverSocket != null) {
084            serverSocket.close();
085        }
086    }
087
088    /**
089     * Closes the specified resource, ignoring any exceptions thrown by the close operation.
090     *
091     * @param datagramSocket the resource to close, may be {@code null}
092     */
093    public static void closeSilently(final DatagramSocket datagramSocket) {
094        try {
095            close(datagramSocket);
096        } catch (final Exception ignored) {
097            // ignored
098        }
099    }
100
101    /**
102     * Closes the specified resource.
103     *
104     * @param datagramSocket the resource to close, may be {@code null}
105     * @throws IOException if a problem occurred closing the specified resource
106     */
107    public static void close(final DatagramSocket datagramSocket) throws IOException {
108        if (datagramSocket != null) {
109            datagramSocket.close();
110        }
111    }
112
113    /**
114     * Closes the specified {@code Statement}, ignoring any exceptions thrown by
115     * the close operation.
116     *
117     * @param statement the resource to close, may be {@code null}
118     */
119    public static void closeSilently(final Statement statement) {
120        try {
121            close(statement);
122        } catch (final Exception ignored) {
123            // ignored
124        }
125    }
126
127    /**
128     * Closes the specified {@code ResultSet}.
129     *
130     * @param resultSet the resource to close, may be {@code null}
131     * @throws SQLException if a problem occurred closing the specified resource
132     */
133    public static void close(final ResultSet resultSet) throws SQLException {
134        if (resultSet != null) {
135            resultSet.close();
136        }
137    }
138
139    /**
140     * Closes the specified {@code Statement}.
141     *
142     * @param statement the resource to close, may be {@code null}
143     * @throws SQLException if a problem occurred closing the specified resource
144     */
145    public static void close(final Statement statement) throws SQLException {
146        if (statement != null) {
147            statement.close();
148        }
149    }
150
151    /**
152     * Closes the specified {@code Connection}, ignoring any exceptions thrown
153     * by the close operation.
154     *
155     * @param connection the resource to close, may be {@code null}
156     */
157    public static void closeSilently(final Connection connection) {
158        try {
159            close(connection);
160        } catch (final Exception ignored) {
161            // ignored
162        }
163    }
164
165    /**
166     * Closes the specified {@code Connection}.
167     *
168     * @param connection the resource to close, may be {@code null}
169     * @throws SQLException if a problem occurred closing the specified resource
170     */
171    public static void close(final Connection connection) throws SQLException {
172        if (connection != null) {
173            connection.close();
174        }
175    }
176
177}