logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpo...@apache.org
Subject svn commit: r1517220 - in /logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core: appender/db/jdbc/JDBCDatabaseManager.java config/plugins/PluginManager.java helpers/Closer.java jmx/LoggerContextAdmin.java
Date Sun, 25 Aug 2013 05:53:36 GMT
Author: rpopma
Date: Sun Aug 25 05:53:35 2013
New Revision: 1517220

URL: http://svn.apache.org/r1517220
Log:
Introduce helper class to make closing resources less verbose.

Added:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Closer.java
  (with props)
Modified:
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JDBCDatabaseManager.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java
    logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JDBCDatabaseManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JDBCDatabaseManager.java?rev=1517220&r1=1517219&r2=1517220&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JDBCDatabaseManager.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/JDBCDatabaseManager.java
Sun Aug 25 05:53:35 2013
@@ -28,6 +28,7 @@ import org.apache.logging.log4j.core.Log
 import org.apache.logging.log4j.core.appender.AppenderLoggingException;
 import org.apache.logging.log4j.core.appender.ManagerFactory;
 import org.apache.logging.log4j.core.appender.db.AbstractDatabaseManager;
+import org.apache.logging.log4j.core.helpers.Closer;
 import org.apache.logging.log4j.core.layout.PatternLayout;
 
 /**
@@ -60,13 +61,9 @@ public final class JDBCDatabaseManager e
     @Override
     protected void disconnectInternal() throws SQLException {
         try {
-            if (this.statement != null && !this.statement.isClosed()) {
-                this.statement.close();
-            }
+            Closer.close(this.statement);
         } finally {
-            if (this.connection != null && !this.connection.isClosed()) {
-                this.connection.close();
-            }
+            Closer.close(this.connection);
         }
     }
 
@@ -109,9 +106,7 @@ public final class JDBCDatabaseManager e
             throw new AppenderLoggingException("Failed to insert record for log event in
JDBC manager: " +
                     e.getMessage(), e);
         } finally {
-            if (reader != null) {
-                reader.close();
-            }
+            Closer.closeSilent(reader);
         }
     }
 

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java?rev=1517220&r1=1517219&r2=1517220&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/config/plugins/PluginManager.java
Sun Aug 25 05:53:35 2013
@@ -34,6 +34,7 @@ import java.util.concurrent.ConcurrentMa
 import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.helpers.Closer;
 import org.apache.logging.log4j.core.helpers.Loader;
 import org.apache.logging.log4j.status.StatusLogger;
 
@@ -246,11 +247,7 @@ public class PluginManager {
                 LOGGER.warn("Unable to preload plugins", ex);
                 return null;
             } finally {
-                try {
-                    dis.close();
-                } catch (final Exception ignored) {
-                    // nothing we can do here...
-                }
+                Closer.closeSilent(dis);
             }
         }
         return map.size() == 0 ? null : map;
@@ -281,11 +278,7 @@ public class PluginManager {
         } catch (final Exception ex) {
             ex.printStackTrace();
         } finally {
-            try {
-                dos.close();
-            } catch (final Exception ignored) {
-                // nothing we can do here...
-            }
+            Closer.closeSilent(dos);
         }
     }
 

Added: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Closer.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Closer.java?rev=1517220&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Closer.java
(added)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Closer.java
Sun Aug 25 05:53:35 2013
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.core.helpers;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * Helper class for closing resources.
+ */
+public class Closer {
+
+    /**
+     * Closes the specified {@code Closeable} (stream or reader/writer),
+     * ignoring any exceptions thrown by the close operation.
+     * 
+     * @param closeable the resource to close, may be {@code null}
+     */
+    public static void closeSilent(Closeable closeable) {
+        try {
+            if (closeable != null) {
+                closeable.close();
+            }
+        } catch (final Exception ignored) {
+            // ignored
+        }
+    }
+
+    /**
+     * Closes the specified {@code Closeable} (stream or reader/writer).
+     * 
+     * @param closeable the resource to close, may be {@code null}
+     * @throws IOException if a problem occurred closing the specified resource
+     */
+    public static void close(Closeable closeable) throws IOException {
+        if (closeable != null) {
+            closeable.close();
+        }
+    }
+
+    /**
+     * Closes the specified {@code Statement}, ignoring any exceptions thrown by
+     * the close operation.
+     * 
+     * @param statement the resource to close, may be {@code null}
+     */
+    public static void closeSilent(Statement statement) {
+        try {
+            if (statement != null) {
+                statement.close();
+            }
+        } catch (final Exception ignored) {
+            // ignored
+        }
+    }
+
+    /**
+     * Closes the specified {@code Statement}.
+     * 
+     * @param statement the resource to close, may be {@code null}
+     * @throws SQLException if a problem occurred closing the specified resource
+     */
+    public static void close(Statement statement) throws SQLException {
+        if (statement != null) {
+            statement.close();
+        }
+    }
+
+    /**
+     * Closes the specified {@code Connection}, ignoring any exceptions thrown
+     * by the close operation.
+     * 
+     * @param connection the resource to close, may be {@code null}
+     */
+    public static void closeSilent(Connection connection) {
+        try {
+            if (connection != null) {
+                connection.close();
+            }
+        } catch (final Exception ignored) {
+            // ignored
+        }
+    }
+
+    /**
+     * Closes the specified {@code Connection}.
+     * 
+     * @param connection the resource to close, may be {@code null}
+     * @throws SQLException if a problem occurred closing the specified resource
+     */
+    public static void close(Connection connection) throws SQLException {
+        if (connection != null) {
+            connection.close();
+        }
+    }
+
+}

Propchange: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/helpers/Closer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java?rev=1517220&r1=1517219&r2=1517220&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
(original)
+++ logging/log4j/log4j2/trunk/core/src/main/java/org/apache/logging/log4j/core/jmx/LoggerContextAdmin.java
Sun Aug 25 05:53:35 2013
@@ -44,6 +44,7 @@ import org.apache.logging.log4j.core.con
 import org.apache.logging.log4j.core.config.ConfigurationFactory.ConfigurationSource;
 import org.apache.logging.log4j.core.helpers.Assert;
 import org.apache.logging.log4j.core.helpers.Charsets;
+import org.apache.logging.log4j.core.helpers.Closer;
 import org.apache.logging.log4j.status.StatusLogger;
 
 /**
@@ -208,20 +209,8 @@ public class LoggerContextAdmin extends 
             }
             return result.toString();
         } finally {
-            try {
-                if (in != null) {
-                    in.close();
-                }
-            } catch (final Exception ignored) {
-                // ignored
-            }
-            try {
-                if (reader != null) {
-                    reader.close();
-                }
-            } catch (final Exception ignored) {
-                // ignored
-            }
+            Closer.closeSilent(in);
+            Closer.closeSilent(reader);
         }
     }
 



Mime
View raw message