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    
018    package org.apache.logging.log4j.core.util;
019    
020    import java.io.Closeable;
021    import java.io.IOException;
022    import java.net.DatagramSocket;
023    import java.net.ServerSocket;
024    import java.sql.Connection;
025    import java.sql.SQLException;
026    import java.sql.Statement;
027    
028    /**
029     * Helper class for closing resources.
030     */
031    public final class Closer {
032    
033        private Closer() {
034        }
035    
036        /**
037         * Closes the specified {@code Closeable} (stream or reader/writer),
038         * ignoring any exceptions thrown by the close operation.
039         *
040         * @param closeable the resource to close, may be {@code null}
041         */
042        public static void closeSilently(final Closeable closeable) {
043            try {
044                close(closeable);
045            } catch (final Exception ignored) {
046                // ignored
047            }
048        }
049    
050        /**
051         * Closes the specified {@code Closeable} (stream or reader/writer).
052         *
053         * @param closeable the resource to close, may be {@code null}
054         * @throws IOException if a problem occurred closing the specified resource
055         */
056        public static void close(final Closeable closeable) throws IOException {
057            if (closeable != null) {
058                closeable.close();
059            }
060        }
061    
062        /**
063         * Closes the specified resource, ignoring any exceptions thrown by the close operation.
064         *
065         * @param serverSocket the resource to close, may be {@code null}
066         */
067        public static void closeSilently(final ServerSocket serverSocket) {
068            try {
069                close(serverSocket);
070            } catch (final Exception ignored) {
071                // ignored
072            }
073        }
074    
075        /**
076         * Closes the specified resource.
077         *
078         * @param serverSocket the resource to close, may be {@code null}
079         * @throws IOException if a problem occurred closing the specified resource
080         */
081        public static void close(final ServerSocket serverSocket) throws IOException {
082            if (serverSocket != null) {
083                serverSocket.close();
084            }
085        }
086    
087        /**
088         * Closes the specified resource, ignoring any exceptions thrown by the close operation.
089         *
090         * @param datagramSocket the resource to close, may be {@code null}
091         */
092        public static void closeSilently(final DatagramSocket datagramSocket) {
093            try {
094                close(datagramSocket);
095            } catch (final Exception ignored) {
096                // ignored
097            }
098        }
099    
100        /**
101         * Closes the specified resource.
102         *
103         * @param datagramSocket the resource to close, may be {@code null}
104         * @throws IOException if a problem occurred closing the specified resource
105         */
106        public static void close(final DatagramSocket datagramSocket) throws IOException {
107            if (datagramSocket != null) {
108                datagramSocket.close();
109            }
110        }
111    
112        /**
113         * Closes the specified {@code Statement}, ignoring any exceptions thrown by
114         * the close operation.
115         *
116         * @param statement the resource to close, may be {@code null}
117         */
118        public static void closeSilently(final Statement statement) {
119            try {
120                close(statement);
121            } catch (final Exception ignored) {
122                // ignored
123            }
124        }
125    
126        /**
127         * Closes the specified {@code Statement}.
128         *
129         * @param statement the resource to close, may be {@code null}
130         * @throws SQLException if a problem occurred closing the specified resource
131         */
132        public static void close(final Statement statement) throws SQLException {
133            if (statement != null) {
134                statement.close();
135            }
136        }
137    
138        /**
139         * Closes the specified {@code Connection}, ignoring any exceptions thrown
140         * by the close operation.
141         *
142         * @param connection the resource to close, may be {@code null}
143         */
144        public static void closeSilently(final Connection connection) {
145            try {
146                close(connection);
147            } catch (final Exception ignored) {
148                // ignored
149            }
150        }
151    
152        /**
153         * Closes the specified {@code Connection}.
154         *
155         * @param connection the resource to close, may be {@code null}
156         * @throws SQLException if a problem occurred closing the specified resource
157         */
158        public static void close(final Connection connection) throws SQLException {
159            if (connection != null) {
160                connection.close();
161            }
162        }
163    
164    }