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