[logback-dev] svn commit: r1134 - in logback/trunk: logback-classic/src/main/java/ch/qos/logback/classic logback-classic/src/main/java/ch/qos/logback/classic/jmx logback-classic/src/main/java/ch/qos/logback/classic/joran logback-classic/src/main/java/ch/qos/logback/classic/joran/action logback-classic/src/test/java/ch/qos/logback/classic logback-core/src/main/java/ch/qos/logback/core/boolex logback-core/src/main/java/ch/qos/logback/core/db logback-core/src/main/java/ch/qos/logback/core/html logback-core/src/main/java/ch/qos/logback/core/joran/action logback-core/src/main/java/ch/qos/logback/core/joran/spi logback-core/src/main/java/ch/qos/logback/core/pattern logback-core/src/main/java/ch/qos/logback/core/pattern/parser logback-core/src/main/java/ch/qos/logback/core/rolling/helper logback-core/src/main/java/ch/qos/logback/core/util logback-core/src/test/java/ch/qos/logback/core/pattern/parser

noreply.seb at qos.ch noreply.seb at qos.ch
Wed Dec 20 10:34:42 CET 2006


Author: seb
Date: Wed Dec 20 10:34:41 2006
New Revision: 1134

Added:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/ConfiguratorMBean.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/JMXConfiguratorAction.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/JoranConfigurator.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/PatternLayoutTest.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/db/BindDataSourceToJNDIAction.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/html/HTMLLayoutBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractEventEvaluatorAction.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractLayoutAction.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AppenderAction.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComponentIA.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Compiler.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Parser.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/OptionHelper.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/CompilerTest.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/SamplePatternLayoutTest.java

Log:
Added a JMCConfigurator that allows reload and level setting (->Work in progress)
Refactored the way that we load classes: we now use the context's classloader.


Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java	Wed Dec 20 10:34:41 2006
@@ -13,10 +13,21 @@
 import org.slf4j.LoggerFactory;
 
 import ch.qos.logback.core.ConsoleAppender;
+import ch.qos.logback.core.status.InfoStatus;
+import ch.qos.logback.core.status.StatusManager;
 
 public class BasicConfigurator {
 
+  final static BasicConfigurator hiddenSingleton = new BasicConfigurator();
+    
+  private BasicConfigurator() {
+  }
+  
   public static void configure(LoggerContext lc) {
+    StatusManager sm = lc.getStatusManager();
+    if(sm != null)  {
+     sm.add(new InfoStatus("Setting up default configuration.", hiddenSingleton));
+    }
     ConsoleAppender ca = new ConsoleAppender();
     ca.setContext(lc);
     ca.setName("console");

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java	Wed Dec 20 10:34:41 2006
@@ -0,0 +1,56 @@
+package ch.qos.logback.classic.jmx;
+
+import java.net.URL;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.classic.util.ContextInitializer;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.spi.ContextAwareBase;
+
+public class Configurator extends ContextAwareBase implements
+    ConfiguratorMBean {
+
+  public Configurator(LoggerContext loggerContext) {
+    this.context = loggerContext;
+  }
+
+  public void reload() {
+    LoggerContext lc = (LoggerContext) context;
+    addInfo("Shutting down context: " + lc.getName());
+    lc.shutdownAndReset();
+    ContextInitializer.autoConfig(lc, lc.getClass().getClassLoader());
+    addInfo("Context: " + lc.getName() + " reloaded.");
+  }
+
+  public void reload(String fileName) throws JoranException {
+    LoggerContext lc = (LoggerContext) context;
+    JoranConfigurator configurator = new JoranConfigurator();
+    configurator.setContext(lc);
+    configurator.doConfigure(fileName);
+  }
+
+  public void reload(URL url) throws JoranException {
+    LoggerContext lc = (LoggerContext) context;
+    addInfo("Shutting down context: " + lc.getName());
+    lc.shutdownAndReset();
+    ContextInitializer.configureByResource(lc, url);
+    addInfo("Context: " + lc.getName() + " reloaded.");
+  }
+
+  public void setLoggerLevel(String loggerName, String levelStr) {
+    LoggerContext lc = (LoggerContext) context;
+    Logger logger = lc.getLogger(loggerName);
+    if ("null".equalsIgnoreCase(levelStr)) {
+      logger.setLevel(null);
+    } else {
+      Level level = Level.toLevel(levelStr, null);
+      if (level != null) {
+        logger.setLevel(level);
+      }
+    }
+  }
+
+}

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/ConfiguratorMBean.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/ConfiguratorMBean.java	Wed Dec 20 10:34:41 2006
@@ -0,0 +1,17 @@
+package ch.qos.logback.classic.jmx;
+
+import java.net.URL;
+
+import ch.qos.logback.core.joran.spi.JoranException;
+
+public interface ConfiguratorMBean {
+  
+  public void reload();
+  
+  public void reload(String fileName) throws JoranException;
+  
+  public void reload(URL url) throws JoranException;
+  
+  public void setLoggerLevel(String loggerName, String levelStr);
+
+}

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	Wed Dec 20 10:34:41 2006
@@ -12,6 +12,7 @@
 
 import ch.qos.logback.classic.joran.action.ConfigurationAction;
 import ch.qos.logback.classic.joran.action.EvaluatorAction;
+import ch.qos.logback.classic.joran.action.JMXConfiguratorAction;
 import ch.qos.logback.classic.joran.action.LayoutAction;
 import ch.qos.logback.classic.joran.action.LevelAction;
 import ch.qos.logback.classic.joran.action.LoggerAction;
@@ -55,6 +56,8 @@
     rs
         .addRule(new Pattern("configuration/appender/layout"),
             new LayoutAction());
+    
+    rs.addRule(new Pattern("configuration/jmxConfigurator"), new JMXConfiguratorAction());
   }
 
 }

