[jboss-svn-commits] JBoss Common SVN: r3354 - in jboss-logmanager/trunk/src/main/java/org/jboss/logmanager: handlers and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jul 9 00:37:08 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-07-09 00:37:08 -0400 (Thu, 09 Jul 2009)
New Revision: 3354

Removed:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AutoFlushingHandler.java
Modified:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AsyncHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/FileHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java
Log:
Fix a number of locking, performance, and race condition issues

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java	2009-07-09 04:01:13 UTC (rev 3353)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/ExtHandler.java	2009-07-09 04:37:08 UTC (rev 3354)
@@ -54,8 +54,10 @@
 
     /** {@inheritDoc} */
     public final void publish(final LogRecord record) {
-        publish((record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME));
-        if (autoFlush) flush();
+        if (record != null && isLoggable(record)) {
+            final ExtLogRecord extRecord = (record instanceof ExtLogRecord) ? (ExtLogRecord) record : new ExtLogRecord(record, LOGGER_CLASS_NAME);
+            doPublish(extRecord);
+        }
     }
 
     /**
@@ -68,9 +70,24 @@
      *
      * @param record the log record to publish
      */
-    public abstract void publish(final ExtLogRecord record);
+    public final void publish(final ExtLogRecord record) {
+        if (record != null && isLoggable(record)) {
+            doPublish(record);
+        }
+    }
 
     /**
+     * Do the actual work of publication; the record will have been filtered already.  The default implementation
+     * does nothing except to flush if the {@code autoFlush} property is set to {@code true}; if this behavior is to be
+     * preserved in a subclass then this method should be called after the record is physically written.
+     *
+     * @param record the log record to publish
+     */
+    protected void doPublish(final ExtLogRecord record) {
+        if (autoFlush) flush();
+    }
+
+    /**
      * Add a sub-handler to this handler.  Some handler types do not utilize sub-handlers.
      *
      * @param handler the handler to add

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AsyncHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AsyncHandler.java	2009-07-09 04:01:13 UTC (rev 3353)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AsyncHandler.java	2009-07-09 04:37:08 UTC (rev 3354)
@@ -106,7 +106,7 @@
     }
 
     /** {@inheritDoc} */
