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 import javax.naming.Context; 28 import javax.naming.NamingException; 29 30 /** 31 * Helper class for closing resources. 32 */ 33 public final class Closer { 34 35 private Closer() { 36 } 37 38 /** 39 * Closes the specified {@code Closeable} (stream or reader/writer), 40 * ignoring any exceptions thrown by the close operation. 41 * 42 * @param closeable the resource to close, may be {@code null} 43 */ 44 public static void closeSilent(Closeable closeable) { 45 try { 46 if (closeable != null) { 47 closeable.close(); 48 } 49 } catch (final Exception ignored) { 50 // ignored 51 } 52 } 53 54 /** 55 * Closes the specified {@code Closeable} (stream or reader/writer). 56 * 57 * @param closeable the resource to close, may be {@code null} 58 * @throws IOException if a problem occurred closing the specified resource 59 */ 60 public static void close(Closeable closeable) throws IOException { 61 if (closeable != null) { 62 closeable.close(); 63 } 64 } 65 66 /** 67 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 68 * 69 * @param serverSocket the resource to close, may be {@code null} 70 */ 71 public static void closeSilent(ServerSocket serverSocket) { 72 try { 73 if (serverSocket != null) { 74 serverSocket.close(); 75 } 76 } catch (final Exception ignored) { 77 // ignored 78 } 79 } 80 81 /** 82 * Closes the specified resource. 83 * 84 * @param serverSocket the resource to close, may be {@code null} 85 * @throws IOException if a problem occurred closing the specified resource 86 */ 87 public static void close(ServerSocket serverSocket) throws IOException { 88 if (serverSocket != null) { 89 serverSocket.close(); 90 } 91 } 92 93 /** 94 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 95 * 96 * @param datagramSocket the resource to close, may be {@code null} 97 */ 98 public static void closeSilent(DatagramSocket datagramSocket) { 99 try { 100 if (datagramSocket != null) { 101 datagramSocket.close(); 102 } 103 } catch (final Exception ignored) { 104 // ignored 105 } 106 } 107 108 /** 109 * Closes the specified resource. 110 * 111 * @param datagramSocket the resource to close, may be {@code null} 112 * @throws IOException if a problem occurred closing the specified resource 113 */ 114 public static void close(DatagramSocket datagramSocket) throws IOException { 115 if (datagramSocket != null) { 116 datagramSocket.close(); 117 } 118 } 119 120 /** 121 * Closes the specified {@code Statement}, ignoring any exceptions thrown by 122 * the close operation. 123 * 124 * @param statement the resource to close, may be {@code null} 125 */ 126 public static void closeSilent(Statement statement) { 127 try { 128 if (statement != null) { 129 statement.close(); 130 } 131 } catch (final Exception ignored) { 132 // ignored 133 } 134 } 135 136 /** 137 * Closes the specified {@code Statement}. 138 * 139 * @param statement the resource to close, may be {@code null} 140 * @throws SQLException if a problem occurred closing the specified resource 141 */ 142 public static void close(Statement statement) throws SQLException { 143 if (statement != null) { 144 statement.close(); 145 } 146 } 147 148 /** 149 * Closes the specified {@code Connection}, ignoring any exceptions thrown 150 * by the close operation. 151 * 152 * @param connection the resource to close, may be {@code null} 153 */ 154 public static void closeSilent(Connection connection) { 155 try { 156 if (connection != null) { 157 connection.close(); 158 } 159 } catch (final Exception ignored) { 160 // ignored 161 } 162 } 163 164 /** 165 * Closes the specified {@code Connection}. 166 * 167 * @param connection the resource to close, may be {@code null} 168 * @throws SQLException if a problem occurred closing the specified resource 169 */ 170 public static void close(Connection connection) throws SQLException { 171 if (connection != null) { 172 connection.close(); 173 } 174 } 175 176 /** 177 * Closes the specified {@code Context}, ignoring any exceptions thrown by the close operation. 178 * 179 * @param context the JNDI Context to close, may be {@code null} 180 */ 181 public static void closeSilent(Context context) { 182 try { 183 close(context); 184 } catch (final NamingException ignored) { 185 // ignored 186 } 187 } 188 189 /** 190 * Closes the specified {@code Context}. 191 * 192 * @param context the JNDI Context to close, may be {@code null} 193 * @throws NamingException if a problem occurred closing the specified JNDI Context 194 */ 195 public static void close(Context context) throws NamingException { 196 if (context != null) { 197 context.close(); 198 } 199 } 200 201 }