Added: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/JMXConfiguratorAction.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/JMXConfiguratorAction.java	Wed Dec 20 10:34:41 2006
@@ -0,0 +1,51 @@
+package ch.qos.logback.classic.joran.action;
+
+import java.lang.management.ManagementFactory;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.xml.sax.Attributes;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.jmx.Configurator;
+import ch.qos.logback.classic.jmx.ConfiguratorMBean;
+import ch.qos.logback.core.joran.action.Action;
+import ch.qos.logback.core.joran.spi.ActionException;
+import ch.qos.logback.core.joran.spi.InterpretationContext;
+
+public class JMXConfiguratorAction extends Action {
+
+  static final String DOMAIN = "ch.qos.logback.classic";
+ 
+  @Override
+  public void begin(InterpretationContext ec, String name, Attributes attributes)
+      throws ActionException {
+    register();
+  }
+
+  @Override
+  public void end(InterpretationContext ec, String name) throws ActionException {
+
+  }
+
+  public void register() {
+    ConfiguratorMBean configuratorMBean = new Configurator((LoggerContext) context);
+
+    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+    try {
+      ObjectName on = new ObjectName(DOMAIN + ":Name=" + context.getName()
+          + ",Type=" + configuratorMBean.getClass().getName());
+
+      // StandardMBean mbean = new StandardMBean(configuratorMBean,
+      // ConfiguratorMBean.class);
+      if (mbs.isRegistered(on)) {
+        mbs.unregisterMBean(on);
+      }
+      mbs.registerMBean(configuratorMBean, on);
+    } catch (Exception e) {
+      addError("Failed to create mbean", e);
+    }
+  }
+
+}

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/PatternLayoutTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/PatternLayoutTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/PatternLayoutTest.java	Wed Dec 20 10:34:41 2006
@@ -28,7 +28,7 @@
 
 public class PatternLayoutTest extends AbstractPatternLayoutBaseTest {
 
-	LoggerContext lc = new LoggerContext();
+  LoggerContext lc = new LoggerContext();
   Logger logger = lc.getLogger(ConverterTest.class);
   LoggingEvent le;
   List optionList = new ArrayList();
@@ -118,4 +118,9 @@
     suite.addTestSuite(PatternLayoutTest.class);
     return suite;
   }
