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 import javax.naming.Context; 028 import javax.naming.NamingException; 029 030 /** 031 * Helper class for closing resources. 032 */ 033 public final class Closer { 034 035 private Closer() { 036 } 037 038 /** 039 * Closes the specified {@code Closeable} (stream or reader/writer), 040 * ignoring any exceptions thrown by the close operation. 041 * 042 * @param closeable the resource to close, may be {@code null} 043 */ 044 public static void closeSilent(Closeable closeable) { 045 try { 046 if (closeable != null) { 047 closeable.close(); 048 } 049 } catch (final Exception ignored) { 050 // ignored 051 } 052 } 053 054 /** 055 * Closes the specified {@code Closeable} (stream or reader/writer). 056 * 057 * @param closeable the resource to close, may be {@code null} 058 * @throws IOException if a problem occurred closing the specified resource 059 */ 060 public static void close(Closeable closeable) throws IOException { 061 if (closeable != null) { 062 closeable.close(); 063 } 064 } 065 066 /** 067 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 068 * 069 * @param serverSocket the resource to close, may be {@code null} 070 */ 071 public static void closeSilent(ServerSocket serverSocket) { 072 try { 073 if (serverSocket != null) { 074 serverSocket.close(); 075 } 076 } catch (final Exception ignored) { 077 // ignored 078 } 079 } 080 081 /** 082 * Closes the specified resource. 083 * 084 * @param serverSocket the resource to close, may be {@code null} 085 * @throws IOException if a problem occurred closing the specified resource 086 */ 087 public static void close(ServerSocket serverSocket) throws IOException { 088 if (serverSocket != null) { 089 serverSocket.close(); 090 } 091 } 092 093 /** 094 * Closes the specified resource, ignoring any exceptions thrown by the close operation. 095 * 096 * @param datagramSocket the resource to close, may be {@code null} 097 */ 098 public static void closeSilent(DatagramSocket datagramSocket) { 099 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 }