[logback-dev] svn commit: r2380 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/joran/action main/java/ch/qos/logback/core/joran/spi test/java/ch/qos/logback/core/joran/spi

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Jul 30 15:18:40 CEST 2009


Author: ceki
Date: Thu Jul 30 15:18:40 2009
New Revision: 2380

Added:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/NoAutoStart.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/NoAutoStartUtil.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/DoNotAutoStart.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/NoAutoStartUtilTest.java
Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComplexPropertyIA.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PackageTest.java

Log:
- Add support for the @NoAutoStart annotation. Classes marked with this annotation will not 
  be auto-started by Joran (via NestedComplexPropertyIA).

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComplexPropertyIA.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComplexPropertyIA.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComplexPropertyIA.java	Thu Jul 30 15:18:40 2009
@@ -15,6 +15,7 @@
 import org.xml.sax.Attributes;
 
 import ch.qos.logback.core.joran.spi.InterpretationContext;
+import ch.qos.logback.core.joran.spi.NoAutoStartUtil;
 import ch.qos.logback.core.joran.spi.Pattern;
 import ch.qos.logback.core.joran.spi.PropertySetter;
 import ch.qos.logback.core.spi.ContextAware;
@@ -110,10 +111,11 @@
         return;
       }
 
-      if(OptionHelper.isEmpty(className)) {
-        addInfo("Assuming default type ["+componentClass.getName()+"] for ["+localName+"] property");
+      if (OptionHelper.isEmpty(className)) {
+        addInfo("Assuming default type [" + componentClass.getName()
+            + "] for [" + localName + "] property");
       }
-      
+
       actionData.setNestedComplexProperty(componentClass.newInstance());
 
       // pass along the repository
@@ -154,9 +156,13 @@
     if (nestedBean.computeAggregationType("parent") == AggregationType.AS_COMPLEX_PROPERTY) {
       nestedBean.setComplexProperty("parent", actionData.parentBean.getObj());
     }
-    // start the nested complex attribute if it implements LifeCycle
-    if (actionData.getNestedComplexProperty() instanceof LifeCycle) {
-      ((LifeCycle) actionData.getNestedComplexProperty()).start();
+
+    // start the nested complex property if it implements LifeCycle and is not
+    // marked with a @NoAutoStart annotation
+    Object nestedComplexProperty = actionData.getNestedComplexProperty();
+    if (nestedComplexProperty instanceof LifeCycle
+        && NoAutoStartUtil.notMarkedWithNoAutoStart(nestedComplexProperty)) {
+      ((LifeCycle) nestedComplexProperty).start();
     }
 
     Object o = ec.peekObject();

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/NoAutoStart.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/NoAutoStart.java	Thu Jul 30 15:18:40 2009
@@ -0,0 +1,27 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library 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.
+ */
+package ch.qos.logback.core.joran.spi;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * If a type (a class) has this annotation, then it will not be automatically
+ * started by Joran at configuration time.
+ * 
+ * @author Ceki Gülcü
+ * 
+ */
+ at Retention(RetentionPolicy.RUNTIME)
+ at Target(ElementType.TYPE)
+public @interface NoAutoStart {
+}

Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/NoAutoStartUtil.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/NoAutoStartUtil.java	Thu Jul 30 15:18:40 2009
@@ -0,0 +1,21 @@
+package ch.qos.logback.core.joran.spi;
+
+public class NoAutoStartUtil {
+
+  /**
+   * Returns true if the class of the object 'o' passed as parameter is *not*
+   * marked with the NoAutoStart annotation. Return true otherwise.
+   * 
+   * @param o
+   * @return true for classes not marked with the NoAutoStart annotation
+   */
+  static public boolean notMarkedWithNoAutoStart(Object o) {
+    if (o == null) {
+      return false;
+    }
+    Class<?> clazz = o.getClass();
+    NoAutoStart a = clazz.getAnnotation(NoAutoStart.class);
+    return a == null;
+  }
+
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/DoNotAutoStart.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/DoNotAutoStart.java	Thu Jul 30 15:18:40 2009
@@ -0,0 +1,30 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2009, QOS.ch
+ * 
+ * This library 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.
+ */
+package ch.qos.logback.core.joran.spi;
+
+import ch.qos.logback.core.spi.LifeCycle;
+
+ at NoAutoStart
+public class DoNotAutoStart implements LifeCycle {
+
+  boolean started = false;
+  public boolean isStarted() {
+    return started;
+  }
+
+  public void start() {
+    started = true;
+  }
+
+  public void stop() {
+    started = false;
+  }
+
+}

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/NoAutoStartUtilTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/NoAutoStartUtilTest.java	Thu Jul 30 15:18:40 2009
@@ -0,0 +1,32 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2009, QOS.ch
+ * 
+ * This library 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.
+ */
+package ch.qos.logback.core.joran.spi;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+
+public class NoAutoStartUtilTest {
+
+  
+  @Test
+  public void commonObject() {
+    Object o = new Object();
+    assertTrue(NoAutoStartUtil.notMarkedWithNoAutoStart(o));
+  }
+  
+  @Test
+  public void markedWithNoAutoStart() {
+    DoNotAutoStart o = new DoNotAutoStart();
+    assertFalse(NoAutoStartUtil.notMarkedWithNoAutoStart(o));
+  }
+}

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PackageTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PackageTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/joran/spi/PackageTest.java	Thu Jul 30 15:18:40 2009
@@ -15,6 +15,7 @@
 
 @RunWith(Suite.class)
 @SuiteClasses( { PatternTest.class, SimpleStoreTest.class,
-    PropertySetterTest.class, DefaultNestedComponentRegistryTest.class })
+    PropertySetterTest.class, NoAutoStartUtilTest.class,
+    DefaultNestedComponentRegistryTest.class })
 public class PackageTest {
 }


More information about the logback-dev mailing list