[logback-dev] svn commit: r1430 - in logback/trunk: logback-classic/src/main/java/ch/qos/logback/classic/joran/action logback-classic/src/test/input/joran logback-classic/src/test/java/ch/qos/logback/classic/joran logback-examples/src/main/java/chapter3

noreply.seb at qos.ch noreply.seb at qos.ch
Thu Mar 15 14:56:44 CET 2007


Author: seb
Date: Thu Mar 15 14:56:44 2007
New Revision: 1430

Added:
   logback/trunk/logback-classic/src/test/input/joran/redirectToFile.xml
   logback/trunk/logback-classic/src/test/input/joran/redirectToUrl.xml
   logback/trunk/logback-examples/src/main/java/chapter3/includedConfig.xml
   logback/trunk/logback-examples/src/main/java/chapter3/redirectConfig.xml
Removed:
   logback/trunk/logback-classic/src/test/input/joran/redirectToInvalid.xml
   logback/trunk/logback-classic/src/test/input/joran/redirectWithSubst.xml
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/IncludeFileAction.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/IncludeFileActionTest.java

Log:
Added url attribute to include element
Modified tests

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/IncludeFileAction.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/IncludeFileAction.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/joran/action/IncludeFileAction.java	Thu Mar 15 14:56:44 2007
@@ -12,6 +12,8 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
 
 import org.xml.sax.Attributes;
 
@@ -26,56 +28,102 @@
 
   private static final String INCLUDED_TAG = "included";
   private static final String FILE_ATTR = "file";
