logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1134740 - in /logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender: AbstractManager.java OutputStreamManager.java
Date Sat, 11 Jun 2011 20:15:07 GMT
Author: rgoers
Date: Sat Jun 11 20:15:07 2011
New Revision: 1134740

URL: http://svn.apache.org/viewvc?rev=1134740&view=rev
Log:
Allow other Manager types besides OutputStreamManagers

Added:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java
      - copied, changed from r1133258, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
Modified:
    logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java

Copied: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java
(from r1133258, logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java)
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java?p2=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java&p1=logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java&r1=1133258&r2=1134740&rev=1134740&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java
Sat Jun 11 20:15:07 2011
@@ -19,22 +19,19 @@ package org.apache.logging.log4j.core.ap
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.internal.StatusLogger;
 
-import java.io.IOException;
-import java.io.OutputStream;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
 /**
- * Manage an OutputStream so that it can be shared by multiple Appenders and will
- * allow appenders to reconfigure without requiring a new stream.
+ * Base class used to register managers.
  */
-public class OutputStreamManager {
+public abstract class AbstractManager {
 
     // Need to lock that map instead of using a ConcurrentMap due to stop removing the
     // manager from the map and closing the stream, requiring the whole stop method to be
locked.
-    private static Map<String, OutputStreamManager> map = new HashMap<String, OutputStreamManager>();
+    private static Map<String, AbstractManager> map = new HashMap<String, AbstractManager>();
 
     private static Lock lock = new ReentrantLock();
 
@@ -43,26 +40,22 @@ public class OutputStreamManager {
      */
     protected static final Logger logger = StatusLogger.getLogger();
 
-    private OutputStream os;
     private String name;
 
     private int count;
 
-    private byte[] footer = null;
-
     public StringBuilder buffer = new StringBuilder();
 
-    public static OutputStreamManager getManager(String name, ManagerFactory<OutputStreamManager,
Object> factory,
+    public static <T extends AbstractManager> T getManager(String name, ManagerFactory<T,
Object> factory,
                                                  Object data) {
         lock.lock();
         try {
-            OutputStreamManager manager = map.get(name);
+            T manager = (T) map.get(name);
             if (manager == null) {
                 manager = factory.createManager(name, data);
                 map.put(name, manager);
             }
             manager.count++;
-            //System.out.println("Using manager " + name + " " + manager.count);
             return manager;
         } finally {
             lock.unlock();
@@ -78,104 +71,29 @@ public class OutputStreamManager {
         }
     }
 
-    public OutputStreamManager(OutputStream os, String streamName) {
-        this.os = os;
-        this.name = streamName;
+    protected AbstractManager(String name) {
+        this.name = name;
     }
 
-    public synchronized void setHeader(byte[] header) {
-        if (header != null) {
-            try {
-                this.os.write(header, 0, header.length);
-            } catch (IOException ioe) {
-                logger.error("Unable to write header", ioe);
-            }
-        }
-    }
+    public abstract void releaseSub();
 
-    public synchronized void setFooter(byte[] footer) {
-        if (footer != null) {
-            this.footer = footer;
-        }
+    protected int getCount() {
+        return count;
     }
 
     public void release() {
         lock.lock();
         try {
             --count;
-            //System.out.println("Released " + name + " " + count);
             if (count <= 0) {
                 map.remove(name);
-                if (footer != null) {
-                    write(footer);
-                }
-                close();
+                releaseSub();
             }
         } finally {
             lock.unlock();
         }
     }
 
-    public boolean isOpen() {
-        return count > 0;
-    }
-
-    protected OutputStream getOutputStream() {
-        return os;
-    }
-
-    protected void setOutputStream(OutputStream os) {
-        this.os = os;
-    }
-
-    /**
-     * Some output streams synchronize writes while others do not. Synchronizing here insures
that
-     * log events won't be intertwined.
-     * @param bytes The serialized Log event.
-     * @param offset The offset into the byte array.
-     * @param length The number of bytes to write.
-     * @throws AppenderRuntimeException if an error occurs.
-     */
-    protected synchronized void write(byte[] bytes, int offset, int length)  {
-        //System.out.println("write " + count);
-        try {
-            os.write(bytes, offset, length);
-        } catch (IOException ex) {
-            String msg = "Error writing to stream " + name;
-            throw new AppenderRuntimeException(msg, ex);
-        }
-    }
-
-    /**
-     * Some output streams synchronize writes while others do not. Synchronizing here insures
that
-     * log events won't be intertwined.
-     * @param bytes The serialized Log event.
-     * @throws AppenderRuntimeException if an error occurs.
-     */
-    protected void write(byte[] bytes)  {
-        write(bytes, 0, bytes.length);
-    }
-
-    protected void close() {
-        if (os == System.out || os == System.err) {
-            return;
-        }
-        try {
-            os.close();
-        } catch (IOException ex) {
-            logger.error("Unable to close stream " + name + ". " + ex);
-        }
-    }
-
-    public void flush() {
-        try {
-            os.flush();
-        } catch (IOException ex) {
-            String msg = "Error flushing stream " + name;
-            throw new AppenderRuntimeException(msg, ex);
-        }
-    }
-
     public String getName() {
         return name;
     }

Modified: logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java?rev=1134740&r1=1134739&r2=1134740&view=diff
==============================================================================
--- logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
(original)
+++ logging/log4j/branches/BRANCH_2_0_EXPERIMENTAL/rgoers/log4j2-core/src/main/java/org/apache/logging/log4j/core/appender/OutputStreamManager.java
Sat Jun 11 20:15:07 2011
@@ -16,37 +16,16 @@
  */
 package org.apache.logging.log4j.core.appender;
 
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.internal.StatusLogger;
-
 import java.io.IOException;
 import java.io.OutputStream;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
 
 /**
  * Manage an OutputStream so that it can be shared by multiple Appenders and will
  * allow appenders to reconfigure without requiring a new stream.
  */
-public class OutputStreamManager {
-
-    // Need to lock that map instead of using a ConcurrentMap due to stop removing the
-    // manager from the map and closing the stream, requiring the whole stop method to be
locked.
-    private static Map<String, OutputStreamManager> map = new HashMap<String, OutputStreamManager>();
-
-    private static Lock lock = new ReentrantLock();
-
-    /**
-     * Allow subclasses access to the status logger without creating another instance.
-     */
-    protected static final Logger logger = StatusLogger.getLogger();
+public class OutputStreamManager extends AbstractManager {
 
     private OutputStream os;
-    private String name;
-
-    private int count;
 
     private byte[] footer = null;
 
@@ -54,33 +33,12 @@ public class OutputStreamManager {
 
     public static OutputStreamManager getManager(String name, ManagerFactory<OutputStreamManager,
Object> factory,
                                                  Object data) {
-        lock.lock();
-        try {
-            OutputStreamManager manager = map.get(name);
-            if (manager == null) {
-                manager = factory.createManager(name, data);
-                map.put(name, manager);
-            }
-            manager.count++;
-            //System.out.println("Using manager " + name + " " + manager.count);
-            return manager;
-        } finally {
-            lock.unlock();
-        }
-    }
-
-    public static boolean hasManager(String name) {
-        lock.lock();
-        try {
-            return map.containsKey(name);
-        } finally {
-            lock.unlock();
-        }
+        return AbstractManager.getManager(name, factory, data);
     }
 
     public OutputStreamManager(OutputStream os, String streamName) {
+        super(streamName);
         this.os = os;
-        this.name = streamName;
     }
 
     public synchronized void setHeader(byte[] header) {
@@ -99,25 +57,15 @@ public class OutputStreamManager {
         }
     }
 
-    public void release() {
-        lock.lock();
-        try {
-            --count;
-            //System.out.println("Released " + name + " " + count);
-            if (count <= 0) {
-                map.remove(name);
-                if (footer != null) {
-                    write(footer);
-                }
-                close();
-            }
-        } finally {
-            lock.unlock();
+    public void releaseSub() {
+        if (footer != null) {
+            write(footer);
         }
+        close();
     }
 
     public boolean isOpen() {
-        return count > 0;
+        return getCount() > 0;
     }
 
     protected OutputStream getOutputStream() {
@@ -141,7 +89,7 @@ public class OutputStreamManager {
         try {
             os.write(bytes, offset, length);
         } catch (IOException ex) {
-            String msg = "Error writing to stream " + name;
+            String msg = "Error writing to stream " + getName();
             throw new AppenderRuntimeException(msg, ex);
         }
     }
@@ -163,7 +111,7 @@ public class OutputStreamManager {
         try {
             os.close();
         } catch (IOException ex) {
-            logger.error("Unable to close stream " + name + ". " + ex);
+            logger.error("Unable to close stream " + getName() + ". " + ex);
         }
     }
 
@@ -171,12 +119,8 @@ public class OutputStreamManager {
         try {
             os.flush();
         } catch (IOException ex) {
-            String msg = "Error flushing stream " + name;
+            String msg = "Error flushing stream " + getName();
             throw new AppenderRuntimeException(msg, ex);
         }
     }
-
-    public String getName() {
-        return name;
-    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message