Skip to content

Commit d886e9b

Browse files
committed
fix LBCORE-169
1 parent 560596f commit d886e9b

File tree

14 files changed

+132
-86
lines changed

14 files changed

+132
-86
lines changed

logback-core/src/main/java/ch/qos/logback/core/rolling/TimeBasedFileNamingAndTriggeringPolicyBase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public void start() {
5555
+ "'.");
5656
rc.printPeriodicity(this);
5757

58-
5958
setDateInCurrentPeriod(new Date(getCurrentTime()));
6059
if (tbrp.getParentsRawFileProperty() != null) {
6160
File currentFile = new File(tbrp.getParentsRawFileProperty());

logback-core/src/main/java/ch/qos/logback/core/rolling/helper/Compressor.java

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@
2222
import java.util.zip.ZipEntry;
2323
import java.util.zip.ZipOutputStream;
2424

25+
import ch.qos.logback.core.rolling.RolloverFailure;
2526
import ch.qos.logback.core.spi.ContextAwareBase;
2627
import ch.qos.logback.core.status.ErrorStatus;
2728
import ch.qos.logback.core.status.WarnStatus;
29+
import ch.qos.logback.core.util.FileUtil;
2830

2931
/**
3032
* The <code>Compression</code> class implements ZIP and GZ file
3133
* compression/decompression methods.
32-
*
34+
*
3335
* @author Ceki G&uuml;lc&uuml;
3436
*/
3537
public class Compressor extends ContextAwareBase {
@@ -40,25 +42,22 @@ public Compressor(CompressionMode compressionMode) {
4042
this.compressionMode = compressionMode;
4143
}
4244

43-
/**
44-
*
45-
* @param nameOfFile2Compress
46-
* @param nameOfCompressedFile
47-
* @param innerEntryName The name of the file within the zip file. Use for ZIP compression.
48-
*/
45+
/**
46+
* @param nameOfFile2Compress
47+
* @param nameOfCompressedFile
48+
* @param innerEntryName The name of the file within the zip file. Use for ZIP compression.
49+
*/
4950
public void compress(String nameOfFile2Compress, String nameOfCompressedFile, String innerEntryName) {
5051
switch (compressionMode) {
51-
case GZ:
52-
addInfo("GZ compressing [" + nameOfFile2Compress + "].");
53-
gzCompress(nameOfFile2Compress, nameOfCompressedFile);
54-
break;
55-
case ZIP:
56-
addInfo("ZIP compressing [" + nameOfFile2Compress + "].");
57-
zipCompress(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
58-
break;
59-
case NONE:
60-
throw new UnsupportedOperationException(
61-
"compress method called in NONE compression mode");
52+
case GZ:
53+
gzCompress(nameOfFile2Compress, nameOfCompressedFile);
54+
break;
55+
case ZIP:
56+
zipCompress(nameOfFile2Compress, nameOfCompressedFile, innerEntryName);
57+
break;
58+
case NONE:
59+
throw new UnsupportedOperationException(
60+
"compress method called in NONE compression mode");
6261
}
6362
}
6463

@@ -67,12 +66,12 @@ private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String
6766

6867
if (!file2zip.exists()) {
6968
addStatus(new WarnStatus("The file to compress named [" + nameOfFile2zip
70-
+ "] does not exist.", this));
69+
+ "] does not exist.", this));
7170

7271
return;
7372
}
7473

75-
if(innerEntryName == null) {
74+
if (innerEntryName == null) {
7675
addStatus(new WarnStatus("The innerEntryName parameter cannot be null", this));
7776
return;
7877
}
@@ -85,11 +84,14 @@ private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String
8584

8685
if (zippedFile.exists()) {
8786
addStatus(new WarnStatus("The target compressed file named ["
88-
+ nameOfZippedFile + "] exist already.", this));
87+
+ nameOfZippedFile + "] exist already.", this));
8988

9089
return;
9190
}
9291