-  private SaxEventRecorder recorder;
-  
+  private static final String URL_ATTR = "url";
+  private SaxEventRecorder recorder = new SaxEventRecorder();;
+
   @Override
   public void begin(InterpretationContext ec, String name, Attributes attributes)
       throws ActionException {
 
-    String attribute = attributes.getValue(FILE_ATTR);
-    if (attribute == null) {
-      addError("Path to configuration file to include is not set.");
+    String attFile = attributes.getValue(FILE_ATTR);
+    String attUrl = attributes.getValue(URL_ATTR);
+    // if both are null, report error and do nothing.
+    if (attFile == null && attUrl == null) {
+      addError("One of path and URL attribute must be set.");
       return;
     }
-    
-    String pathToFile;
-    
-    if (attribute.startsWith("$")) {
-      pathToFile = ec.subst(attribute);
-    } else {
-      pathToFile = attribute;
+
+    String pathToFile = null;
+    if (attFile != null) {
+      if (attFile.startsWith("$")) {
+        pathToFile = ec.subst(attFile);
+      } else {
+        pathToFile = attFile;
+      }
+    }
+
+    URL urlToFile = null;
+    String tmpUrl;
+    if (attUrl != null) {
+      if (attUrl.startsWith("$")) {
+        tmpUrl = ec.subst(attUrl);
+      } else {
+        tmpUrl = attUrl;
+      }
+      try {
+        urlToFile = new URL(tmpUrl);
+      } catch (MalformedURLException mue) {
+        String errMsg = "URL [" + tmpUrl + "] is not well formed.";
+        addError(errMsg, mue);
+        return;
+      }
     }
 
+    // we know now that either pathToFile or urlToFile
+    // is not null and correctly formed (in case of urlToFile).
+
     try {
-      InputStream in = new FileInputStream(pathToFile);
-      parseAndRecord(in);
-      in.close();
-    } catch (IOException ioe) {
-      String errMsg = "File [" + pathToFile + "] does not exist.";
-      addError(errMsg, ioe);
+      InputStream in = getInputStream(pathToFile, urlToFile);
+      if (in != null) {
+        parseAndRecord(in);
+        in.close();
+      }
     } catch (JoranException e) {
       addError("Error while parsing file " + pathToFile + e);
+    } catch (IOException e) {
+      // called if in.close did not work
     }
-    
+
     if (recorder.saxEventList.size() == 0) {
       return;
     }
-    
+
+    //Let's remove the two <included> events before
+    //adding the events to the player.
     SaxEvent first = recorder.saxEventList.get(0);
     if (first != null && first.qName.equalsIgnoreCase(INCLUDED_TAG)) {
       recorder.saxEventList.remove(0);
     }
 
-    SaxEvent last = recorder.saxEventList.get(recorder.saxEventList.size()-1);
+    SaxEvent last = recorder.saxEventList.get(recorder.saxEventList.size() - 1);
     if (last != null && last.qName.equalsIgnoreCase(INCLUDED_TAG)) {
-      recorder.saxEventList.remove(recorder.saxEventList.size()-1);
+      recorder.saxEventList.remove(recorder.saxEventList.size() - 1);
     }
-    
+
     ec.getJoranInterpreter().addEvents(recorder.saxEventList);
   }
-  
+
+  private InputStream getInputStream(String pathToFile, URL urlToFile) {
+    if (pathToFile != null) {
+      try {
+        return new FileInputStream(pathToFile);
+      } catch (IOException ioe) {
+        String errMsg = "File [" + pathToFile + "] does not exist.";
+        addError(errMsg, ioe);
+        return null;
+      }
+    } else {
+      try {
+        return urlToFile.openStream();
+      } catch (IOException e) {
+        String errMsg = "URL [" + urlToFile.toString() + "] does not exist.";
+        addError(errMsg, e);
+        return null;
+      }
+    }
+  }
+
   private void parseAndRecord(InputStream inputSource) throws JoranException {
-    recorder = new SaxEventRecorder();
     recorder.setContext(context);
     recorder.recordEvents(inputSource);
   }

Added: logback/trunk/logback-classic/src/test/input/joran/redirectToFile.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/input/joran/redirectToFile.xml	Thu Mar 15 14:56:44 2007
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration>
+
+<configuration>
+
+  <include file="${testing.value.file}" />
+
+</configuration>

Added: logback/trunk/logback-classic/src/test/input/joran/redirectToUrl.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/input/joran/redirectToUrl.xml	Thu Mar 15 14:56:44 2007
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration>
+
+<configuration>
+
+  <include url="${testing.value.url}" />
+
+</configuration>

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/IncludeFileActionTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/IncludeFileActionTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/joran/IncludeFileActionTest.java	Thu Mar 15 14:56:44 2007
@@ -10,20 +10,23 @@
 import ch.qos.logback.core.ConsoleAppender;
 import ch.qos.logback.core.joran.spi.JoranException;
 import ch.qos.logback.core.status.Status;
+import ch.qos.logback.core.util.StatusPrinter;
 
 public class IncludeFileActionTest extends TestCase {
 
   LoggerContext context;
   IncludeFileAction action;
 
-  String filePath = Constants.TEST_DIR_PREFIX
-      + "input/joran/redirectConfig.xml";
-  String invalidRedirect = Constants.TEST_DIR_PREFIX
-      + "input/joran/invalidRedirect.xml";
-  String filePathWithSubst = Constants.TEST_DIR_PREFIX
-      + "input/joran/redirectWithSubst.xml";
-  String redirectToInvalid = Constants.TEST_DIR_PREFIX
-      + "input/joran/redirectToInvalid.xml";
+  String redirectToFile = Constants.TEST_DIR_PREFIX
+      + "input/joran/redirectToFile.xml";
+  String redirectToURL = Constants.TEST_DIR_PREFIX
+      + "input/joran/redirectToUrl.xml";
+
+  String urlConfig = "http://logback.qos.ch/simpleConfig.xml";
+  String simpleConfig = Constants.TEST_DIR_PREFIX
+      + "input/joran/simpleConfig.xml";
+  String invalidConfig = Constants.TEST_DIR_PREFIX
+      + "input/joran/invalidConfig.xml";
 
   @Override
   protected void setUp() throws Exception {
@@ -41,37 +44,63 @@
   }
 
   public void testLoadFileOK() throws JoranException {
+    System.setProperty("testing.value.file", simpleConfig);
     JoranConfigurator jc = new JoranConfigurator();
     jc.setContext(context);
-    jc.doConfigure(filePath);
+    jc.doConfigure(redirectToFile);
 
     verifyConfig();
   }
 
   public void testNoFileFound() throws JoranException {
+    System.setProperty("testing.value.file", "toto");
     JoranConfigurator jc = new JoranConfigurator();
     jc.setContext(context);
-    jc.doConfigure(invalidRedirect);
+    jc.doConfigure(redirectToFile);
 
-    assertEquals(3, context.getStatusManager().getCount());
+    assertEquals(2, context.getStatusManager().getCount());
     assertEquals(Status.ERROR, context.getStatusManager().getLevel());
   }
 
   public void testWithCorruptFile() throws JoranException {
+    System.setProperty("testing.value.file", invalidConfig);
     JoranConfigurator jc = new JoranConfigurator();
     jc.setContext(context);
-    jc.doConfigure(redirectToInvalid);
+    jc.doConfigure(redirectToFile);
 
     assertEquals(10, context.getStatusManager().getCount());
     assertEquals(Status.ERROR, context.getStatusManager().getLevel());
   }
 
-  public void testWithSubst() throws JoranException {
+//  public void testURLOK() throws JoranException {
+//    //This one needs that we put a file on the web
+//    //and requires a net connection on the test-runner's side.
+//    System.setProperty("testing.value.url", urlConfig);
+//    JoranConfigurator jc = new JoranConfigurator();
+//    jc.setContext(context);
+//    jc.doConfigure(redirectToURL);
+//
+//    verifyConfig();
+//  }
+
+  public void testMalformedURL() throws JoranException {
+    System.setProperty("testing.value.url", "htp://logback.qos.ch");
     JoranConfigurator jc = new JoranConfigurator();
     jc.setContext(context);
-    jc.doConfigure(filePathWithSubst);
+    jc.doConfigure(redirectToURL);
 
-    verifyConfig();
+    assertEquals(2, context.getStatusManager().getCount());
+    assertEquals(Status.ERROR, context.getStatusManager().getLevel());
+  }
+
+  public void testUnknownURL() throws JoranException {
+    System.setProperty("testing.value.url", "http://logback2345.qos.ch");
+    JoranConfigurator jc = new JoranConfigurator();
+    jc.setContext(context);
+    jc.doConfigure(redirectToURL);
+
+    assertEquals(2, context.getStatusManager().getCount());
+    assertEquals(Status.ERROR, context.getStatusManager().getLevel());
   }
 
   private void verifyConfig() {

Added: logback/trunk/logback-examples/src/main/java/chapter3/includedConfig.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter3/includedConfig.xml	Thu Mar 15 14:56:44 2007
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<included>
+
+  <appender name="redirectConsole"
+    class="ch.qos.logback.core.ConsoleAppender">
+    <layout class="ch.qos.logback.classic.PatternLayout">
+      <param name="Pattern" value="%d - %m%n" />
+    </layout>
+  </appender>
+
+  <root>
+    <level value="DEBUG" />
+    <appender-ref ref="redirectConsole" />
+  </root>
+
+</included>
\ No newline at end of file

Added: logback/trunk/logback-examples/src/main/java/chapter3/redirectConfig.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-examples/src/main/java/chapter3/redirectConfig.xml	Thu Mar 15 14:56:44 2007
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<configuration>
+
+  <include file="path/to/included/configuration/file" />
+
+</configuration>



More information about the logback-dev mailing list