[logback-dev] svn commit: r2416 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/rolling main/java/ch/qos/logback/core/rolling/helper test/java/ch/qos/logback/core/rolling
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Thu Aug 6 23:34:47 CEST 2009
Author: ceki
Date: Thu Aug 6 23:34:46 2009
New Revision: 2416
Added:
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileFilterUtil.java
Modified:
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileNamePattern.java
logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java
logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java
Log:
- removed duplicate code as much as possible
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP.java Thu Aug 6 23:34:46 2009
@@ -10,14 +10,10 @@
package ch.qos.logback.core.rolling;
import java.io.File;
-import java.io.FilenameFilter;
-import java.util.Arrays;
-import java.util.Comparator;
import java.util.Date;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import ch.qos.logback.core.joran.spi.NoAutoStart;
+import ch.qos.logback.core.rolling.helper.FileFilterUtil;
import ch.qos.logback.core.util.FileSize;
@NoAutoStart
@@ -37,58 +33,26 @@
// we need to get the correct value of currentPeriodsCounter.
// usually the value is 0, unless the appender or the application
// is stopped and restarted within the same period
-
if (tbrp.getParentsRawFileProperty() == null) {
- String sregex = tbrp.fileNamePattern.toSRegex(dateInCurrentPeriod);
- String simplifiedRegex = afterLastSlash(sregex);
- computeCurrentPeriodsCounter(simplifiedRegex);
+ String slashifiedRegex = tbrp.fileNamePattern.toSRegex(dateInCurrentPeriod);
+ String stemRegex = FileFilterUtil.afterLastSlash(slashifiedRegex);
+ computeCurrentPeriodsHighestCounterValue(stemRegex);
}
started = true;
}
- String afterLastSlash(String sregex) {
- int i = sregex.lastIndexOf('/');
- if (i == -1) {
- return sregex;
- } else {
- return sregex.substring(i + 1);
- }
- }
-
- void computeCurrentPeriodsCounter(final String simplifiedRegex) {
+ void computeCurrentPeriodsHighestCounterValue(final String stemRegex) {
File file = new File(getCurrentPeriodsFileNameWithoutCompressionSuffix());
File parentDir = file.getParentFile();
- if (parentDir != null && parentDir.isDirectory()) {
- File[] matchingFileArray = parentDir.listFiles(new FilenameFilter() {
- public boolean accept(File dir, String name) {
- return name.matches(simplifiedRegex);
- }
- });
- if (matchingFileArray == null || matchingFileArray.length == 0) {
- return;
- }
- Arrays.sort(matchingFileArray, new Comparator<File>() {
- public int compare(File o1, File o2) {
- String o1Name = o1.getName();
- String o2Name = o2.getName();
- return (o2Name.compareTo(o1Name));
- }
- });
- File lastFile = matchingFileArray[0];
-
- Pattern p = Pattern.compile(simplifiedRegex);
- String lastFileName = lastFile.getName();
-
- Matcher m = p.matcher(lastFileName);
- if (!m.matches()) {
- throw new IllegalStateException("The regex [" + simplifiedRegex
- + "] should match [" + lastFileName + "]");
- }
- String currentPeriodsCounterAsStr = m.group(1);
- currentPeriodsCounter = new Integer(currentPeriodsCounterAsStr)
- .intValue();
+ File[] matchingFileArray = FileFilterUtil
+ .filesInFolderMatchingStemRegex(parentDir, stemRegex);
+
+ if (matchingFileArray == null || matchingFileArray.length == 0) {
+ return;
}
+ FileFilterUtil.reverseSortFileArrayByName(matchingFileArray);
+ currentPeriodsCounter = FileFilterUtil.extractCounter(matchingFileArray[0], stemRegex);
}
// IMPORTANT: This field can be updated by multiple threads. It follows that
Added: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileFilterUtil.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/rolling/helper/FileFilterUtil.java Thu Aug 6 23:34:46 2009
@@ -0,0 +1,90 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ *
+ * 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
+ * Software Foundation.
+ */
+
+package ch.qos.logback.core.rolling.helper;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class FileFilterUtil {
+
+ public static void sortFileArrayByName(File[] fileArray) {
+ Arrays.sort(fileArray, new Comparator<File>() {
+ public int compare(File o1, File o2) {
+ String o1Name = o1.getName();
+ String o2Name = o2.getName();
+ return (o1Name.compareTo(o2Name));
+ }
+ });
+ }
+
+ public static void reverseSortFileArrayByName(File[] fileArray) {
+ Arrays.sort(fileArray, new Comparator<File>() {
+ public int compare(File o1, File o2) {
+ String o1Name = o1.getName();
+ String o2Name = o2.getName();
+ return (o2Name.compareTo(o1Name));
+ }
+ });
+ }
+
+ public static String afterLastSlash(String sregex) {
+ int i = sregex.lastIndexOf('/');
+ if (i == -1) {
+ return sregex;
+ } else {
+ return sregex.substring(i + 1);
+ }
+ }
+
+ /**
+ * Return the set of files matching the stemRegex as found in 'directory'. A
+ * stemRegex does not contain any slash characters or any folder seperators.
+ *
+ * @param directory
+ * @param stemRegex
+ * @return
+ */
+ public static File[] filesInFolderMatchingStemRegex(File directory,
+ final String stemRegex) {
+ if (directory == null || directory.isDirectory()) {
+ throw new IllegalArgumentException("[" + directory
+ + " cannot be null or a non-directory");
+ }
+ File[] matchingFileArray = directory.listFiles(new FilenameFilter() {
+ public boolean accept(File dir, String name) {
+ return name.matches(stemRegex);
+ }
+ });
+ return matchingFileArray;
+ }
+
+ static public int extractCounter(File file, final String stemRegex) {
+ Pattern p = Pattern.compile(stemRegex);
+ String lastFileName = file.getName();
+
+ Matcher m = p.matcher(lastFileName);
+ if (!m.matches()) {
+ throw new IllegalStateException("The regex [" + stemRegex
+ + "] should match [" + lastFileName + "]");
+ }
+ String counterAsStr = m.group(1);
+ int counter = new Integer(counterAsStr).intValue();
+ return counter;
+ }
+
+ static String slashify(String in) {
+ return in.replace('\\', '/');
+ }
+}
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 Thu Aug 6 23:34:46 2009
@@ -150,7 +150,7 @@
Converter<Object> p = headTokenConverter;
while (p != null) {
if (p instanceof LiteralConverter) {
- buf.append(slashify(p.convert(null)));
+ buf.append(FileFilterUtil.slashify(p.convert(null)));
} else if (p instanceof IntegerTokenConverter) {
buf.append("(\\d{1,2})");
} else if (p instanceof DateTokenConverter) {
@@ -161,10 +161,6 @@
return buf.toString();
}
- private String slashify(String in) {
- return in.replace('\\', '/');
- }
-
/**
* Given date, convert this instance to a slashified regular expression
*/
@@ -173,7 +169,7 @@
Converter<Object> p = headTokenConverter;
while (p != null) {
if (p instanceof LiteralConverter) {
- buf.append(slashify(p.convert(null)));
+ buf.append(FileFilterUtil.slashify(p.convert(null)));
} else if (p instanceof IntegerTokenConverter) {
buf.append("\\d{1,2}");
} else if (p instanceof DateTokenConverter) {
Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java (original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/ScaffoldingForRollingTests.java Thu Aug 6 23:34:46 2009
@@ -17,14 +17,13 @@
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Calendar;
-import java.util.Comparator;
import java.util.List;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.ContextBase;
import ch.qos.logback.core.layout.EchoLayout;
+import ch.qos.logback.core.rolling.helper.FileFilterUtil;
import ch.qos.logback.core.testUtil.FileToBufferUtil;
import ch.qos.logback.core.testUtil.RandomUtil;
import ch.qos.logback.core.util.CoreTestConstants;
@@ -89,28 +88,14 @@
public static void sortedContentCheck(String outputDirStr, int runLength,
String prefix) throws IOException {
File[] fileArray = getFilesInDirectory(outputDirStr);
- Arrays.sort(fileArray, new Comparator<File>() {
- public int compare(File o1, File o2) {
- String o1Name = o1.getName();
- String o2Name = o2.getName();
- return (o1Name.compareTo(o2Name));
- }
- });
+ FileFilterUtil.sortFileArrayByName(fileArray);
fileContentCheck(fileArray, runLength, prefix);
}
public static void reverseSortedContentCheck(String outputDirStr,
int runLength, String prefix) throws IOException {
File[] fileArray = getFilesInDirectory(outputDirStr);
-
- Arrays.sort(fileArray, new Comparator<File>() {
- public int compare(File o1, File o2) {
- String o1Name = o1.getName();
- String o2Name = o2.getName();
- return (o2Name.compareTo(o1Name));
- }
- });
- System.out.println(Arrays.toString(fileArray));
+ FileFilterUtil.reverseSortFileArrayByName(fileArray);
fileContentCheck(fileArray, runLength, prefix);
}
Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java (original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_Test.java Thu Aug 6 23:34:46 2009
@@ -16,7 +16,6 @@
import org.junit.Before;
import org.junit.Test;
-import ch.qos.logback.core.status.StatusChecker;
import ch.qos.logback.core.util.StatusPrinter;
public class SizeAndTimeBasedFNATP_Test extends
More information about the logback-dev
mailing list