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.SQLException; 26 import java.sql.Statement; 27 28 /** 29 * Helper class for closing resources. 30 */ 31 public final class Closer { 32 33 private Closer() { 34 } 35 36 /** 37 * Closes the specified {@code Closeable} (stream or reader/writer), 38 * ignoring any exceptions thrown by the close operation. 39 * 40 * @param closeable the resource to close, may be {@code null} 41 */ 42 public static void closeSilently(final Closeable closeable) { 43 try { 44 close(closeable); 45 } catch (final Exception ignored) { 46 // ignored 47 } 48 } 49 50 /** 51 * Closes the specified {@code Closeable} (stream or reader/writer). 52 * 53 * @param closeable the resource to close, may be {@code null} 54 * @throws IOException if a problem occurred closing the specified resource 55 */ 56 public static void close(final Closeable closeable) throws IOException { 57 if (closeable != null) { 58 closeable.close(); 59 } 60 } 61 62 /** 63 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 64 * 65 * @param serverSocket the resource to close, may be {@code null} 66 */ 67 public static void closeSilently(final ServerSocket serverSocket) { 68 try { 69 close(serverSocket); 70 } catch (final Exception ignored) { 71 // ignored 72 } 73 } 74 75 /** 76 * Closes the specified resource. 77 * 78 * @param serverSocket the resource to close, may be {@code null} 79 * @throws IOException if a problem occurred closing the specified resource 80 */ 81 public static void close(final ServerSocket serverSocket) throws IOException { 82 if (serverSocket != null) { 83 serverSocket.close(); 84 } 85 } 86 87 /** 88 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 89 * 90 * @param datagramSocket the resource to close, may be {@code null} 91 */ 92 public static void closeSilently(final DatagramSocket datagramSocket) { 93 try { 94 close(datagramSocket); 95 } catch (final Exception ignored) { 96 // ignored 97 } 98 } 99 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 }