[logback-dev] svn commit: r2368 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/rolling main/java/ch/qos/logback/core/rolling/helper main/java/ch/qos/logback/core/spi test/java/ch/qos/logback/core/rolling/helper

noreply.ceki at qos.ch noreply.ceki at qos.ch
Thu Jul 23 20:45:20 CEST 2009


Author: ceki
Date: Thu Jul 23 20:45:20 2009
New Revision: 2368

Modified:
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/AsynchronousCompressor.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionRunnable.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java
   logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareBase.java
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/CompressTest.java

Log:
- refactored Compressor class. The String nameOfFile2Compress and
nameOfCompressedFile strings are no longer constructor parameters but
parameters to the compress() method. This results in shorter code.


Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/FixedWindowRollingPolicy.java	Thu Jul 23 20:45:20 2009
@@ -12,7 +12,6 @@
 import java.io.File;
 
 import ch.qos.logback.core.CoreConstants;
-import ch.qos.logback.core.rolling.helper.CompressionMode;
 import ch.qos.logback.core.rolling.helper.Compressor;
 import ch.qos.logback.core.rolling.helper.FileNamePattern;
 import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
@@ -34,7 +33,8 @@
   int maxIndex;
   int minIndex;
   RenameUtil util = new RenameUtil();
-
+  Compressor compressor;
+  
   /**
    * It's almost always a bad idea to have a large window size, say over 12.
    */
@@ -89,6 +89,9 @@
           + fileNamePattern.getPattern()
           + "] does not contain a valid IntegerToken");
     }
+
+    compressor = new Compressor(compressionMode);
+    compressor.setContext(this.context);
   }
 
   public void rollover() throws RolloverFailure {
@@ -116,23 +119,14 @@
       }
 
       // move active file name to min
-      Compressor compressor;
       switch (compressionMode) {
       case NONE:
         util.rename(getActiveFileName(), fileNamePattern
             .convertInt(minIndex));
         break;
       case GZ:
-        compressor = new Compressor(CompressionMode.GZ,
-            getActiveFileName(), fileNamePattern.convertInt(minIndex));
-        compressor.setContext(this.context);
-        compressor.compress();
-        break;
       case ZIP:
-        compressor = new Compressor(CompressionMode.ZIP,
-            getActiveFileName(), fileNamePattern.convertInt(minIndex));
-        compressor.setContext(this.context);
-        compressor.compress();
+        compressor.compress(getActiveFileName(), fileNamePattern.convertInt(minIndex));
         break;
       }
     }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedRollingPolicy.java	Thu Jul 23 20:45:20 2009
@@ -46,6 +46,7 @@
   Date lastCheck = null;
   String elapsedPeriodsFileName;
   FileNamePattern activeFileNamePattern;
+  Compressor compressor;
   RenameUtil util = new RenameUtil();
   Future<?> future;
 
@@ -88,12 +89,14 @@
           + "] does not contain a valid DateToken");
     }
 
+    compressor = new Compressor(compressionMode);
+    compressor.setContext(context);
+
     int len = fileNamePatternStr.length();
     switch (compressionMode) {
     case GZ:
       activeFileNamePattern = new FileNamePattern(fileNamePatternStr.substring(
-          0, len - 3), this.context);
-
+          0, len - 3), this.context);;
       break;
     case ZIP:
       activeFileNamePattern = new FileNamePattern(fileNamePatternStr.substring(
@@ -102,6 +105,7 @@
     case NONE:
       activeFileNamePattern = fileNamePattern;
     }
+    
     addInfo("Will use the pattern " + activeFileNamePattern
         + " for the active file");
 
@@ -154,7 +158,7 @@
       if(getParentsRawFileProperty() == null) {
         doCompression(false, elapsedPeriodsFileName, elapsedPeriodsFileName);
       } else {
-        doCompression(true, elapsedPeriodsFileName, elapsedPeriodsFileName);
+        doCompression(true, null, elapsedPeriodsFileName);
       }
     }
     