-    public void publish(final ExtLogRecord record) {
+    protected void doPublish(final ExtLogRecord record) {
         final Queue<ExtLogRecord> recordQueue = this.recordQueue;
         boolean intr = Thread.interrupted();
         // prepare record to move to another thread

Deleted: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AutoFlushingHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AutoFlushingHandler.java	2009-07-09 04:01:13 UTC (rev 3353)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/AutoFlushingHandler.java	2009-07-09 04:37:08 UTC (rev 3354)
@@ -1,59 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-
-package org.jboss.logmanager.handlers;
-
-import org.jboss.logmanager.ExtHandler;
-import org.jboss.logmanager.ExtLogRecord;
-
-import java.util.logging.Handler;
-
-/**
- * A handler which wraps other handlers, forcing a flush after each successful message publication.
- */
-public class AutoFlushingHandler extends ExtHandler {
-
-    /**
-     * Construct a new instance.
-     */
-    public AutoFlushingHandler() {
-    }
-
-    /** {@inheritDoc} */
-    public void publish(final ExtLogRecord record) {
-        for (Handler handler : handlers) {
-            handler.publish(record);
-            handler.flush();
-        }
-    }
-
-    /** {@inheritDoc} */
-    public void flush() {
-        for (Handler handler : handlers) {
-            handler.flush();
-        }
-    }
-
-    /** {@inheritDoc}  This implementation does nothing. */
-    public void close() throws SecurityException {
-    }
-}

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java	2009-07-09 04:01:13 UTC (rev 3353)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/ConsoleHandler.java	2009-07-09 04:37:08 UTC (rev 3354)
@@ -92,7 +92,7 @@
      * @param formatter the formatter to use
      */
     public ConsoleHandler(final Target target, final Formatter formatter) {
-        super(targets.get(target), formatter);
+        super(wrap(targets.get(target)), formatter);
     }
 
     /**
@@ -104,12 +104,16 @@
         setOutputStream(targets.get(target));
     }
 
+    private static OutputStream wrap(final OutputStream outputStream) {
+        return outputStream == null ?
+                null :
+                outputStream instanceof UncloseableOutputStream ?
+                        outputStream :
+                        new UncloseableOutputStream(outputStream);
+    }
+
     /** {@inheritDoc} */
     public void setOutputStream(final OutputStream outputStream) {
-        if (outputStream == null || outputStream instanceof UncloseableOutputStream) {
-            super.setOutputStream(outputStream);
-        } else {
-            super.setOutputStream(new UncloseableOutputStream(outputStream));
-        }
+        super.setOutputStream(wrap(outputStream));
     }
 }

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/FileHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/FileHandler.java	2009-07-09 04:01:13 UTC (rev 3353)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/FileHandler.java	2009-07-09 04:37:08 UTC (rev 3354)
@@ -84,6 +84,7 @@
         synchronized (outputLock) {
             if (file == null) {
                 setOutputStream(null);
+                return;
             }
             final File parentFile = file.getParentFile();
             if (parentFile != null) {
@@ -94,6 +95,7 @@
             try {
                 setOutputStream(fos);
                 this.file = file;
+                ok = true;
             } finally {
                 if (! ok) {
                     safeClose(fos);

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java	2009-07-09 04:01:13 UTC (rev 3353)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/NullHandler.java	2009-07-09 04:37:08 UTC (rev 3354)
@@ -31,8 +31,7 @@
 public final class NullHandler extends ExtHandler {
 
     /** {@inheritDoc} */
-    public void publish(final ExtLogRecord record) {
-        isLoggable(record);
+    protected void doPublish(final ExtLogRecord record) {
     }
 
     /** {@inheritDoc} */

Modified: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java	2009-07-09 04:01:13 UTC (rev 3353)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/handlers/WriterHandler.java	2009-07-09 04:37:08 UTC (rev 3354)
@@ -41,34 +41,34 @@
     protected final Object outputLock = new Object();
     private Writer writer;
 
-    /**
-     * Publish a log record.
-     *
-     * @param record the log record to publish
-     */
-    public void publish(final ExtLogRecord record) {
-        if (isLoggable(record)) {
-            final String formatted;
-            final Formatter formatter = getFormatter();
-            try {
-                formatted = formatter.format(record);
-            } catch (Exception ex) {
-                reportError("Formatting error", ex, ErrorManager.FORMAT_FAILURE);
-                return;
-            }
-            try {
-                synchronized (outputLock) {
-                    final Writer writer = this.writer;
-                    if (writer == null) {
-                        return;
-                    }
-                    preWrite(record);
-                    writer.write(formatted);
+    /** {@inheritDoc} */
+    protected void doPublish(final ExtLogRecord record) {
+        final String formatted;
+        final Formatter formatter = getFormatter();
+        try {
+            formatted = formatter.format(record);
+        } catch (Exception ex) {
+            reportError("Formatting error", ex, ErrorManager.FORMAT_FAILURE);
+            return;
+        }
+        if (formatted.length() == 0) {
+            // nothing to write; don't bother
+            return;
+        }
+        try {
+            synchronized (outputLock) {
+                final Writer writer = this.writer;
+                if (writer == null) {
+                    return;
                 }
-            } catch (Exception ex) {
-                reportError("Error writing log message", ex, ErrorManager.WRITE_FAILURE);
-                return;
+                preWrite(record);
+                writer.write(formatted);
+                // only flush if something was written
+                super.doPublish(record);
             }
+        } catch (Exception ex) {
+            reportError("Error writing log message", ex, ErrorManager.WRITE_FAILURE);
+            return;
         }
     }
 




More information about the jboss-svn-commits mailing list