Fixed #1513 paranoid cleanup
This commit is contained in:
parent
d9f9791e39
commit
2d8ef2f5f0
|
@ -24,9 +24,7 @@ import java.io.FilterOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.ZoneId;
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
@ -55,7 +53,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
final static int ROLLOVER_FILE_RETAIN_DAYS = 31;
|
final static int ROLLOVER_FILE_RETAIN_DAYS = 31;
|
||||||
|
|
||||||
private RollTask _rollTask;
|
private RollTask _rollTask;
|
||||||
private ZonedDateTime _midnight;
|
|
||||||
private SimpleDateFormat _fileBackupFormat;
|
private SimpleDateFormat _fileBackupFormat;
|
||||||
private SimpleDateFormat _fileDateFormat;
|
private SimpleDateFormat _fileDateFormat;
|
||||||
|
|
||||||
|
@ -120,7 +117,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
TimeZone zone)
|
TimeZone zone)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
|
|
||||||
this(filename,append,retainDays,zone,null,null);
|
this(filename,append,retainDays,zone,null,null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -168,7 +164,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
_filename=filename;
|
_filename=filename;
|
||||||
_append=append;
|
_append=append;
|
||||||
_retainDays=retainDays;
|
_retainDays=retainDays;
|
||||||
setFile();
|
|
||||||
|
|
||||||
synchronized(RolloverFileOutputStream.class)
|
synchronized(RolloverFileOutputStream.class)
|
||||||
{
|
{
|
||||||
|
@ -176,8 +171,8 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
__rollover=new Timer(RolloverFileOutputStream.class.getName(),true);
|
__rollover=new Timer(RolloverFileOutputStream.class.getName(),true);
|
||||||
|
|
||||||
// Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds)
|
// Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds)
|
||||||
ZonedDateTime now = ZonedDateTime.now(zone.toZoneId());
|
ZonedDateTime now = ZonedDateTime.now(zone.toZoneId());
|
||||||
_midnight = toMidnight(now, zone.toZoneId());
|
setFile(now);
|
||||||
// This will schedule the rollover event to the next midnight
|
// This will schedule the rollover event to the next midnight
|
||||||
scheduleNextRollover(now);
|
scheduleNextRollover(now);
|
||||||
}
|
}
|
||||||
|
@ -187,28 +182,12 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
/**
|
/**
|
||||||
* Get the "start of day" for the provided DateTime at the zone specified.
|
* Get the "start of day" for the provided DateTime at the zone specified.
|
||||||
*
|
*
|
||||||
* @param dateTime the date time to calculate from
|
* @param now the date time to calculate from
|
||||||
* @param zone the zone to return the date in
|
|
||||||
* @return start of the day of the date provided
|
* @return start of the day of the date provided
|
||||||
*/
|
*/
|
||||||
public static ZonedDateTime toMidnight(ZonedDateTime dateTime, ZoneId zone)
|
public static ZonedDateTime toMidnight(ZonedDateTime now)
|
||||||
{
|
{
|
||||||
return dateTime.toLocalDate().atStartOfDay(zone);
|
return now.toLocalDate().atStartOfDay(now.getZone());
|
||||||
}
|
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
|
||||||
/**
|
|
||||||
* Get the next "start of day" for the provided date.
|
|
||||||
*
|
|
||||||
* @param dateTime the date to calculate from
|
|
||||||
* @return the start of the next day
|
|
||||||
*/
|
|
||||||
public static ZonedDateTime nextMidnight(ZonedDateTime dateTime)
|
|
||||||
{
|
|
||||||
// Increment to next day, based on Configured TimeZone, then find start of that day.
|
|
||||||
// Will always be in the future, even if the Daylights Savings Time kicks in during
|
|
||||||
// the calculation.
|
|
||||||
return dateTime.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(dateTime.getZone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
|
@ -216,11 +195,10 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
{
|
{
|
||||||
_rollTask = new RollTask();
|
_rollTask = new RollTask();
|
||||||
// Get tomorrow's midnight based on Configured TimeZone
|
// Get tomorrow's midnight based on Configured TimeZone
|
||||||
while (_midnight.isBefore(now))
|
ZonedDateTime midnight = toMidnight(now);
|
||||||
_midnight = nextMidnight(_midnight);
|
|
||||||
|
|
||||||
long delay = _midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli();
|
|
||||||
// Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds
|
// Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds
|
||||||
|
long delay = midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli();
|
||||||
__rollover.schedule(_rollTask,delay);
|
__rollover.schedule(_rollTask,delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,7 +223,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
private synchronized void setFile()
|
private synchronized void setFile(ZonedDateTime now)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
// Check directory
|
// Check directory
|
||||||
|
@ -256,8 +234,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
if (!dir.isDirectory() || !dir.canWrite())
|
if (!dir.isDirectory() || !dir.canWrite())
|
||||||
throw new IOException("Cannot write log directory "+dir);
|
throw new IOException("Cannot write log directory "+dir);
|
||||||
|
|
||||||
Date now=new Date();
|
|
||||||
|
|
||||||
// Is this a rollover file?
|
// Is this a rollover file?
|
||||||
String filename=file.getName();
|
String filename=file.getName();
|
||||||
int i=filename.toLowerCase(Locale.ENGLISH).indexOf(YYYY_MM_DD);
|
int i=filename.toLowerCase(Locale.ENGLISH).indexOf(YYYY_MM_DD);
|
||||||
|
@ -265,7 +241,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
{
|
{
|
||||||
file=new File(dir,
|
file=new File(dir,
|
||||||
filename.substring(0,i)+
|
filename.substring(0,i)+
|
||||||
_fileDateFormat.format(now)+
|
_fileDateFormat.format(new Date(now.toInstant().toEpochMilli()))+
|
||||||
filename.substring(i+YYYY_MM_DD.length()));
|
filename.substring(i+YYYY_MM_DD.length()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,19 +299,19 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
@Override
|
@Override
|
||||||
public void write (byte[] buf)
|
public void write (byte[] buf)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
out.write (buf);
|
out.write (buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
@Override
|
@Override
|
||||||
public void write (byte[] buf, int off, int len)
|
public void write (byte[] buf, int off, int len)
|
||||||
throws IOException
|
throws IOException
|
||||||
{
|
{
|
||||||
out.write (buf, off, len);
|
out.write (buf, off, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
/* ------------------------------------------------------------ */
|
||||||
@Override
|
@Override
|
||||||
public void close()
|
public void close()
|
||||||
|
@ -367,8 +343,8 @@ public class RolloverFileOutputStream extends FilterOutputStream
|
||||||
{
|
{
|
||||||
synchronized(RolloverFileOutputStream.class)
|
synchronized(RolloverFileOutputStream.class)
|
||||||
{
|
{
|
||||||
ZonedDateTime now = ZonedDateTime.now(_midnight.getZone());
|
ZonedDateTime now = ZonedDateTime.now(_fileDateFormat.getTimeZone().toZoneId());
|
||||||
RolloverFileOutputStream.this.setFile();
|
RolloverFileOutputStream.this.setFile(now);
|
||||||
RolloverFileOutputStream.this.scheduleNextRollover(now);
|
RolloverFileOutputStream.this.scheduleNextRollover(now);
|
||||||
RolloverFileOutputStream.this.removeOldFiles(now);
|
RolloverFileOutputStream.this.removeOldFiles(now);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import static org.junit.Assert.assertThat;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.time.temporal.TemporalAccessor;
|
import java.time.temporal.TemporalAccessor;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
@ -58,7 +59,7 @@ public class RolloverFileOutputStreamTest
|
||||||
for (int i = 0; i < expected.length; i++)
|
for (int i = 0; i < expected.length; i++)
|
||||||
{
|
{
|
||||||
long lastMs = nextEvent.toInstant().toEpochMilli();
|
long lastMs = nextEvent.toInstant().toEpochMilli();
|
||||||
nextEvent = RolloverFileOutputStream.nextMidnight(nextEvent);
|
nextEvent = nextEvent.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(nextEvent.getZone());
|
||||||
assertThat("Next Event", toString(nextEvent), is(expected[i][0]));
|
assertThat("Next Event", toString(nextEvent), is(expected[i][0]));
|
||||||
long duration = (nextEvent.toInstant().toEpochMilli() - lastMs);
|
long duration = (nextEvent.toInstant().toEpochMilli() - lastMs);
|
||||||
assertThat("Duration to next event", duration, is((long) expected[i][1]));
|
assertThat("Duration to next event", duration, is((long) expected[i][1]));
|
||||||
|
@ -71,7 +72,7 @@ public class RolloverFileOutputStreamTest
|
||||||
ZoneId zone = toZoneId("PST");
|
ZoneId zone = toZoneId("PST");
|
||||||
ZonedDateTime initialDate = toDateTime("2016.03.11-01:23:45.0 PM PST", zone);
|
ZonedDateTime initialDate = toDateTime("2016.03.11-01:23:45.0 PM PST", zone);
|
||||||
|
|
||||||
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
|
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
|
||||||
assertThat("Midnight", toString(midnight), is("2016.03.11-12:00:00.0 AM PST"));
|
assertThat("Midnight", toString(midnight), is("2016.03.11-12:00:00.0 AM PST"));
|
||||||
|
|
||||||
Object expected[][] = {
|
Object expected[][] = {
|
||||||
|
@ -91,7 +92,7 @@ public class RolloverFileOutputStreamTest
|
||||||
ZoneId zone = toZoneId("PST");
|
ZoneId zone = toZoneId("PST");
|
||||||
ZonedDateTime initialDate = toDateTime("2016.11.04-11:22:33.0 AM PDT", zone);
|
ZonedDateTime initialDate = toDateTime("2016.11.04-11:22:33.0 AM PDT", zone);
|
||||||
|
|
||||||
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
|
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
|
||||||
assertThat("Midnight", toString(midnight), is("2016.11.04-12:00:00.0 AM PDT"));
|
assertThat("Midnight", toString(midnight), is("2016.11.04-12:00:00.0 AM PDT"));
|
||||||
|
|
||||||
Object expected[][] = {
|
Object expected[][] = {
|
||||||
|
@ -111,7 +112,7 @@ public class RolloverFileOutputStreamTest
|
||||||
ZoneId zone = toZoneId("Australia/Sydney");
|
ZoneId zone = toZoneId("Australia/Sydney");
|
||||||
ZonedDateTime initialDate = toDateTime("2016.10.01-01:23:45.0 PM AEST", zone);
|
ZonedDateTime initialDate = toDateTime("2016.10.01-01:23:45.0 PM AEST", zone);
|
||||||
|
|
||||||
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
|
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
|
||||||
assertThat("Midnight", toString(midnight), is("2016.10.01-12:00:00.0 AM AEST"));
|
assertThat("Midnight", toString(midnight), is("2016.10.01-12:00:00.0 AM AEST"));
|
||||||
|
|
||||||
Object expected[][] = {
|
Object expected[][] = {
|
||||||
|
@ -131,7 +132,7 @@ public class RolloverFileOutputStreamTest
|
||||||
ZoneId zone = toZoneId("Australia/Sydney");
|
ZoneId zone = toZoneId("Australia/Sydney");
|
||||||
ZonedDateTime initialDate = toDateTime("2016.04.02-11:22:33.0 AM AEDT", zone);
|
ZonedDateTime initialDate = toDateTime("2016.04.02-11:22:33.0 AM AEDT", zone);
|
||||||
|
|
||||||
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
|
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
|
||||||
assertThat("Midnight", toString(midnight), is("2016.04.02-12:00:00.0 AM AEDT"));
|
assertThat("Midnight", toString(midnight), is("2016.04.02-12:00:00.0 AM AEDT"));
|
||||||
|
|
||||||
Object expected[][] = {
|
Object expected[][] = {
|
||||||
|
|
Loading…
Reference in New Issue