[logback-dev] svn commit: r944 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic main/java/ch/qos/logback/classic/spi test/java/ch/qos/logback/classic/spi

noreply.seb at qos.ch noreply.seb at qos.ch
Fri Nov 17 15:34:32 CET 2006


Author: seb
Date: Fri Nov 17 15:34:32 2006
New Revision: 944

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ContextListener.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/EventType.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LogbackEvent.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/BasicContextListener.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/ContextListenerTest.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java

Log:
first implementation of a ContextListener and LogbackEvent objects.

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/LoggerContext.java	Fri Nov 17 15:34:32 2006
@@ -10,12 +10,17 @@
 
 package ch.qos.logback.classic;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.List;
 
 import org.slf4j.ILoggerFactory;
 import org.slf4j.Marker;
 
+import ch.qos.logback.classic.spi.ContextListener;
+import ch.qos.logback.classic.spi.EventType;
+import ch.qos.logback.classic.spi.LogbackEvent;
 import ch.qos.logback.classic.spi.LoggerContextRemoteView;
 import ch.qos.logback.classic.spi.TurboFilterAttachable;
 import ch.qos.logback.classic.spi.TurboFilterAttachableImpl;
@@ -36,6 +41,7 @@
   final Logger root;
   private int size;
   private int noAppenderWarning = 0;
+  private List<ContextListener> listenerList;
 
   // We want loggerCache to be synchronized so Hashtable is a good choice. In
   // practice, it
@@ -164,9 +170,8 @@
   }
 
   public void shutdownAndReset() {
-
     root.recursiveReset();
-
+    notifyListeners(EventType.CONTEXT_RESTART);
   }
 
   public void addTurboFilter(TurboFilter newFilter) {
@@ -200,4 +205,24 @@
     }
     return cfai.getFirstTurboFilter();
   }
+  
+  public void addListener(ContextListener listener) {
+    if (listenerList == null) {
+      listenerList = new ArrayList<ContextListener>();
+    }
+    listenerList.add(listener);
+  }
+  
+  public void removeListener(ContextListener listener) {
+    if (listenerList != null) {
+      listenerList.remove(listener);
+    }
+  }
+  
+  private void notifyListeners(EventType eventType) {
+    LogbackEvent event = new LogbackEvent(this, eventType);
+    for (ContextListener listener: listenerList) {
+      listener.update(event);
+    }
+  }
 }

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ContextListener.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/ContextListener.java	Fri Nov 17 15:34:32 2006
@@ -0,0 +1,9 @@
+package ch.qos.logback.classic.spi;
+
+import java.util.EventListener;
+
+public interface ContextListener extends EventListener {
+  
+  public void update(LogbackEvent logbackEvent);
+
+}

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/EventType.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/EventType.java	Fri Nov 17 15:34:32 2006
@@ -0,0 +1,5 @@
+package ch.qos.logback.classic.spi;
+
+public enum EventType {
+  CONTEXT_RESTART;
+}

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LogbackEvent.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LogbackEvent.java	Fri Nov 17 15:34:32 2006
@@ -0,0 +1,19 @@
+package ch.qos.logback.classic.spi;
+
+import java.util.EventObject;
+
+public class LogbackEvent extends EventObject {
+
+  private static final long serialVersionUID = -2688530978380786446L;
+
+  private final EventType type;
+  
+  public LogbackEvent(Object source, EventType type) {
+    super(source);
+    this.type = type;
+  }
+  
+  public EventType getType() {
+    return type;
+  }
+}

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/BasicContextListener.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/BasicContextListener.java	Fri Nov 17 15:34:32 2006
@@ -0,0 +1,12 @@
+package ch.qos.logback.classic.spi;
+
+public class BasicContextListener implements ContextListener {
+
+  boolean updated = false;
+  LogbackEvent lastEvent;
+  
+  public void update(LogbackEvent logbackEvent) {
+    updated = true;
+    lastEvent = logbackEvent;
+  }
+}

Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/ContextListenerTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/spi/ContextListenerTest.java	Fri Nov 17 15:34:32 2006
@@ -0,0 +1,25 @@
+package ch.qos.logback.classic.spi;
+
+import junit.framework.TestCase;
+import ch.qos.logback.classic.LoggerContext;
+
+public class ContextListenerTest extends TestCase {
+  
+  LoggerContext context;
+  BasicContextListener listener;
+  
+  public void setUp() throws Exception {
+    context = new LoggerContext();
+    listener = new BasicContextListener();
+    context.addListener(listener);
+    super.setUp();
+  }
+  
+  public void testNotify() {
+    context.shutdownAndReset();
+    assertTrue(listener.updated);
+    assertEquals(EventType.CONTEXT_RESTART, listener.lastEvent.getType());
+    assertEquals(context, listener.lastEvent.getSource());
+  }
+
+}



More information about the logback-dev mailing list