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    import javax.naming.Context;
029    import javax.naming.NamingException;
030    
031    /**
032     * Helper class for closing resources.
033     */
034    public final class Closer {
035    
036        private Closer() {
037        }
038    
039        /**
040         * Closes the specified {@code Closeable} (stream or reader/writer),
041         * ignoring any exceptions thrown by the close operation.
042         *
043         * @param closeable the resource to close, may be {@code null}
044         */
045        public static void closeSilently(final Closeable closeable) {
046            try {
047                close(closeable);
048            } catch (final Exception ignored) {
049                // ignored
050            }
051        }
052    
053        /**
054         * Closes the specified {@code Closeable} (stream or reader/writer).
055         *
056         * @param closeable the resource to close, may be {@code null}
057         * @throws IOException if a problem occurred closing the specified resource
058         */
059        public static void close(final Closeable closeable) throws IOException {
060            if (closeable != null) {
061                closeable.close();
062            }
063        }
064    
065        /**
066         * Closes the specified resource, ignoring any exceptions thrown by the close operation.
067         *
068         * @param serverSocket the resource to close, may be {@code null}
069         */
070        public static void closeSilently(final ServerSocket serverSocket) {
071            try {
072                close(serverSocket);
073            } catch (final Exception ignored) {
074                // ignored
075            }
076        }
077    
078        /**
079         * Closes the specified resource.
080         *
081         * @param serverSocket the resource to close, may be {@code null}
082         * @throws IOException if a problem occurred closing the specified resource
083         */
084        public static void close(final ServerSocket serverSocket) throws IOException {
085            if (serverSocket != null) {
086                serverSocket.close();
087            }
088        }
089    
090        /**
091         * Closes the specified resource, ignoring any exceptions thrown by the close operation.
092         *
093         * @param datagramSocket the resource to close, may be {@code null}
094         */
095        public static void closeSilently(final DatagramSocket datagramSocket) {
096            try {
097                close(datagramSocket);
098            } catch (final Exception ignored) {
099                // ignored
100            }
101        }
102    
103        /**
104         * Closes the specified resource.
105         *
106         * @param datagramSocket the resource to close, may be {@code null}
107         * @throws IOException if a problem occurred closing the specified resource
108         */
109        public static void close(final DatagramSocket datagramSocket) throws IOException {
110            if (datagramSocket != null) {
111                datagramSocket.close();
112            }
113        }
114    
115        /**
116         * Closes the specified {@code Statement}, ignoring any exceptions thrown by
117         * the close operation.
118         *
119         * @param statement the resource to close, may be {@code null}
120         */
121        public static void closeSilently(final Statement statement) {
122            try {
123                close(statement);
124            } catch (final Exception ignored) {
125                // ignored
126            }
127        }
128    
129        /**
130         * Closes the specified {@code Statement}.
131         *
132         * @param statement the resource to close, may be {@code null}
133         * @throws SQLException if a problem occurred closing the specified resource
134         */
135        public static void close(final Statement statement) throws SQLException {
136            if (statement != null) {
137                statement.close();
138            }
139        }
140    
141        /**
142         * Closes the specified {@code Connection}, ignoring any exceptions thrown
143         * by the close operation.
144         *
145         * @param connection the resource to close, may be {@code null}
146         */
147        public static void closeSilently(final Connection connection) {
148            try {
149                close(connection);
150            } catch (final Exception ignored) {
151                // ignored
152            }
153        }
154    
155        /**
156         * Closes the specified {@code Connection}.
157         *
158         * @param connection the resource to close, may be {@code null}
159         * @throws SQLException if a problem occurred closing the specified resource
160         */
161        public static void close(final Connection connection) throws SQLException {
162            if (connection != null) {
163                connection.close();
164            }
165        }
166    
167        /**
168         * Closes the specified {@code Context}, ignoring any exceptions thrown by the close operation.
169         *
170         * @param context the JNDI Context to close, may be {@code null}
171         */
172        public static void closeSilently(final Context context) {
173            try {
174                close(context);
175            } catch (final NamingException ignored) {
176                // ignored
177            }
178        }
179    
180        /**
181         * Closes the specified {@code Context}.
182         *
183         * @param context the JNDI Context to close, may be {@code null}
184         * @throws NamingException if a problem occurred closing the specified JNDI Context
185         */
186        public static void close(final Context context) throws NamingException {
187            if (context != null) {
188                context.close();
189            }
190        }
191    
192    }