92+
addInfo("ZIP compressing [" + file2zip + "] as ["+zippedFile+"]");
93+
createMissingTargetDirsIfNecessary(zippedFile);
94+
9395
BufferedInputStream bis = null;
9496
ZipOutputStream zos = null;
9597
try {
@@ -113,20 +115,20 @@ private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String
113115

114116
if (!file2zip.delete()) {
115117
addStatus(new WarnStatus("Could not delete [" + nameOfFile2zip + "].",
116-
this));
118+
this));
117119
}
118120
} catch (Exception e) {
119121
addStatus(new ErrorStatus("Error occurred while compressing ["
120-
+ nameOfFile2zip + "] into [" + nameOfZippedFile + "].", this, e));
122+
+ nameOfFile2zip + "] into [" + nameOfZippedFile + "].", this, e));
121123
} finally {
122-
if(bis != null) {
124+
if (bis != null) {
123125
try {
124126
bis.close();
125127
} catch (IOException e) {
126128
// ignore
127129
}
128130
}
129-
if(zos != null) {
131+
if (zos != null) {
130132
try {
131133
zos.close();
132134
} catch (IOException e) {
@@ -168,24 +170,27 @@ private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {
168170

169171
if (!file2gz.exists()) {
170172
addStatus(new WarnStatus("The file to compress named [" + nameOfFile2gz
171-
+ "] does not exist.", this));
173+
+ "] does not exist.", this));
172174

173175
return;
174176
}
175177

178+
176179
if (!nameOfgzedFile.endsWith(".gz")) {
177180
nameOfgzedFile = nameOfgzedFile + ".gz";
178181
}
179182

180183
File gzedFile = new File(nameOfgzedFile);
181184

182185
if (gzedFile.exists()) {
183-
addStatus(new WarnStatus("The target compressed file named ["
184-
+ nameOfgzedFile + "] exist already.", this));
185-
186+
addWarn("The target compressed file named ["
187+
+ nameOfgzedFile + "] exist already. Aborting file compression.");
186188
return;
187189
}
188190

191+
addInfo("GZ compressing [" + file2gz + "] as ["+gzedFile+"]");
192+
createMissingTargetDirsIfNecessary(gzedFile);
193+
189194
BufferedInputStream bis = null;
190195
GZIPOutputStream gzos = null;
191196
try {
@@ -205,20 +210,20 @@ private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {
205210

206211
if (!file2gz.delete()) {
207212
addStatus(new WarnStatus("Could not delete [" + nameOfFile2gz + "].",
208-
this));
213+
this));
209214
}
210215
} catch (Exception e) {
211216
addStatus(new ErrorStatus("Error occurred while compressing ["
212-
+ nameOfFile2gz + "] into [" + nameOfgzedFile + "].", this, e));
217+
+ nameOfFile2gz + "] into [" + nameOfgzedFile + "].", this, e));
213218
} finally {
214-
if(bis != null) {
219+
if (bis != null) {
215220
try {
216221
bis.close();
217222
} catch (IOException e) {
218223
// ignore
219224
}
220225
}
221-
if(gzos != null) {
226+
if (gzos != null) {
222227
try {
223228
gzos.close();
224229
} catch (IOException e) {
@@ -233,24 +238,38 @@ static public String computeFileNameStr_WCS(String fileNamePatternStr,
233238
int len = fileNamePatternStr.length();
234239
switch (compressionMode) {
235240
case GZ:
236-
if(fileNamePatternStr.endsWith(".gz"))
237-
return fileNamePatternStr.substring(0, len - 3);
238-
else
239-
return fileNamePatternStr;
241+
if (fileNamePatternStr.endsWith(".gz"))
242+
return fileNamePatternStr.substring(0, len - 3);
243+
else
244+
return fileNamePatternStr;
240245
case ZIP:
241-
if(fileNamePatternStr.endsWith(".zip"))
242-
return fileNamePatternStr.substring(0, len - 4);
243-
else
244-
return fileNamePatternStr;
246+
if (fileNamePatternStr.endsWith(".zip"))
247+
return fileNamePatternStr.substring(0, len - 4);
248+
else
249+
return fileNamePatternStr;
245250
case NONE:
246251
return fileNamePatternStr;
247252
}
248253
throw new IllegalStateException("Execution should not reach this point");
249254
}
250255

256+
257+
void createMissingTargetDirsIfNecessary(File file) {
258+
if (FileUtil.isParentDirectoryCreationRequired(file)) {
259+
boolean result = FileUtil.createMissingParentDirectories(file);
260+
if (!result) {
261+
addError("Failed to create parent directories for ["
262+
+ file.getAbsolutePath() + "]");
263+
} else {
264+
addInfo("Created missing parent directories for ["
265+
+ file.getAbsolutePath() + "]");
266+
}
267+
}
268+
}
269+
251270
@Override
252271
public String toString() {
253-
return "c.q.l.core.rolling.helper.Compress";
272+
return this.getClass().getName();
254273
}
255274

256275
}

logback-core/src/test/java/ch/qos/logback/core/util/Compare.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@
1919
import java.io.FileReader;
2020
import java.io.IOException;
2121
import java.io.InputStreamReader;
22+
import java.util.Enumeration;
2223
import java.util.zip.GZIPInputStream;
24+
import java.util.zip.ZipEntry;
25+
import java.util.zip.ZipFile;
26+
import java.util.zip.ZipInputStream;
2327

2428
public class Compare {
2529
static final int B1_NULL = -1;
2630
static final int B2_NULL = -2;
2731

2832
public static boolean compare(String file1, String file2) throws FileNotFoundException, IOException {
2933
if (file1.endsWith(".gz")) {
30-
//System.out.println(file1 +" is a gz file");
3134
return gzFileCompare(file1, file2);
35+
} else if(file1.endsWith(".zip")) {
36+
return zipFileCompare(file1, file2);
3237
} else {
3338
return regularFileCompare(file1, file2);
3439
}
@@ -39,13 +44,24 @@ static BufferedReader gzFileToBufferedReader(String file) throws IOException {
3944
GZIPInputStream gzis = new GZIPInputStream(fis);
4045
return new BufferedReader(new InputStreamReader(gzis));
4146
}
42-
47+
48+
static BufferedReader zipFileToBufferedReader(String file) throws IOException {
49+
FileInputStream fis = new FileInputStream(file);
50+
ZipInputStream zis = new ZipInputStream(fis);
51+
zis.getNextEntry();
52+
return new BufferedReader(new InputStreamReader(zis));
53+
}
4354
public static boolean gzFileCompare(String file1, String file2) throws IOException {
4455
BufferedReader in1 = gzFileToBufferedReader(file1);
4556
BufferedReader in2 = gzFileToBufferedReader(file2);
4657
return bufferCompare(in1, in2, file1, file2);
4758
}
4859

60+
public static boolean zipFileCompare(String file1, String file2) throws IOException {
61+
BufferedReader in1 = zipFileToBufferedReader(file1);
62+
BufferedReader in2 = zipFileToBufferedReader(file2);
63+
return bufferCompare(in1, in2, file1, file2);
64+
}
4965
public static boolean regularFileCompare(String file1, String file2)
5066
throws FileNotFoundException, IOException {
5167
BufferedReader in1 = new BufferedReader(new FileReader(file1));

logback-core/src/test/scala/ch/qos/logback/core/rolling/RollingScaffolding.scala

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
* either the terms of the Eclipse Public License v1.0 as published by
77
* the Eclipse Foundation
88
*
9-
* or (per the licensee's choosing)
9+
* or (per the licensee's choosing)
1010
*
1111
* under the terms of the GNU Lesser General Public License version 2.1
1212
* as published by the Free Software Foundation.
1313
*/
1414
package ch.qos.logback.core.rolling
1515

1616
import ch.qos.logback.core.{ContextBase, Context}
17-
import helper.FileFilterUtil
17+
import helper.{FileNamePattern, FileFilterUtil}
1818
import java.util.concurrent.TimeUnit
1919
import ch.qos.logback.core.util.{CachingDateFormatter, CoreTestConstants}
2020
import org.junit.Assert._
@@ -29,7 +29,7 @@ trait RollingScaffolding {
2929

3030
val context: Context = new ContextBase
3131
val sm = context.getStatusManager()
32-
32+
3333
var diff: Int = RandomUtil.getPositiveInt
3434

3535
protected var currentTime: Long = 0L
@@ -63,24 +63,23 @@ trait RollingScaffolding {
6363
return new Date(currentTime - delta)
6464
}
6565

66-
protected def addExpectedFileName_ByDate(outputDir: String, testId: String, millis: Long, gzExtension: Boolean): Unit = {
67-
var fn: String = outputDir + testId + "-" + SDF.format(millis)
68-
if (gzExtension) {
69-
fn += ".gz"
70-
}
66+
protected def addExpectedFileName_ByDate(patternStr: String, millis: Long): Unit = {
67+
val fileNamePattern = new FileNamePattern(patternStr, context)
68+
var fn: String = fileNamePattern.convert(new Date(millis))
69+
println("fn=" + fn)
7170
expectedFilenameList = expectedFilenameList ::: List(fn)
7271

7372
}
7473

7574
protected def addExpectedFileName_ByFileIndexCounter(randomOutputDir: String, testId: String, millis: Long, fileIndexCounter: Int, compressionSuffix: String): Unit = {
76-
var fn: String = randomOutputDir + testId + "-" + SDF.format(millis) + "-" + fileIndexCounter + ".txt"+compressionSuffix
75+
var fn: String = randomOutputDir + testId + "-" + SDF.format(millis) + "-" + fileIndexCounter + ".txt" + compressionSuffix
7776
expectedFilenameList = expectedFilenameList ::: List(fn)
7877
}
7978

80-
81-
protected def addExpectedFileNamedIfItsTime_ByDate(outputDir: String, testId: String, gzExtension: Boolean): Unit = {
79+
80+
protected def addExpectedFileNamedIfItsTime_ByDate(fileNamePatternStr: String): Unit = {
8281
if (passThresholdTime(nextRolloverThreshold)) {
83-
addExpectedFileName_ByDate(outputDir, testId, getMillisOfCurrentPeriodsStart, gzExtension)
82+
addExpectedFileName_ByDate(fileNamePatternStr, getMillisOfCurrentPeriodsStart)
8483
recomputeRolloverThreshold(currentTime)
8584
}
8685
}
@@ -112,9 +111,16 @@ trait RollingScaffolding {
112111
// =========================================================================
113112
// utility methods
114113
// =========================================================================
115-
private[rolling] def massageExpectedFilesToCorresponToCurrentTarget(file: String): Unit = {
114+
private[rolling] def massageExpectedFilesToCorresponToCurrentTarget(file: String, fileOptionIsSet: Boolean): Unit = {
115+
val last: String = expectedFilenameList.last
116116
expectedFilenameList = expectedFilenameList.dropRight(1)
117-
expectedFilenameList = expectedFilenameList ::: List(file)
117+
118+
if (fileOptionIsSet) {
119+
expectedFilenameList = expectedFilenameList ::: List(file)
120+
} else if (last.endsWith(".gz")) {
121+
val stem = last.dropRight(3)
122+
expectedFilenameList = expectedFilenameList ::: List(stem)
123+
}
118124
}
119125

120126
def existenceCheck(filenameList: List[String]): Unit = {
@@ -133,7 +139,7 @@ trait RollingScaffolding {
133139
FileFilterUtil.sortFileArrayByName(fileArray)
134140
fileContentCheck(fileArray, runLength, prefix)
135141
}
136-
142+
137143
def reverseSortedContentCheck(outputDirStr: String, runLength: Int, prefix: String) {
138144
var fileArray: Array[File] = getFilesInDirectory(outputDirStr)
139145
FileFilterUtil.reverseSortFileArrayByName(fileArray)

logback-core/src/test/scala/ch/qos/logback/core/rolling/SizeAndTimeBasedFNATP_STest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ class SizeAndTimeBasedFNATP_STest extends RollingScaffolding {
106106
secondPhase
107107
runLength = runLength*2
108108
}
109-
109+
110110
if (stem != null)
111-
massageExpectedFilesToCorresponToCurrentTarget(file)
111+
massageExpectedFilesToCorresponToCurrentTarget(file, true)
112112

113113
Thread.sleep(20)
114114
// wait for compression to finish

0 commit comments

Comments
 (0)