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 018package org.apache.logging.log4j.core.helpers; 019 020import java.io.Closeable; 021import java.io.IOException; 022import java.sql.Connection; 023import java.sql.SQLException; 024import java.sql.Statement; 025 026/** 027 * Helper class for closing resources. 028 */ 029public class Closer { 030 031 /** 032 * Closes the specified {@code Closeable} (stream or reader/writer), 033 * ignoring any exceptions thrown by the close operation. 034 * 035 * @param closeable the resource to close, may be {@code null} 036 */ 037 public static void closeSilent(Closeable closeable) { 038 try { 039 if (closeable != null) { 040 closeable.close(); 041 } 042 } catch (final Exception ignored) { 043 // ignored 044 } 045 } 046 047 /** 048 * Closes the specified {@code Closeable} (stream or reader/writer). 049 * 050 * @param closeable the resource to close, may be {@code null} 051 * @throws IOException if a problem occurred closing the specified resource 052 */ 053 public static void close(Closeable closeable) throws IOException { 054 if (closeable != null) { 055 closeable.close(); 056 } 057 } 058 059 /** 060 * Closes the specified {@code Statement}, ignoring any exceptions thrown by 061 * the close operation. 062 * 063 * @param statement the resource to close, may be {@code null} 064 */ 065 public static void closeSilent(Statement statement) { 066 try { 067 if (statement != null) { 068 statement.close(); 069 } 070 } catch (final Exception ignored) { 071 // ignored 072 } 073 } 074 075 /** 076 * Closes the specified {@code Statement}. 077 * 078 * @param statement the resource to close, may be {@code null} 079 * @throws SQLException if a problem occurred closing the specified resource 080 */ 081 public static void close(Statement statement) throws SQLException { 082 if (statement != null) { 083 statement.close(); 084 } 085 } 086 087 /** 088 * Closes the specified {@code Connection}, ignoring any exceptions thrown 089 * by the close operation. 090 * 091 * @param connection the resource to close, may be {@code null} 092 */ 093 public static void closeSilent(Connection connection) { 094 try { 095 if (connection != null) { 096 connection.close(); 097 } 098 } catch (final Exception ignored) { 099 // ignored 100 } 101 } 102 103 /** 104 * Closes the specified {@code Connection}. 105 * 106 * @param connection the resource to close, may be {@code null} 107 * @throws SQLException if a problem occurred closing the specified resource 108 */ 109 public static void close(Connection connection) throws SQLException { 110 if (connection != null) { 111 connection.close(); 112 } 113 } 114 115}