[logback-dev] [GIT] Logback: the generic, reliable, fast and flexible logging framework. branch, master, updated. release_0.9.19-6-g2391b73

added by portage for gitosis-gentoo git-noreply at pixie.qos.ch
Mon Mar 29 20:07:43 CEST 2010


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "Logback: the generic, reliable, fast and flexible logging framework.".

The branch, master has been updated
       via  2391b73837b528d78505a0b3a339b533b791d283 (commit)
      from  1a86d32aba6e17f21c242999dad27916e051ee3d (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.qos.ch/gitweb/?p=logback.git;a=commit;h=2391b73837b528d78505a0b3a339b533b791d283
http://github.com/ceki/logback/commit/2391b73837b528d78505a0b3a339b533b791d283

commit 2391b73837b528d78505a0b3a339b533b791d283
Author: Ceki Gulcu <ceki at qos.ch>
Date:   Mon Mar 29 20:06:23 2010 +0200

    - added missing files for if-then-else support in Joran (this is still ongoing work).

diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/Condition.java b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/Condition.java
new file mode 100644
index 0000000..344ffcf
--- /dev/null
+++ b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/Condition.java
@@ -0,0 +1,18 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2010, QOS.ch. All rights reserved.
+ * 
+ * This program and the accompanying materials are dual-licensed under either
+ * the terms of the Eclipse Public License v1.0 as published by the Eclipse
+ * Foundation
+ * 
+ * or (per the licensee's choosing)
+ * 
+ * under the terms of the GNU Lesser General Public License version 2.1 as
+ * published by the Free Software Foundation.
+ */
+package ch.qos.logback.core.joran.conditional;
+
+public interface Condition {
+  public boolean evaluate();
+}
diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ElseAction.java b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ElseAction.java
new file mode 100644
index 0000000..63ede3f
--- /dev/null
+++ b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ElseAction.java
@@ -0,0 +1,44 @@
+package ch.qos.logback.core.joran.conditional;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.event.InPlayListener;
+import ch.qos.logback.core.joran.event.SaxEvent;
+import ch.qos.logback.core.joran.spi.ActionException;
+import ch.qos.logback.core.joran.spi.InterpretationContext;
+
+public class ElseAction  extends Action implements InPlayListener {
+
+  List<SaxEvent> saxEventList;
+  
+  @Override
+  public void begin(InterpretationContext ic, String name, Attributes attributes)
+      throws ActionException {
+    saxEventList = new ArrayList<SaxEvent>();
+    ic.addInPlayListener(this);
+  }
+
+  @Override
+  public void end(InterpretationContext ic, String name) throws ActionException {
+    ic.removeInPlayListener(this);
+    Object o = ic.peekObject();
+    if (o instanceof IfAction) {
+      IfAction ifAction = (IfAction) o;
+      removeFirstLastFromList();
+      ifAction.setElseSaxEventList(saxEventList);
+    }
+  }
+
+  public void inPlay(SaxEvent event) {
+    saxEventList.add(event);
+  }
+
+  void removeFirstLastFromList() {
+    saxEventList.remove(0);
+    saxEventList.remove(saxEventList.size() - 1);
+  }
+}
diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/IfAction.java b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/IfAction.java
new file mode 100644
index 0000000..23eaf7c
--- /dev/null
+++ b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/IfAction.java
@@ -0,0 +1,90 @@
+package ch.qos.logback.core.joran.conditional;
+
+import java.util.List;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.event.SaxEvent;
+import ch.qos.logback.core.joran.spi.ActionException;
+import ch.qos.logback.core.joran.spi.InterpretationContext;
+import ch.qos.logback.core.joran.spi.Interpreter;
+import ch.qos.logback.core.util.OptionHelper;
+
+public class IfAction extends Action {
+  private static final String CONDITION_ATTR = "condition";
+  
+  Boolean boolResult;
+  List<SaxEvent> thenSaxEventList;
+  List<SaxEvent> elseSaxEventList;
+
+  @Override
+  public void begin(InterpretationContext ic, String name, Attributes attributes)
+      throws ActionException {
+
+    ic.pushObject(this);
+    Condition condition = null;
+    String conditionAttribute = attributes.getValue(CONDITION_ATTR);
+    if (!OptionHelper.isEmpty(conditionAttribute)) {
+      PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder();
+      pesb.setContext(context);
+      try {
+        condition = pesb.build(conditionAttribute);
+      } catch (Exception e) {
+        addError("Faield to parse condition ["+conditionAttribute+"]", e);
+      }
+     
+      if(condition!=null) {
+        boolResult = condition.evaluate();
+      }
+      
+    }
+  }
+
+
+  @Override
+  public void end(InterpretationContext ic, String name) throws ActionException {
+
+    Object o = ic.peekObject();
+    if (o == null) {
+      throw new IllegalStateException("Unexpected null object on stack");
+    }
+    if (!(o instanceof IfAction)) {
+      throw new IllegalStateException("Unexpected object of type ["
+          + o.getClass() + "] on stack");
+    }
+
+    if (o != this) {
+      throw new IllegalStateException(
+          "IfAction different then current one on stack");
+    }
+    ic.popObject();
+
+    if (boolResult == null) {
+      addError("Failed to determine \"if then else\" result");
+      return;
+    }
+
+    Interpreter interpreter = ic.getJoranInterpreter();
+    List<SaxEvent> listToPlay = thenSaxEventList;
+    if (!boolResult) {
+      listToPlay = elseSaxEventList;
+    }
+    
+    if (interpreter.pattern.peekLast().equals("if")) {
+      interpreter.pattern.pop();
+      interpreter.play(listToPlay);
+      interpreter.pattern.push("if");
+    }
+
+  }
+
+  public void setThenSaxEventList(List<SaxEvent> thenSaxEventList) {
+    this.thenSaxEventList = thenSaxEventList;
+  }
+
+  public void setElseSaxEventList(List<SaxEvent> elseSaxEventList) {
+    this.elseSaxEventList = elseSaxEventList;
+  }
+
+}
diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/MapWrapperForScripts.java b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/MapWrapperForScripts.java
new file mode 100644
index 0000000..e20a9ec
--- /dev/null
+++ b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/MapWrapperForScripts.java
@@ -0,0 +1,57 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2010, QOS.ch. All rights reserved.
+ * 
+ * This program and the accompanying materials are dual-licensed under either
+ * the terms of the Eclipse Public License v1.0 as published by the Eclipse
+ * Foundation
+ * 
+ * or (per the licensee's choosing)
+ * 
+ * under the terms of the GNU Lesser General Public License version 2.1 as
+ * published by the Free Software Foundation.
+ */
+package ch.qos.logback.core.joran.conditional;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class MapWrapperForScripts {
+  public Map map = new HashMap<String, String>();
+  public String name;
+  
+  
+  public void setMap(Map map) {
+    this.map = map;
+  }
+  public void setName(String name) {
+    this.name = name;
+  }
+  
+  public boolean isNull(String k) {
+    Object o = map.get(k);
+    if (o instanceof String) {
+      return false;
+    }
+    return (System.getProperty(k) == null);
+  }
+  
+  public String p(String k) {
+    return property(k);
+  }
+  
+  public String property(String k) {
+    Object o = map.get(k);
+    if (o instanceof String) {
+      return (String) o;
+    }
+    String v = System.getProperty(k);
+    if(v != null) {
+      return v;
+    }
+    System.getProperties();
+    return "";
+  }
+   
+
+}
diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilder.java b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilder.java
new file mode 100644
index 0000000..e29c414
--- /dev/null
+++ b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilder.java
@@ -0,0 +1,49 @@
+package ch.qos.logback.core.joran.conditional;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.codehaus.janino.ClassBodyEvaluator;
+import org.codehaus.janino.CompileException;
+import org.codehaus.janino.Parser.ParseException;
+import org.codehaus.janino.Scanner.ScanException;
+import org.junit.Before;
+
+import ch.qos.logback.core.spi.ContextAwareBase;
+
+public class PropertyEvalScriptBuilder extends ContextAwareBase {
+
+  private static String SCRIPT_PREFIX = ""
+      + "public boolean evaluate() { return ";
+  private static String SCRIPT_SUFFIX = "" + "; }";
+
+  static String SCRIPT = ""
+      + "public boolean eval() { return p(\"Ka\").equals(\"Va\"); }";
+
+  Map<String, String> map = new HashMap<String, String>();
+
+  @Before
+  public Condition build(String script) throws IllegalAccessException,
+      CompileException, ParseException, ScanException, InstantiationException,
+      SecurityException, NoSuchMethodException, IllegalArgumentException,
+      InvocationTargetException {
+
+    ClassBodyEvaluator cbe = new ClassBodyEvaluator();
+    cbe.setImplementedTypes(new Class[] { Condition.class });
+    cbe.setExtendedType(MapWrapperForScripts.class);
+    cbe.cook(SCRIPT_PREFIX + script + SCRIPT_SUFFIX);
+
+    Class<?> clazz = cbe.getClazz();
+    Condition instance = (Condition) clazz.newInstance();
+    Method setMapMethod = clazz.getMethod("setMap", Map.class);
+    setMapMethod.invoke(instance, context.getCopyOfPropertyMap());
+
+    Method setNameMethod = clazz.getMethod("setName", String.class);
+    setNameMethod.invoke(instance, context.getName());
+
+    return instance;
+  }
+
+}
diff --git a/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ThenAction.java b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ThenAction.java
new file mode 100644
index 0000000..5e5f99b
--- /dev/null
+++ b/logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ThenAction.java
@@ -0,0 +1,46 @@
+package ch.qos.logback.core.joran.conditional;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.event.InPlayListener;
+import ch.qos.logback.core.joran.event.SaxEvent;
+import ch.qos.logback.core.joran.spi.ActionException;
+import ch.qos.logback.core.joran.spi.InterpretationContext;
+
+public class ThenAction  extends Action implements InPlayListener {
+
+  List<SaxEvent> eventList;
+  
+  @Override
+  public void begin(InterpretationContext ic, String name, Attributes attributes)
+      throws ActionException {
+    eventList = new ArrayList<SaxEvent>();
+    ic.addInPlayListener(this);
+  }
+
+  @Override
+  public void end(InterpretationContext ic, String name) throws ActionException {
+    ic.removeInPlayListener(this);
+    Object o = ic.peekObject();
+    if (o instanceof IfAction) {
+      IfAction ifAction = (IfAction) o;
+      removeFirstLastFromList();
+      ifAction.setThenSaxEventList(eventList);
+    } else {
+      throw new IllegalStateException("Missing IfAction on top of stack");
+    }
+  }
+
+  public void inPlay(SaxEvent event) {
+    eventList.add(event);
+  }
+
+  void removeFirstLastFromList() {
+    eventList.remove(0);
+    eventList.remove(eventList.size() - 1);
+  }
+}
diff --git a/logback-core/src/test/input/joran/conditional/if0.xml b/logback-core/src/test/input/joran/conditional/if0.xml
new file mode 100644
index 0000000..0273e59
--- /dev/null
+++ b/logback-core/src/test/input/joran/conditional/if0.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<x>
+  <if condition='p("Ki1").equals("Val1")'>
+    <then>
+      <inc increment="1"/>
+    </then>
+    <else>
+      <inc increment="1"/>
+      <inc increment="1"/>
+    </else>
+  </if>
+</x>
diff --git a/logback-core/src/test/input/joran/conditional/if2.xml b/logback-core/src/test/input/joran/conditional/if2.xml
new file mode 100644
index 0000000..bc70e1f
--- /dev/null
+++ b/logback-core/src/test/input/joran/conditional/if2.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<x>
+  <if class="">
+    <a>asd</a>
+    <then>
+      <inc increment="1"/>
+    </then>
+    <else>
+      <inc increment="1"/>
+      <inc increment="1"/>
+    </else>
+  </if>
+</x>
diff --git a/logback-core/src/test/java/ch/qos/logback/core/joran/conditional/IfThenElseTest.java b/logback-core/src/test/java/ch/qos/logback/core/joran/conditional/IfThenElseTest.java
new file mode 100644
index 0000000..bf9aa89
--- /dev/null
+++ b/logback-core/src/test/java/ch/qos/logback/core/joran/conditional/IfThenElseTest.java
@@ -0,0 +1,88 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2010, QOS.ch. All rights reserved.
+ * 
+ * This program and the accompanying materials are dual-licensed under either
+ * the terms of the Eclipse Public License v1.0 as published by the Eclipse
+ * Foundation
+ * 
+ * or (per the licensee's choosing)
+ * 
+ * under the terms of the GNU Lesser General Public License version 2.1 as
+ * published by the Free Software Foundation.
+ */
+package ch.qos.logback.core.joran.conditional;
+
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.joran.TrivialConfigurator;
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.action.NOPAction;
+import ch.qos.logback.core.joran.action.ext.IncAction;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.joran.spi.Pattern;
+import ch.qos.logback.core.testUtil.RandomUtil;
+import ch.qos.logback.core.util.CoreTestConstants;
+import ch.qos.logback.core.util.StatusPrinter;
+
+public class IfThenElseTest {
+
+  Context context = new ContextBase();
+  TrivialConfigurator tc;
+  int diff = RandomUtil.getPositiveInt();
+  static final String CONDITIONAL_DIR_PREFIX = CoreTestConstants.JORAN_INPUT_PREFIX
+  + "conditional/";
+
+  
+  @Before
+  public void setUp() throws Exception {
+    HashMap<Pattern, Action> rulesMap = new HashMap<Pattern, Action>();
+    rulesMap.put(new Pattern("x"), new NOPAction());
+    rulesMap.put(new Pattern("x/inc"), new IncAction());
+    rulesMap.put(new Pattern("*/if"), new IfAction());
+    rulesMap.put(new Pattern("*/if/then"), new ThenAction());
+    rulesMap.put(new Pattern("*/if/then/*"), new NOPAction());
+    rulesMap.put(new Pattern("*/if/else"), new ElseAction());
+    rulesMap.put(new Pattern("*/if/else/*"), new NOPAction());    
+    
+    tc = new TrivialConfigurator(rulesMap);
+    tc.setContext(context);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    IncAction.reset();
+  }
+
+  @Test
+  public void if0_Then() throws JoranException {
+    context.putProperty("Ki1", "Val1");
+    tc.doConfigure(CONDITIONAL_DIR_PREFIX+"if0.xml");
+    verifyConfig(1);
+  }
+
+  @Test
+  public void if0_Else() throws JoranException {
+    tc.doConfigure(CONDITIONAL_DIR_PREFIX+"if0.xml");
+    StatusPrinter.print(context);
+    verifyConfig(2);
+  }
+
+  
+  void verifyConfig(int expected) {
+    assertEquals(expected, IncAction.beginCount);
+    assertEquals(expected, IncAction.endCount);
+  }
+  
+  
+  
+}
diff --git a/logback-core/src/test/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilderTest.java b/logback-core/src/test/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilderTest.java
new file mode 100644
index 0000000..aab2646
--- /dev/null
+++ b/logback-core/src/test/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilderTest.java
@@ -0,0 +1,74 @@
+package ch.qos.logback.core.joran.conditional;
+
+import static org.junit.Assert.*;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.testUtil.RandomUtil;
+
+public class PropertyEvalScriptBuilderTest {
+
+  
+  Context context = new ContextBase();
+  PropertyEvalScriptBuilder pesb = new PropertyEvalScriptBuilder();
+  int diff = RandomUtil.getPositiveInt();
+  
+  @Before
+  public void setUp() {
+    context.setName("c"+diff);
+    pesb.setContext(context);
+  }
+  
+  @Test
+  public void existing() throws Exception {
+    String k = "ka"+diff;
+    context.putProperty("ka"+diff, "va");
+    Condition condition = pesb.build("p(\""+k+"\").contains(\"va\")");
+    assertNotNull(condition);
+    assertTrue(condition.evaluate());
+  }
+
+  @Test
+  public void isNullForExisting() throws Exception {
+    String k = "ka"+diff;
+    context.putProperty("ka"+diff, "va");
+    Condition condition = pesb.build("isNull(\""+k+"\")");
+    assertNotNull(condition);
+    assertFalse(condition.evaluate());
+  }
+
+  
+  @Test
+  public void inexistentProperty() throws Exception {
+    String k = "ka"+diff;
+    Condition condition = pesb.build("p(\""+k+"\").contains(\"va\")");
+    assertNotNull(condition);
+    assertFalse(condition.evaluate());
+  }
+
+  @Test
+  public void isNullForInexistent() throws Exception {
+    String k = "ka"+diff;
+    Condition condition = pesb.build("isNull(\""+k+"\")");
+    assertNotNull(condition);
+    assertTrue(condition.evaluate());
+  }
+
+  @Test
+  public void nameOK() throws Exception {
+    Condition condition = pesb.build("name.contains(\""+context.getName()+"\")");
+    assertNotNull(condition);
+    assertTrue(condition.evaluate());
+  }
+
+  @Test
+  public void wrongName() throws Exception {
+    Condition condition = pesb.build("name.contains(\"x\")");
+    assertNotNull(condition);
+    assertFalse(condition.evaluate());
+  }
+  
+}

-----------------------------------------------------------------------

Summary of changes:
 .../logback/core/joran/conditional/Condition.java  |   10 +--
 .../logback/core/joran/conditional/ElseAction.java |   44 ++++++++++
 .../logback/core/joran/conditional/IfAction.java   |   90 ++++++++++++++++++++
 .../joran/conditional/MapWrapperForScripts.java    |   57 ++++++++++++
 .../conditional/PropertyEvalScriptBuilder.java     |   49 +++++++++++
 .../logback/core/joran/conditional/ThenAction.java |   46 ++++++++++
 .../src/test/input/joran/conditional/if0.xml       |   13 +++
 .../src/test/input/joran/conditional/if2.xml       |   14 +++
 .../core/joran/conditional/IfThenElseTest.java     |   88 +++++++++++++++++++
 .../conditional/PropertyEvalScriptBuilderTest.java |   74 ++++++++++++++++
 10 files changed, 479 insertions(+), 6 deletions(-)
 copy logback-classic/src/main/java/ch/qos/logback/classic/db/names/DBNameResolver.java => logback-core/src/main/java/ch/qos/logback/core/joran/conditional/Condition.java (71%)
 create mode 100644 logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ElseAction.java
 create mode 100644 logback-core/src/main/java/ch/qos/logback/core/joran/conditional/IfAction.java
 create mode 100644 logback-core/src/main/java/ch/qos/logback/core/joran/conditional/MapWrapperForScripts.java
 create mode 100644 logback-core/src/main/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilder.java
 create mode 100644 logback-core/src/main/java/ch/qos/logback/core/joran/conditional/ThenAction.java
 create mode 100644 logback-core/src/test/input/joran/conditional/if0.xml
 create mode 100644 logback-core/src/test/input/joran/conditional/if2.xml
 create mode 100644 logback-core/src/test/java/ch/qos/logback/core/joran/conditional/IfThenElseTest.java
 create mode 100644 logback-core/src/test/java/ch/qos/logback/core/joran/conditional/PropertyEvalScriptBuilderTest.java


hooks/post-receive
-- 
Logback: the generic, reliable, fast and flexible logging framework.


More information about the logback-dev mailing list