[logback-dev] svn commit: r2055 - in logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran: . action

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Dec 4 19:20:11 CET 2008


Author: ceki
Date: Thu Dec  4 19:20:11 2008
New Revision: 2055

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/InsertFromJNDIAction.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/LoggerAction.java

Log:
Allow setting variables from JNDI

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java	Thu Dec  4 19:20:11 2008
@@ -14,6 +14,7 @@
 import ch.qos.logback.classic.joran.action.ConsolePluginAction;
 import ch.qos.logback.classic.joran.action.ContextNameAction;
 import ch.qos.logback.classic.joran.action.EvaluatorAction;
+import ch.qos.logback.classic.joran.action.InsertFromJNDIAction;
 import ch.qos.logback.classic.joran.action.JMXConfiguratorAction;
 import ch.qos.logback.classic.joran.action.LayoutAction;
 import ch.qos.logback.classic.joran.action.LevelAction;
@@ -45,6 +46,7 @@
     rs.addRule(new Pattern("configuration"), new ConfigurationAction());
 
     rs.addRule(new Pattern("configuration/contextName"), new ContextNameAction());
+    rs.addRule(new Pattern("configuration/insertFromJNDI"), new InsertFromJNDIAction());
     rs.addRule(new Pattern("*/evaluator"), new EvaluatorAction());
     rs.addRule(new Pattern("*/evaluator/matcher"),
         new MatcherAction());

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/InsertFromJNDIAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/InsertFromJNDIAction.java	Thu Dec  4 19:20:11 2008
@@ -0,0 +1,74 @@
+/**
+ * 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.classic.joran.action;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.classic.util.JNDIUtil;
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.InterpretationContext;
+import ch.qos.logback.core.util.OptionHelper;
+
+/**
+ * Insert an env-entry found in JNDI as a new context variable  
+
+ * @author Ceki Gulcu
+ *
+ */
+public class InsertFromJNDIAction extends Action {
+
+  public static String ENV_ENTRY_NAME_ATTR="env-entry-name";
+  public static String AS_ATTR="as";
+  
+  public void begin(InterpretationContext ec, String name, Attributes attributes) {
+
+    int errorCount = 0;
+    String envEntryName = attributes.getValue(ENV_ENTRY_NAME_ATTR);
+    String asName = attributes.getValue(AS_ATTR);
+    String envEntryValue;
+    
+    if(OptionHelper.isEmpty(envEntryName)) {
+      String lineColStr = getLineColStr(ec);
+      addError("["+ENV_ENTRY_NAME_ATTR+"] missing, around "+lineColStr);
+      errorCount++;
+    }
+    
+    if(OptionHelper.isEmpty(asName)) {
+      String lineColStr = getLineColStr(ec);
+      addError("["+AS_ATTR+"] missing, around "+lineColStr);
+      errorCount++;
+    }
+    
+    if(errorCount != 0) {
+      return;
+    }
+    
+    try {
+      Context ctx = JNDIUtil.getInitialContext();
+      envEntryValue = JNDIUtil.lookup(ctx, envEntryName);
+      if(OptionHelper.isEmpty(envEntryValue)) {
+        addError("["+envEntryName+"] has null or empty value");
+      } else {
+        context.putProperty(asName, envEntryValue);
+      }
+    } catch (NamingException e) {
+      addError("Failed to lookup JNDI env-entry ["+envEntryName+"]");
+    }
+    
+    
+  }
+
+  public void end(InterpretationContext ec, String name) {
+  }
+}

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/LoggerAction.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/LoggerAction.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/LoggerAction.java	Thu Dec  4 19:20:11 2008
@@ -41,9 +41,8 @@
 
     if (OptionHelper.isEmpty(loggerName)) {
       inError = true;
-      String line = ", around line " + getLineNumber(ec) + " column "
-          + getColumnNumber(ec);
-      String errorMsg = "No 'name' attribute in element " + name + line;
+      String aroundLine = getLineColStr(ec);
+      String errorMsg = "No 'name' attribute in element " + name + ", around " +aroundLine;
       addError(errorMsg);
       return;
     }


More information about the logback-dev mailing list