View Javadoc

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