1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache license, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the license for the specific language governing permissions and 15 * limitations under the license. 16 */ 17 18 package org.apache.logging.log4j.core.util; 19 20 import java.io.Closeable; 21 import java.io.IOException; 22 import java.net.DatagramSocket; 23 import java.net.ServerSocket; 24 import java.sql.Connection; 25 import java.sql.ResultSet; 26 import java.sql.SQLException; 27 import java.sql.Statement; 28 29 /** 30 * Helper class for closing resources. 31 */ 32 public final class Closer { 33 34 private Closer() { 35 } 36 37 /** 38 * Closes the specified {@code Closeable} (stream or reader/writer), 39 * ignoring any exceptions thrown by the close operation. 40 * 41 * @param closeable the resource to close, may be {@code null} 42 */ 43 public static void closeSilently(final Closeable closeable) { 44 try { 45 close(closeable); 46 } catch (final Exception ignored) { 47 // ignored 48 } 49 } 50 51 /** 52 * Closes the specified {@code Closeable} (stream or reader/writer). 53 * 54 * @param closeable the resource to close, may be {@code null} 55 * @throws IOException if a problem occurred closing the specified resource 56 */ 57 public static void close(final Closeable closeable) throws IOException { 58 if (closeable != null) { 59 closeable.close(); 60 } 61 } 62 63 /** 64 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 65 * 66 * @param serverSocket the resource to close, may be {@code null} 67 */ 68 public static void closeSilently(final ServerSocket serverSocket) { 69 try { 70 close(serverSocket); 71 } catch (final Exception ignored) { 72 // ignored 73 } 74 } 75 76 /** 77 * Closes the specified resource. 78 * 79 * @param serverSocket the resource to close, may be {@code null} 80 * @throws IOException if a problem occurred closing the specified resource 81 */ 82 public static void close(final ServerSocket serverSocket) throws IOException { 83 if (serverSocket != null) { 84 serverSocket.close(); 85 } 86 } 87 88 /** 89 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 90 * 91 * @param datagramSocket the resource to close, may be {@code null} 92 */ 93 public static void closeSilently(final DatagramSocket datagramSocket) { 94 try { 95 close(datagramSocket); 96 } catch (final Exception ignored) { 97 // ignored 98 } 99 } 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 }