+
+  @Override
+  public Context getContext() {
+    return lc;
+  }
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java	Wed Dec 20 10:34:41 2006
@@ -48,8 +48,10 @@
 
   public void start() {
     try {
+      assert context != null;
+      ClassLoader cl = context.getClass().getClassLoader();
       ee = new ExpressionEvaluator(getDecoratedExpression(), EXPRESSION_TYPE,
-          getParameterNames(), getParameterTypes(), THROWN_EXCEPTIONS, null);
+          getParameterNames(), getParameterTypes(), THROWN_EXCEPTIONS, cl);
       start = true;
     } catch (Exception e) {
       addError(

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/db/BindDataSourceToJNDIAction.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/db/BindDataSourceToJNDIAction.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/db/BindDataSourceToJNDIAction.java	Wed Dec 20 10:34:41 2006
@@ -55,7 +55,7 @@
 
     try {
       DataSource ds =
-        (DataSource) OptionHelper.instantiateByClassName(dsClassName, DataSource.class);
+        (DataSource) OptionHelper.instantiateByClassName(dsClassName, DataSource.class, context);
 
       PropertySetter setter = new PropertySetter(ds);
 

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/html/HTMLLayoutBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/html/HTMLLayoutBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/html/HTMLLayoutBase.java	Wed Dec 20 10:34:41 2006
@@ -71,9 +71,7 @@
     
     try {
       Parser p = new Parser(pattern);
-      if (getContext() != null) {
-        p.setStatusManager(getContext().getStatusManager());
-      }
+      p.setContext(getContext());
       Node t = p.parse();
       this.head = p.compile(t, getDefaultConverterMap());
       DynamicConverter.startConverters(this.head);

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractEventEvaluatorAction.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractEventEvaluatorAction.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractEventEvaluatorAction.java	Wed Dec 20 10:34:41 2006
@@ -59,7 +59,7 @@
     try {
       evaluator = (EventEvaluator)
         OptionHelper.instantiateByClassName(
-          className, ch.qos.logback.core.boolex.EventEvaluator.class);
+          className, ch.qos.logback.core.boolex.EventEvaluator.class, context);
       
       if(isOfCorrectType(evaluator)) {
         evaluator.setContext(this.context);

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractLayoutAction.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractLayoutAction.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AbstractLayoutAction.java	Wed Dec 20 10:34:41 2006
@@ -38,7 +38,7 @@
     try {
       layout = (Layout)
         OptionHelper.instantiateByClassName(
-          className, ch.qos.logback.core.Layout.class);
+          className, ch.qos.logback.core.Layout.class, context);
       
       if(isOfCorrectType(layout)) {
         layout.setContext(this.context);

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AppenderAction.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AppenderAction.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/AppenderAction.java	Wed Dec 20 10:34:41 2006
@@ -43,7 +43,7 @@
       addInfo("About to instantiate appender of type ["+className+"]");
 
       appender = (Appender) OptionHelper.instantiateByClassName(
-          className, ch.qos.logback.core.Appender.class);
+          className, ch.qos.logback.core.Appender.class, context);
 
       appender.setContext(context);
 

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComponentIA.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComponentIA.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/action/NestedComponentIA.java	Wed Dec 20 10:34:41 2006
@@ -103,7 +103,8 @@
       // className);
 
       // FIXME: Loading classes should be governed by config file rules.
-      actionData.nestedComponent = Loader.loadClass(className).newInstance();
+      
+      actionData.nestedComponent = Loader.loadClass(className, context).newInstance();
 
       // pass along the repository
       if (actionData.nestedComponent instanceof ContextAware) {

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/joran/spi/SimpleRuleStore.java	Wed Dec 20 10:34:41 2006
@@ -57,7 +57,7 @@
     
     try {
       action = (Action) OptionHelper.instantiateByClassName(
-        actionClassName, Action.class);
+        actionClassName, Action.class, context);
     } catch(Exception e) {
       addError("Could not instantiate class ["+actionClassName+"]", e);
     }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/PatternLayoutBase.java	Wed Dec 20 10:34:41 2006
@@ -72,7 +72,7 @@
     try {
       Parser p = new Parser(pattern);
       if (getContext() != null) {
-        p.setStatusManager(getContext().getStatusManager());
+        p.setContext(getContext());
       }
       Node t = p.parse();
       this.head = p.compile(t, getEffectiveConverterMap());

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Compiler.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Compiler.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Compiler.java	Wed Dec 20 10:34:41 2006
@@ -15,19 +15,17 @@
 import ch.qos.logback.core.pattern.Converter;
 import ch.qos.logback.core.pattern.DynamicConverter;
 import ch.qos.logback.core.pattern.LiteralConverter;
+import ch.qos.logback.core.spi.ContextAwareBase;
 import ch.qos.logback.core.status.ErrorStatus;
-import ch.qos.logback.core.status.Status;
-import ch.qos.logback.core.status.StatusManager;
 import ch.qos.logback.core.util.OptionHelper;
 
 
-class Compiler {
+class Compiler extends ContextAwareBase {
 
   Converter head;
   Converter tail;
   final Node top;
   final Map converterMap;
-  StatusManager statusManager;
   
   Compiler(final Node top, final Map converterMap) {
     this.top = top;
@@ -45,8 +43,9 @@
         CompositeNode cn = (CompositeNode) n;
         CompositeConverter compositeConverter = new CompositeConverter();
         compositeConverter.setFormattingInfo(cn.getFormatInfo());
-        Converter childConverter = new Compiler(cn.getChildNode(), converterMap)
-            .compile();
+        Compiler childCompiler = new Compiler(cn.getChildNode(), converterMap);
+        childCompiler.setContext(context);
+        Converter childConverter = childCompiler.compile();
         compositeConverter.setChildConverter(childConverter);
         addToList(compositeConverter);
         break;
@@ -94,7 +93,7 @@
     if (converterClassStr != null) {
       try {
         return (DynamicConverter) OptionHelper.instantiateByClassName(
-            converterClassStr, DynamicConverter.class);
+            converterClassStr, DynamicConverter.class, context);
       } catch (Exception e) {
         return null;
       }
@@ -103,13 +102,13 @@
     }
   }
 
-  public void setStatusManager(StatusManager statusManager) {
-    this.statusManager = statusManager;
-  }
-  
-  void addStatus(Status status) {
-    if(statusManager != null) {
-      statusManager.add(status);
-    }
-  }
+//  public void setStatusManager(StatusManager statusManager) {
+//    this.statusManager = statusManager;
+//  }
+//  
+//  void addStatus(Status status) {
+//    if(statusManager != null) {
+//      statusManager.add(status);
+//    }
+//  }
 }
\ No newline at end of file

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Parser.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Parser.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/pattern/parser/Parser.java	Wed Dec 20 10:34:41 2006
@@ -5,12 +5,11 @@
 
 import ch.qos.logback.core.pattern.Converter;
 import ch.qos.logback.core.pattern.FormatInfo;
-import ch.qos.logback.core.status.StatusManager;
+import ch.qos.logback.core.spi.ContextAwareBase;
 
 
-public class Parser {
+public class Parser extends ContextAwareBase {
 
-  StatusManager statusManager;
   final List tokenList;
   int pointer = 0;
   
@@ -42,7 +41,8 @@
    */
   public Converter compile(final Node top, Map converterMap) {
     Compiler compiler = new Compiler(top, converterMap);
-    compiler.setStatusManager(statusManager);
+    compiler.setContext(context);
+    //compiler.setStatusManager(statusManager);
     return compiler.compile();
   }
 
@@ -185,7 +185,7 @@
     }
   }
 
-  public void setStatusManager(StatusManager statusManager) {
-    this.statusManager = statusManager;
-  }
+//  public void setStatusManager(StatusManager statusManager) {
+//    this.statusManager = statusManager;
+//  }
 }
\ No newline at end of file

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java	Wed Dec 20 10:34:41 2006
@@ -53,7 +53,7 @@
   void parse() {
     try {
       Parser p = new Parser(pattern);
-      p.setStatusManager(getStatusManager());
+      p.setContext(context);
       Node t = p.parse();
       this.headTokenConverter = p.compile(t, CONVERTER_MAP);
 

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java	Wed Dec 20 10:34:41 2006
@@ -11,6 +11,8 @@
 
 import java.net.URL;
 
+import ch.qos.logback.core.Context;
+
 /**
  * Load resources (or images) from various sources.
  * 
@@ -31,6 +33,7 @@
     }
   }
 
+  
   /**
    * This method will search for <code>resource</code> in different places.
    * The search order is as follows:
@@ -74,6 +77,10 @@
     return Thread.currentThread().getContextClassLoader();
   }
 
+  public static Class loadClass(String clazz, Context context) throws ClassNotFoundException {
+    ClassLoader cl = context.getClass().getClassLoader();
+    return cl.loadClass(clazz);
+  }
   /**
    * If running under JDK 1.2 load the specified class using the
    * <code>Thread</code> <code>contextClassLoader</code> if that fails try

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/OptionHelper.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/OptionHelper.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/OptionHelper.java	Wed Dec 20 10:34:41 2006
@@ -11,6 +11,7 @@
 
 import java.util.Map;
 
+import ch.qos.logback.core.Context;
 import ch.qos.logback.core.CoreGlobal;
 
 /**
@@ -18,8 +19,15 @@
  */
 public class OptionHelper {
 
+  public static Object instantiateByClassName(String className,
+      Class superClass, Context context) throws IncompatibleClassException, DynamicClassLoadingException {
+    ClassLoader classLoader = context.getClass().getClassLoader();
+    return instantiateByClassName(className, superClass, classLoader);
+  }
+  
   @SuppressWarnings("unchecked")
-  public static Object instantiateByClassName(String className, Class superClass)
+  public static Object instantiateByClassName(String className,
+      Class superClass, ClassLoader classLoader)
       throws IncompatibleClassException, DynamicClassLoadingException {
 
     if (className == null) {
@@ -27,15 +35,8 @@
     }
 
     try {
-      // FIXME This is temporary (really!).
       Class classObj = null;
-      try {
-        classObj = Class.forName(className);
-      } catch (ClassNotFoundException e) {
-        ClassLoader cccl = Thread.currentThread().getContextClassLoader();
-        classObj = cccl.loadClass(className);
-      }
-
+      classObj = classLoader.loadClass(className);
       if (!superClass.isAssignableFrom(classObj)) {
         throw new IncompatibleClassException(superClass, classObj);
       }
@@ -43,7 +44,8 @@
     } catch (IncompatibleClassException ice) {
       throw ice;
     } catch (Throwable t) {
-      throw new DynamicClassLoadingException("Failed to instantiate type "+className, t);
+      throw new DynamicClassLoadingException("Failed to instantiate type "
+          + className, t);
     }
   }
 

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/AbstractPatternLayoutBaseTest.java	Wed Dec 20 10:34:41 2006
@@ -28,6 +28,7 @@
 
   abstract public PatternLayoutBase getPatternLayoutBase();
   abstract public Object getEventObject();
+  abstract public Context getContext();
   
   public void testUnStarted() {
     PatternLayoutBase plb = getPatternLayoutBase();
@@ -46,6 +47,7 @@
    */
   public void testConverterStart() {
     PatternLayoutBase plb = getPatternLayoutBase();
+    plb.setContext(getContext());
     plb.getInstanceConverterMap().put("EX", ExceptionalConverter.class.getName());
     plb.setPattern("%EX");
     plb.start();

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/CompilerTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/CompilerTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/CompilerTest.java	Wed Dec 20 10:34:41 2006
@@ -13,21 +13,20 @@
 import java.util.Map;
 
 import junit.framework.TestCase;
-
-import ch.qos.logback.core.BasicStatusManager;
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
 import ch.qos.logback.core.pattern.Converter;
 import ch.qos.logback.core.pattern.Converter123;
 import ch.qos.logback.core.pattern.ConverterHello;
-import ch.qos.logback.core.pattern.parser.Node;
-import ch.qos.logback.core.pattern.parser.Parser;
 import ch.qos.logback.core.status.StatusChecker;
-import ch.qos.logback.core.status.StatusManager;
+import ch.qos.logback.core.util.StatusPrinter;
 
 
 public class CompilerTest extends TestCase {
 
   Map<String, String> converterMap = new HashMap<String, String>();
-
+  Context context = new ContextBase();
+  
   public CompilerTest(String arg0) {
     super(arg0);
     converterMap.put("OTT", Converter123.class.getName());
@@ -63,6 +62,7 @@
   public void testBasic() throws Exception {
     {
       Parser p = new Parser("abc %hello");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -70,6 +70,7 @@
     }
     {
       Parser p = new Parser("abc %hello %OTT");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -80,6 +81,7 @@
   public void testFormat() throws Exception {
     {
       Parser p = new Parser("abc %7hello");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -88,6 +90,7 @@
 
     {
       Parser p = new Parser("abc %-7hello");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -96,6 +99,7 @@
 
     {
       Parser p = new Parser("abc %.3hello");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -104,6 +108,7 @@
 
     {
       Parser p = new Parser("abc %.-3hello");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -112,6 +117,7 @@
 
     {
       Parser p = new Parser("abc %4.5OTT");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -119,6 +125,7 @@
     }
     {
       Parser p = new Parser("abc %-4.5OTT");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -126,6 +133,7 @@
     }
     {
       Parser p = new Parser("abc %3.4hello");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -133,6 +141,7 @@
     }
     {
       Parser p = new Parser("abc %-3.-4hello");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -141,22 +150,27 @@
   }
 
   public void testComposite() throws Exception {
+//    {
+//      Parser p = new Parser("%(ABC)");
+//      p.setContext(context);
+//      Node t = p.parse();
+//      Converter head = p.compile(t, converterMap);
+//      String result = write(head, new Object());
+//      assertEquals("ABC", result);
+//    }
     {
-      Parser p = new Parser("%(ABC)");
-      Node t = p.parse();
-      Converter head = p.compile(t, converterMap);
-      String result = write(head, new Object());
-      assertEquals("ABC", result);
-    }
-    {
+      Context c = new ContextBase();
       Parser p = new Parser("%(ABC %hello)");
+      p.setContext(c);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
+      StatusPrinter.print(c);
       assertEquals("ABC Hello", result);
     }
     {
       Parser p = new Parser("%(ABC %hello)");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -167,6 +181,7 @@
   public void testCompositeFormatting() throws Exception {
     {
       Parser p = new Parser("xyz %4.10(ABC)");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -175,6 +190,7 @@
 
     {
       Parser p = new Parser("xyz %-4.10(ABC)");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -183,6 +199,7 @@
 
     {
       Parser p = new Parser("xyz %.2(ABC %hello)");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -191,6 +208,7 @@
 
     {
       Parser p = new Parser("xyz %.-2(ABC)");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -199,6 +217,7 @@
 
     {
       Parser p = new Parser("xyz %30.30(ABC %20hello)");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());
@@ -208,11 +227,10 @@
 
   public void testUnknownWord() throws Exception {
     Parser p = new Parser("%unknown");
-    StatusManager sm = new BasicStatusManager();
-    p.setStatusManager(sm);
+    p.setContext(context);
     Node t = p.parse();
     p.compile(t, converterMap);
-    StatusChecker chercker = new StatusChecker(sm);
+    StatusChecker chercker = new StatusChecker(context.getStatusManager());
     assertTrue(chercker
         .containsMatch("\\[unknown] is not a valid conversion word"));
   }
@@ -220,6 +238,7 @@
   public void testWithNopEscape() throws Exception {
     {
       Parser p = new Parser("xyz %hello\\_world");
+      p.setContext(context);
       Node t = p.parse();
       Converter head = p.compile(t, converterMap);
       String result = write(head, new Object());

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/SamplePatternLayoutTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/SamplePatternLayoutTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/pattern/parser/SamplePatternLayoutTest.java	Wed Dec 20 10:34:41 2006
@@ -16,10 +16,12 @@
 
 public class SamplePatternLayoutTest extends AbstractPatternLayoutBaseTest {
 
+  Context context = new ContextBase();
   public SamplePatternLayoutTest(String arg) {
     super(arg);
   }
   
+
   public PatternLayoutBase getPatternLayoutBase() {
     return new SamplePatternLayout();
   }
@@ -42,4 +44,9 @@
     assertEquals("x123", s);
 
   }
+
+  @Override
+  public Context getContext() {
+    return  context;
+  }
 }



More information about the logback-dev mailing list