@@ -165,7 +169,6 @@
 
   void doCompression(boolean renameToTempFile, String nameOfFile2Compress,
       String nameOfCompressedFile) throws RolloverFailure {
-    Compressor compressor = null;
 
     if (renameToTempFile) {
       String tmpTarget = nameOfFile2Compress + System.nanoTime() + ".tmp";
@@ -173,24 +176,8 @@
       nameOfFile2Compress = tmpTarget;
     }
 
-    switch (compressionMode) {
-    case GZ:
-      addInfo("GZIP compressing [" + nameOfFile2Compress + "].");
-      compressor = new Compressor(CompressionMode.GZ, nameOfFile2Compress,
-          nameOfCompressedFile);
-      compressor.setContext(this.context);
-      break;
-    case ZIP:
-      addInfo("ZIP compressing [" + nameOfFile2Compress + "]");
-      compressor = new Compressor(CompressionMode.ZIP, nameOfFile2Compress,
-          nameOfCompressedFile);
-      compressor.setContext(this.context);
-      break;
-    }
-
     AsynchronousCompressor ac = new AsynchronousCompressor(compressor);
-    future = ac.compressAsynchronously();
-
+    future = ac.compressAsynchronously(nameOfFile2Compress, nameOfCompressedFile);
   }
 
   /**

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/AsynchronousCompressor.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/AsynchronousCompressor.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/AsynchronousCompressor.java	Thu Jul 23 20:45:20 2009
@@ -6,16 +6,18 @@
 
 public class AsynchronousCompressor {
   Compressor compressor;
-  
+
   public AsynchronousCompressor(Compressor compressor) {
     this.compressor = compressor;
   }
-  
-  public Future<?> compressAsynchronously() {
+
+  public Future<?> compressAsynchronously(String nameOfFile2Compress,
+      String nameOfCompressedFile) {
     ExecutorService executor = Executors.newScheduledThreadPool(1);
-    Future<?> future = executor.submit(new CompressionRunnable(compressor));
+    Future<?> future = executor.submit(new CompressionRunnable(compressor,
+        nameOfFile2Compress, nameOfCompressedFile));
     executor.shutdown();
     return future;
   }
-  
+
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionRunnable.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionRunnable.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/CompressionRunnable.java	Thu Jul 23 20:45:20 2009
@@ -4,11 +4,17 @@
 public class CompressionRunnable implements Runnable {
 
   final Compressor compressor;
-  public CompressionRunnable(Compressor compressor) {
+  final String nameOfFile2Compress;
+  final  String nameOfCompressedFile;
+  
+  public CompressionRunnable(Compressor compressor, String nameOfFile2Compress,
+      String nameOfCompressedFile) {
     this.compressor = compressor;
+    this.nameOfFile2Compress = nameOfFile2Compress;
+    this.nameOfCompressedFile = nameOfCompressedFile;
   }
 
   public void run() {
-    compressor.compress();
+    compressor.compress(nameOfFile2Compress, nameOfCompressedFile);
   }
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java	Thu Jul 23 20:45:20 2009
@@ -30,30 +30,33 @@
 public class Compressor extends ContextAwareBase {
 
   final CompressionMode compressionMode;
-  final String nameOfFile2Compress;
-  final String nameOfCompressedFile;
+  //final String nameOfFile2Compress;
+  //final String nameOfCompressedFile;
 
-  
-  public Compressor(CompressionMode compressionMode, String nameOfFile2Compress, String nameOfCompressedFile) {
+
+  public Compressor(CompressionMode compressionMode) {
     this.compressionMode = compressionMode;
-    this.nameOfFile2Compress = nameOfFile2Compress;
-    this.nameOfCompressedFile = nameOfCompressedFile;
   }
   
-  public Compressor(CompressionMode compressionMode, String nameOfFile2Compress) {
-    // Here we rely on the fact that the two argument version of ZIPCompress/GZCompress
-    // automatically adds a .zip/.gz extension to the second argument
-    this(compressionMode, nameOfFile2Compress, nameOfFile2Compress);
-  }
+  
+//  public Compressor(CompressionMode compressionMode, String nameOfFile2Compress, String nameOfCompressedFile) {
+//    this.compressionMode = compressionMode;
+//    //this.nameOfFile2Compress = nameOfFile2Compress;
+//    //this.nameOfCompressedFile = nameOfCompressedFile;
+//  }
 
-  public void compress() {
+  public void compress( String nameOfFile2Compress, String nameOfCompressedFile) {
     switch(compressionMode) {
     case GZ: 
+      addInfo("GZ compressing [" + nameOfFile2Compress + "].");
       gzCompress(nameOfFile2Compress, nameOfCompressedFile);
       break;
     case ZIP:
+      addInfo("ZIP compressing [" + nameOfFile2Compress + "].");
       zipCompress(nameOfFile2Compress, nameOfCompressedFile);
       break;
+    case NONE:
+      throw new UnsupportedOperationException("compress method called in NONE compression mode");
     }
   }
 
@@ -161,4 +164,5 @@
   public String toString() {
     return "c.q.l.core.rolling.helper.Compress";
   }
+
 }

Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareBase.java	(original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/spi/ContextAwareBase.java	Thu Jul 23 20:45:20 2009
@@ -16,8 +16,6 @@
 import ch.qos.logback.core.status.StatusManager;
 import ch.qos.logback.core.status.WarnStatus;
 
-
-
 /**
  * A helper class that implements ContextAware methods. A class can implement
  * the ContextAware interface by deriving from this class.
@@ -47,6 +45,16 @@
     return context.getStatusManager();
   }
 
+  /**
+   * The declared origin of status messages. By default 'this'. Derived classes may override this
+   * method to declare other origin.
+   * 
+   * @return the declared origin, by default 'this'
+   */ 
+  protected Object getDeclaredOrigin() {
+    return this;
+  }
+
   public void addStatus(Status status) {
     if (context == null) {
       if (noContextWarning++ == 0) {
@@ -61,26 +69,26 @@
   }
 
   public void addInfo(String msg) {
-    addStatus(new InfoStatus(msg, this));
+    addStatus(new InfoStatus(msg, getDeclaredOrigin()));
   }
 
   public void addInfo(String msg, Throwable ex) {
-    addStatus(new InfoStatus(msg, this, ex));
+    addStatus(new InfoStatus(msg, getDeclaredOrigin(), ex));
   }
 
   public void addWarn(String msg) {
-    addStatus(new WarnStatus(msg, this));
+    addStatus(new WarnStatus(msg, getDeclaredOrigin()));
   }
 
   public void addWarn(String msg, Throwable ex) {
-    addStatus(new WarnStatus(msg, this, ex));
+    addStatus(new WarnStatus(msg, getDeclaredOrigin(), ex));
   }
 
   public void addError(String msg) {
-    addStatus(new ErrorStatus(msg, this));
+    addStatus(new ErrorStatus(msg, getDeclaredOrigin()));
   }
 
   public void addError(String msg, Throwable ex) {
-    addStatus(new ErrorStatus(msg, this, ex));
+    addStatus(new ErrorStatus(msg, getDeclaredOrigin(), ex));
   }
 }

Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/CompressTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/CompressTest.java	(original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/helper/CompressTest.java	Thu Jul 23 20:45:20 2009
@@ -1,7 +1,7 @@
 /**
- * LOGBack: the generic, reliable, fast and flexible logging framework.
+ * Logback: the generic, reliable, fast and flexible logging framework.
  * 
- * Copyright (C) 1999-2006, QOS.ch
+ * 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
@@ -9,7 +9,6 @@
  */
 package ch.qos.logback.core.rolling.helper;
 
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
@@ -24,9 +23,9 @@
 
 import ch.qos.logback.core.Context;
 import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.status.StatusChecker;
 import ch.qos.logback.core.util.Compare;
 import ch.qos.logback.core.util.CoreTestConstants;
-import ch.qos.logback.core.util.StatusPrinter;
 
 /**
  * @author Ceki Gulcu
@@ -72,14 +71,13 @@
 
   @Test
   public void test1() throws Exception {
-    Compressor compressor = new Compressor(CompressionMode.GZ,
-        CoreTestConstants.TEST_DIR_PREFIX + "input/compress1.txt",
-        CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz");
+    Compressor compressor = new Compressor(CompressionMode.GZ);
     compressor.setContext(context);
-    compressor.compress();
+    compressor.compress(CoreTestConstants.TEST_DIR_PREFIX + "input/compress1.txt",
+        CoreTestConstants.OUTPUT_DIR_PREFIX + "compress1.txt.gz");
 
-    StatusPrinter.print(context.getStatusManager());
-    assertEquals(0, context.getStatusManager().getCount());
+    StatusChecker checker = new StatusChecker(context);
+    assertTrue(checker.isErrorFree());
     assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX
         + "compress1.txt.gz", CoreTestConstants.TEST_DIR_PREFIX
         + "witness/compress1.txt.gz"));
@@ -87,14 +85,14 @@
 
   @Test
   public void test2() throws Exception {
-    Compressor compressor = new Compressor(CompressionMode.GZ,
-        CoreTestConstants.TEST_DIR_PREFIX + "input/compress2.txt",
-        CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt");
+    Compressor compressor = new Compressor(CompressionMode.GZ);
     compressor.setContext(context);
-    compressor.compress();
+    compressor.compress(CoreTestConstants.TEST_DIR_PREFIX + "input/compress2.txt",
+        CoreTestConstants.OUTPUT_DIR_PREFIX + "compress2.txt");
+
+    StatusChecker checker = new StatusChecker(context);
+    assertTrue(checker.isErrorFree());
 
-    StatusPrinter.print(context.getStatusManager());
-    assertEquals(0, context.getStatusManager().getCount());
     assertTrue(Compare.gzCompare(CoreTestConstants.OUTPUT_DIR_PREFIX
         + "compress2.txt.gz", CoreTestConstants.TEST_DIR_PREFIX
         + "witness/compress2.txt.gz"));
@@ -102,13 +100,14 @@
 
   @Test
   public void test3() throws Exception {
-    Compressor compressor = new Compressor(CompressionMode.ZIP, 
+    Compressor compressor = new Compressor(CompressionMode.ZIP);
+    compressor.setContext(context);
+    compressor.compress( 
         CoreTestConstants.TEST_DIR_PREFIX + "input/compress3.txt",
         CoreTestConstants.OUTPUT_DIR_PREFIX + "compress3.txt");
-    compressor.setContext(context);
-    compressor.compress();
-    StatusPrinter.print(context.getStatusManager());
-    assertEquals(0, context.getStatusManager().getCount());
+    StatusChecker checker = new StatusChecker(context);
+    assertTrue(checker.isErrorFree());
+
     // assertTrue(Compare.compare("output/compress3.txt.zip",
     // "witness/compress3.txt.zip"));
   }


More information about the logback-dev mailing list